ua_plugin_log.h 3.7 KB

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