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