ua_plugin_log.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <stdarg.h>
  11. #include "ua_config.h"
  12. _UA_BEGIN_DECLS
  13. /**
  14. * Logging Plugin API
  15. * ==================
  16. *
  17. * Servers and clients must define a logger in their configuration. The logger
  18. * is just a function pointer. Every log-message consists of a log-level, a
  19. * log-category and a string message content. The timestamp of the log-message
  20. * is created within the logger. */
  21. typedef enum {
  22. UA_LOGLEVEL_TRACE,
  23. UA_LOGLEVEL_DEBUG,
  24. UA_LOGLEVEL_INFO,
  25. UA_LOGLEVEL_WARNING,
  26. UA_LOGLEVEL_ERROR,
  27. UA_LOGLEVEL_FATAL
  28. } UA_LogLevel;
  29. typedef enum {
  30. UA_LOGCATEGORY_NETWORK,
  31. UA_LOGCATEGORY_SECURECHANNEL,
  32. UA_LOGCATEGORY_SESSION,
  33. UA_LOGCATEGORY_SERVER,
  34. UA_LOGCATEGORY_CLIENT,
  35. UA_LOGCATEGORY_USERLAND,
  36. UA_LOGCATEGORY_SECURITYPOLICY
  37. } UA_LogCategory;
  38. typedef struct {
  39. /* The message string and following varargs are formatted according to the
  40. * rules of the printf command. Use the convenience macros below that take
  41. * the minimum log-level defined in ua_config.h into account. */
  42. void (*log)(void *logContext, UA_LogLevel level, UA_LogCategory category,
  43. const char *msg, va_list args);
  44. void *logContext;
  45. void (*clear)(void *logContext);
  46. } UA_Logger;
  47. static UA_INLINE UA_FORMAT(3,4) void
  48. UA_LOG_TRACE(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  49. #if UA_LOGLEVEL <= 100
  50. if(!logger || !logger->log)
  51. return;
  52. va_list args; va_start(args, msg);
  53. logger->log(logger->logContext, UA_LOGLEVEL_TRACE, category, msg, args);
  54. va_end(args);
  55. #endif
  56. }
  57. static UA_INLINE UA_FORMAT(3,4) void
  58. UA_LOG_DEBUG(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  59. #if UA_LOGLEVEL <= 200
  60. if(!logger || !logger->log)
  61. return;
  62. va_list args; va_start(args, msg);
  63. logger->log(logger->logContext, UA_LOGLEVEL_DEBUG, category, msg, args);
  64. va_end(args);
  65. #endif
  66. }
  67. static UA_INLINE UA_FORMAT(3,4) void
  68. UA_LOG_INFO(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  69. #if UA_LOGLEVEL <= 300
  70. if(!logger || !logger->log)
  71. return;
  72. va_list args; va_start(args, msg);
  73. logger->log(logger->logContext, UA_LOGLEVEL_INFO, category, msg, args);
  74. va_end(args);
  75. #endif
  76. }
  77. static UA_INLINE UA_FORMAT(3,4) void
  78. UA_LOG_WARNING(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  79. #if UA_LOGLEVEL <= 400
  80. if(!logger || !logger->log)
  81. return;
  82. va_list args; va_start(args, msg);
  83. logger->log(logger->logContext, UA_LOGLEVEL_WARNING, category, msg, args);
  84. va_end(args);
  85. #endif
  86. }
  87. static UA_INLINE UA_FORMAT(3,4) void
  88. UA_LOG_ERROR(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  89. #if UA_LOGLEVEL <= 500
  90. if(!logger || !logger->log)
  91. return;
  92. va_list args; va_start(args, msg);
  93. logger->log(logger->logContext, UA_LOGLEVEL_ERROR, category, msg, args);
  94. va_end(args);
  95. #endif
  96. }
  97. static UA_INLINE UA_FORMAT(3,4) void
  98. UA_LOG_FATAL(const UA_Logger *logger, UA_LogCategory category, const char *msg, ...) {
  99. #if UA_LOGLEVEL <= 600
  100. if(!logger || !logger->log)
  101. return;
  102. va_list args; va_start(args, msg);
  103. logger->log(logger->logContext, UA_LOGLEVEL_FATAL, category, msg, args);
  104. va_end(args);
  105. #endif
  106. }
  107. _UA_END_DECLS
  108. #endif /* UA_PLUGIN_LOG_H_ */