log.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef SRC_LOG_H_
  3. #define SRC_LOG_H_
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <tss2/tss2_sys.h>
  7. #include <tss2/tss2_rc.h>
  8. #include "tpm2_util.h"
  9. typedef enum log_level log_level;
  10. enum log_level {
  11. log_level_error,
  12. log_level_warning,
  13. log_level_verbose
  14. };
  15. void _log (log_level level, const char *file, unsigned lineno, const char *fmt, ...)
  16. COMPILER_ATTR(format (printf, 4, 5));
  17. /*
  18. * Prints an error message. The fmt and variadic arguments mirror printf.
  19. *
  20. * Use this to log all error conditions.
  21. */
  22. #define LOG_ERR(fmt, ...) _log(log_level_error, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  23. /**
  24. * Prints an error message for a TSS2_Sys call to the TPM.
  25. * The format is <function-name>(0x<rc>) - <error string>
  26. * @param func
  27. * The function that caused the error
  28. * @param rc
  29. * The return code to print.
  30. */
  31. #define LOG_PERR(func, rc) _LOG_PERR(xstr(func), rc)
  32. /**
  33. * Internal use only.
  34. *
  35. * Handles the expanded LOG_PERR call checking argument values
  36. * and handing them off to LOG_ERR.
  37. * @param func
  38. * The function name.
  39. * @param rc
  40. * The rc to decode.
  41. */
  42. static inline void _LOG_PERR(const char *func, TSS2_RC rc) {
  43. LOG_ERR("%s(0x%X) - %s", func, rc, Tss2_RC_Decode(rc));
  44. }
  45. /*
  46. * Prints an warning message. The fmt and variadic arguments mirror printf.
  47. *
  48. * Use this to log a warning. A warning is when something is wrong, but it is not a fatal
  49. * issue.
  50. */
  51. #define LOG_WARN(fmt, ...) _log(log_level_warning, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  52. /*
  53. * Prints an informational message. The fmt and variadic arguments mirror printf.
  54. *
  55. * Informational messages are only shown when verboseness is increased. Valid messages
  56. * would be debugging type messages where additional, extraneous information is printed.
  57. */
  58. #define LOG_INFO(fmt, ...) _log(log_level_verbose, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  59. /**
  60. * Sets the log level so only messages <= to it print.
  61. * @param level
  62. * The logging level to set.
  63. */
  64. void log_set_level(log_level level);
  65. #endif /* SRC_LOG_H_ */