log.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <open62541/config.h>
  11. #include <stdarg.h>
  12. _UA_BEGIN_DECLS
  13. /**
  14. * Logging Plugin API
  15. * ==================
  16. *
  17. * Servers and clients define a logger in their configuration. The logger is a
  18. * plugin. A default plugin that logs to ``stdout`` is provided as an example.
  19. * The logger plugin is stateful and can point to custom data. So it is possible
  20. * to keep open file handlers in the logger context.
  21. *
  22. * Every log-message consists of a log-level, a log-category and a string
  23. * message content. The timestamp of the log-message is created within the
  24. * logger. */
  25. typedef enum {
  26. UA_LOGLEVEL_TRACE,
  27. UA_LOGLEVEL_DEBUG,
  28. UA_LOGLEVEL_INFO,
  29. UA_LOGLEVEL_WARNING,
  30. UA_LOGLEVEL_ERROR,
  31. UA_LOGLEVEL_FATAL
  32. } UA_LogLevel;
  33. typedef enum {
  34. UA_LOGCATEGORY_NETWORK,
  35. UA_LOGCATEGORY_SECURECHANNEL,
  36. UA_LOGCATEGORY_SESSION,
  37. UA_LOGCATEGORY_SERVER,
  38. UA_LOGCATEGORY_CLIENT,
  39. UA_LOGCATEGORY_USERLAND,
  40. UA_LOGCATEGORY_SECURITYPOLICY
  41. } UA_LogCategory;
  42. typedef struct {
  43. /* Log a message. The message string and following varargs are formatted
  44. * according to the rules of the printf command. Use the convenience macros
  45. * below that take the minimum log-level defined in ua_config.h into
  46. * account. */
  47. void (*log)(void *logContext, UA_LogLevel level, UA_LogCategory category,
  48. const char *msg, va_list args);
  49. void *context; /* Logger state */
  50. void (*clear)(void *context); /* Clean up the logger plugin */
  51. } UA_Logger;
  52. static UA_INLINE UA_FORMAT(3,4) void
  53. UA_LOG_TRACE(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  54. #if UA_LOGLEVEL <= 100
  55. if(!logger || !logger->log)
  56. return;
  57. va_list args; va_start(args, msg);
  58. logger->log(logger->context, UA_LOGLEVEL_TRACE, category, msg, args);
  59. va_end(args);
  60. #else
  61. (void) logger;
  62. (void) category;
  63. (void) msg;
  64. #endif
  65. }
  66. static UA_INLINE UA_FORMAT(3,4) void
  67. UA_LOG_DEBUG(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  68. #if UA_LOGLEVEL <= 200
  69. if(!logger || !logger->log)
  70. return;
  71. va_list args; va_start(args, msg);
  72. logger->log(logger->context, UA_LOGLEVEL_DEBUG, category, msg, args);
  73. va_end(args);
  74. #else
  75. (void) logger;
  76. (void) category;
  77. (void) msg;
  78. #endif
  79. }
  80. static UA_INLINE UA_FORMAT(3,4) void
  81. UA_LOG_INFO(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  82. #if UA_LOGLEVEL <= 300
  83. if(!logger || !logger->log)
  84. return;
  85. va_list args; va_start(args, msg);
  86. logger->log(logger->context, UA_LOGLEVEL_INFO, category, msg, args);
  87. va_end(args);
  88. #else
  89. (void) logger;
  90. (void) category;
  91. (void) msg;
  92. #endif
  93. }
  94. static UA_INLINE UA_FORMAT(3,4) void
  95. UA_LOG_WARNING(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  96. #if UA_LOGLEVEL <= 400
  97. if(!logger || !logger->log)
  98. return;
  99. va_list args; va_start(args, msg);
  100. logger->log(logger->context, UA_LOGLEVEL_WARNING, category, msg, args);
  101. va_end(args);
  102. #else
  103. (void) logger;
  104. (void) category;
  105. (void) msg;
  106. #endif
  107. }
  108. static UA_INLINE UA_FORMAT(3,4) void
  109. UA_LOG_ERROR(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  110. #if UA_LOGLEVEL <= 500
  111. if(!logger || !logger->log)
  112. return;
  113. va_list args; va_start(args, msg);
  114. logger->log(logger->context, UA_LOGLEVEL_ERROR, category, msg, args);
  115. va_end(args);
  116. #else
  117. (void) logger;
  118. (void) category;
  119. (void) msg;
  120. #endif
  121. }
  122. static UA_INLINE UA_FORMAT(3,4) void
  123. UA_LOG_FATAL(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  124. #if UA_LOGLEVEL <= 600
  125. if(!logger || !logger->log)
  126. return;
  127. va_list args; va_start(args, msg);
  128. logger->log(logger->context, UA_LOGLEVEL_FATAL, category, msg, args);
  129. va_end(args);
  130. #else
  131. (void) logger;
  132. (void) category;
  133. (void) msg;
  134. #endif
  135. }
  136. _UA_END_DECLS
  137. #endif /* UA_PLUGIN_LOG_H_ */