log.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #endif
  61. }
  62. static UA_INLINE UA_FORMAT(3,4) void
  63. UA_LOG_DEBUG(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  64. #if UA_LOGLEVEL <= 200
  65. if(!logger || !logger->log)
  66. return;
  67. va_list args; va_start(args, msg);
  68. logger->log(logger->context, UA_LOGLEVEL_DEBUG, category, msg, args);
  69. va_end(args);
  70. #endif
  71. }
  72. static UA_INLINE UA_FORMAT(3,4) void
  73. UA_LOG_INFO(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  74. #if UA_LOGLEVEL <= 300
  75. if(!logger || !logger->log)
  76. return;
  77. va_list args; va_start(args, msg);
  78. logger->log(logger->context, UA_LOGLEVEL_INFO, category, msg, args);
  79. va_end(args);
  80. #endif
  81. }
  82. static UA_INLINE UA_FORMAT(3,4) void
  83. UA_LOG_WARNING(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  84. #if UA_LOGLEVEL <= 400
  85. if(!logger || !logger->log)
  86. return;
  87. va_list args; va_start(args, msg);
  88. logger->log(logger->context, UA_LOGLEVEL_WARNING, category, msg, args);
  89. va_end(args);
  90. #endif
  91. }
  92. static UA_INLINE UA_FORMAT(3,4) void
  93. UA_LOG_ERROR(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  94. #if UA_LOGLEVEL <= 500
  95. if(!logger || !logger->log)
  96. return;
  97. va_list args; va_start(args, msg);
  98. logger->log(logger->context, UA_LOGLEVEL_ERROR, category, msg, args);
  99. va_end(args);
  100. #endif
  101. }
  102. static UA_INLINE UA_FORMAT(3,4) void
  103. UA_LOG_FATAL(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  104. #if UA_LOGLEVEL <= 600
  105. if(!logger || !logger->log)
  106. return;
  107. va_list args; va_start(args, msg);
  108. logger->log(logger->context, UA_LOGLEVEL_FATAL, category, msg, args);
  109. va_end(args);
  110. #endif
  111. }
  112. _UA_END_DECLS
  113. #endif /* UA_PLUGIN_LOG_H_ */