ua_plugin_log.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_PLUGIN_LOG_H_
  5. #define UA_PLUGIN_LOG_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <stdarg.h>
  10. #include "ua_config.h"
  11. /**
  12. * Logging Plugin API
  13. * ==================
  14. *
  15. * Servers and clients must define a logger in their configuration. The logger
  16. * is just a function pointer. Every log-message consists of a log-level, a
  17. * log-category and a string message content. The timestamp of the log-message
  18. * is created within the logger. */
  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 message string and following varargs are formatted according to the rules
  37. * of the printf command. Do not call the logger directly. Instead, make use of
  38. * the convenience macros that take the minimum log-level defined in ua_config.h
  39. * into account. */
  40. typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category,
  41. const char *msg, va_list args);
  42. static UA_INLINE UA_FORMAT(3,4) void
  43. UA_LOG_TRACE(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  44. #if UA_LOGLEVEL <= 100
  45. va_list args; va_start(args, msg);
  46. logger(UA_LOGLEVEL_TRACE, category, msg, args);
  47. va_end(args);
  48. #endif
  49. }
  50. static UA_INLINE UA_FORMAT(3,4) void
  51. UA_LOG_DEBUG(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  52. #if UA_LOGLEVEL <= 200
  53. va_list args; va_start(args, msg);
  54. logger(UA_LOGLEVEL_DEBUG, category, msg, args);
  55. va_end(args);
  56. #endif
  57. }
  58. static UA_INLINE UA_FORMAT(3,4) void
  59. UA_LOG_INFO(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  60. #if UA_LOGLEVEL <= 300
  61. va_list args; va_start(args, msg);
  62. logger(UA_LOGLEVEL_INFO, category, msg, args);
  63. va_end(args);
  64. #endif
  65. }
  66. static UA_INLINE UA_FORMAT(3,4) void
  67. UA_LOG_WARNING(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  68. #if UA_LOGLEVEL <= 400
  69. va_list args; va_start(args, msg);
  70. logger(UA_LOGLEVEL_WARNING, category, msg, args);
  71. va_end(args);
  72. #endif
  73. }
  74. static UA_INLINE UA_FORMAT(3,4) void
  75. UA_LOG_ERROR(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  76. #if UA_LOGLEVEL <= 500
  77. va_list args; va_start(args, msg);
  78. logger(UA_LOGLEVEL_ERROR, category, msg, args);
  79. va_end(args);
  80. #endif
  81. }
  82. static UA_INLINE UA_FORMAT(3,4) void
  83. UA_LOG_FATAL(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  84. #if UA_LOGLEVEL <= 600
  85. va_list args; va_start(args, msg);
  86. logger(UA_LOGLEVEL_FATAL, category, msg, args);
  87. va_end(args);
  88. #endif
  89. }
  90. /**
  91. * Convenience macros for complex types
  92. * ------------------------------------ */
  93. #define UA_PRINTF_GUID_FORMAT "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"
  94. #define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
  95. (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
  96. (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
  97. #define UA_PRINTF_STRING_FORMAT "\"%.*s\""
  98. #define UA_PRINTF_STRING_DATA(STRING) (int)(STRING).length, (STRING).data
  99. #ifdef __cplusplus
  100. } // extern "C"
  101. #endif
  102. #endif /* UA_PLUGIN_LOG_H_ */