ua_plugin_log.h 3.2 KB

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