ua_log.h 3.1 KB

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