a93c1c26af769c4a48c76958ff9752b0230adbac
[aaf/sshsm.git] / TPM2-Plugin / lib / include / log.h
1 //**********************************************************************;
2 // Copyright (c) 2017, Intel Corporation
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of Intel Corporation nor the names of its contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 // THE POSSIBILITY OF SUCH DAMAGE.
30 //**********************************************************************;
31 #ifndef SRC_LOG_H_
32 #define SRC_LOG_H_
33
34 #include <stdbool.h>
35 #include <stdio.h>
36
37 #include <tss2/tss2_sys.h>
38
39 #include "tpm2_error.h"
40 #include "tpm2_util.h"
41
42 typedef enum log_level log_level;
43 enum log_level {
44     log_level_error,
45     log_level_warning,
46     log_level_verbose
47 };
48
49 void _log (log_level level, const char *file, unsigned lineno, const char *fmt, ...)
50     COMPILER_ATTR(format (printf, 4, 5));
51
52 /*
53  * Prints an error message. The fmt and variadic arguments mirror printf.
54  *
55  * Use this to log all error conditions.
56  */
57 #define LOG_ERR(fmt, ...) _log(log_level_error, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
58
59 /**
60  * Prints an error message for a TSS2_Sys call to the TPM.
61  * The format is <function-name>(0x<rc>) - <error string>
62  * @param func
63  *  The function that caused the error
64  * @param rc
65  *  The return code to print.
66  */
67 #define LOG_PERR(func, rc) _LOG_PERR(xstr(func), rc)
68
69 /**
70  * Internal use only.
71  *
72  * Handles the expanded LOG_PERR call checking argument values
73  * and handing them off to LOG_ERR.
74  * @param func
75  *  The function name.
76  * @param rc
77  *  The rc to decode.
78  */
79 static inline void _LOG_PERR(const char *func, TSS2_RC rc) {
80
81     LOG_ERR("%s(0x%X) - %s", func, rc, tpm2_error_str(rc));
82 }
83
84 /*
85  * Prints an warning message. The fmt and variadic arguments mirror printf.
86  *
87  * Use this to log a warning. A warning is when something is wrong, but it is not a fatal
88  * issue.
89  */
90 #define LOG_WARN(fmt, ...) _log(log_level_warning, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
91
92 /*
93  * Prints an informational message. The fmt and variadic arguments mirror printf.
94  *
95  * Informational messages are only shown when verboseness is increased. Valid messages
96  * would be debugging type messages where additional, extraneous information is printed.
97  */
98 #define LOG_INFO(fmt, ...) _log(log_level_verbose, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
99
100 /**
101  * Sets the log level so only messages <= to it print.
102  * @param level
103  *  The logging level to set.
104  */
105 void log_set_level (log_level level);
106
107 #endif /* SRC_LOG_H_ */