ua_log.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. #ifndef UA_LOG_H_
  5. #define UA_LOG_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "ua_config.h"
  10. /**
  11. * Logging
  12. * -------
  13. *
  14. * Servers and clients may contain a logger. Every logger needs to implement the
  15. * `UA_Logger` signature. An example logger that writes to stdout is provided in
  16. * the plugins folder.
  17. *
  18. * Every log-message consists of a log-level, a log-category and a string
  19. * message content. The timestamp of the log-message is created within the
  20. * logger.
  21. */
  22. typedef enum {
  23. UA_LOGLEVEL_TRACE,
  24. UA_LOGLEVEL_DEBUG,
  25. UA_LOGLEVEL_INFO,
  26. UA_LOGLEVEL_WARNING,
  27. UA_LOGLEVEL_ERROR,
  28. UA_LOGLEVEL_FATAL
  29. } UA_LogLevel;
  30. typedef enum {
  31. UA_LOGCATEGORY_NETWORK,
  32. UA_LOGCATEGORY_SECURECHANNEL,
  33. UA_LOGCATEGORY_SESSION,
  34. UA_LOGCATEGORY_SERVER,
  35. UA_LOGCATEGORY_CLIENT,
  36. UA_LOGCATEGORY_USERLAND
  37. } UA_LogCategory;
  38. /**
  39. * The signature of the logger. The msg string and following varargs are
  40. * formatted according to the rules of the printf command.
  41. *
  42. * Do not use the logger directly but make use of the following macros that take
  43. * the minimum log-level defined in ua_config.h into account. */
  44. typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category,
  45. const char *msg, ...);
  46. #if UA_LOGLEVEL <= 100
  47. #define UA_LOG_TRACE(LOGGER, CATEGORY, ...) do { \
  48. if(LOGGER) LOGGER(UA_LOGLEVEL_TRACE, CATEGORY, __VA_ARGS__); } while(0)
  49. #else
  50. #define UA_LOG_TRACE(LOGGER, CATEGORY, ...) do {} while(0)
  51. #endif
  52. #if UA_LOGLEVEL <= 200
  53. #define UA_LOG_DEBUG(LOGGER, CATEGORY, ...) do { \
  54. if(LOGGER) LOGGER(UA_LOGLEVEL_DEBUG, CATEGORY, __VA_ARGS__); } while(0)
  55. #else
  56. #define UA_LOG_DEBUG(LOGGER, CATEGORY, ...) do {} while(0)
  57. #endif
  58. #if UA_LOGLEVEL <= 300
  59. #define UA_LOG_INFO(LOGGER, CATEGORY, ...) do { \
  60. if(LOGGER) LOGGER(UA_LOGLEVEL_INFO, CATEGORY, __VA_ARGS__); } while(0)
  61. #else
  62. #define UA_LOG_INFO(LOGGER, CATEGORY, ...) do {} while(0)
  63. #endif
  64. #if UA_LOGLEVEL <= 400
  65. #define UA_LOG_WARNING(LOGGER, CATEGORY, ...) do { \
  66. if(LOGGER) LOGGER(UA_LOGLEVEL_WARNING, CATEGORY, __VA_ARGS__); } while(0)
  67. #else
  68. #define UA_LOG_WARNING(LOGGER, CATEGORY, ...) do {} while(0)
  69. #endif
  70. #if UA_LOGLEVEL <= 500
  71. #define UA_LOG_ERROR(LOGGER, CATEGORY, ...) do { \
  72. if(LOGGER) LOGGER(UA_LOGLEVEL_ERROR, CATEGORY, __VA_ARGS__); } while(0)
  73. #else
  74. #define UA_LOG_ERROR(LOGGER, CATEGORY, ...) do {} while(0)
  75. #endif
  76. #if UA_LOGLEVEL <= 600
  77. #define UA_LOG_FATAL(LOGGER, CATEGORY, ...) do { \
  78. if(LOGGER) LOGGER(UA_LOGLEVEL_FATAL, CATEGORY, __VA_ARGS__); } while(0)
  79. #else
  80. #define UA_LOG_FATAL(LOGGER, CATEGORY, ...) do {} while(0)
  81. #endif
  82. /**
  83. * Convenience macros for complex types
  84. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  85. #define UA_PRINTF_GUID_FORMAT "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"
  86. #define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
  87. (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
  88. (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
  89. #define UA_PRINTF_STRING_FORMAT "\"%.*s\""
  90. #define UA_PRINTF_STRING_DATA(STRING) (STRING).length, (STRING).data
  91. #ifdef __cplusplus
  92. } // extern "C"
  93. #endif
  94. #endif /* UA_LOG_H_ */