ua_plugin_log.h 3.5 KB

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