ua_plugin_log.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  42. * The message string and following varargs are formatted according to the rules
  43. * of the printf command. Do not call the logger directly. Instead, make use of
  44. * the convenience macros that take the minimum log-level defined in ua_config.h
  45. * into account. */
  46. typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category,
  47. const char *msg, va_list args);
  48. static UA_INLINE UA_FORMAT(3,4) void
  49. UA_LOG_TRACE(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  50. #if UA_LOGLEVEL <= 100
  51. va_list args; va_start(args, msg);
  52. logger(UA_LOGLEVEL_TRACE, category, msg, args);
  53. va_end(args);
  54. #endif
  55. }
  56. static UA_INLINE UA_FORMAT(3,4) void
  57. UA_LOG_DEBUG(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  58. #if UA_LOGLEVEL <= 200
  59. va_list args; va_start(args, msg);
  60. logger(UA_LOGLEVEL_DEBUG, category, msg, args);
  61. va_end(args);
  62. #endif
  63. }
  64. static UA_INLINE UA_FORMAT(3,4) void
  65. UA_LOG_INFO(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  66. #if UA_LOGLEVEL <= 300
  67. va_list args; va_start(args, msg);
  68. logger(UA_LOGLEVEL_INFO, category, msg, args);
  69. va_end(args);
  70. #endif
  71. }
  72. static UA_INLINE UA_FORMAT(3,4) void
  73. UA_LOG_WARNING(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  74. #if UA_LOGLEVEL <= 400
  75. va_list args; va_start(args, msg);
  76. logger(UA_LOGLEVEL_WARNING, category, msg, args);
  77. va_end(args);
  78. #endif
  79. }
  80. static UA_INLINE UA_FORMAT(3,4) void
  81. UA_LOG_ERROR(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  82. #if UA_LOGLEVEL <= 500
  83. va_list args; va_start(args, msg);
  84. logger(UA_LOGLEVEL_ERROR, category, msg, args);
  85. va_end(args);
  86. #endif
  87. }
  88. static UA_INLINE UA_FORMAT(3,4) void
  89. UA_LOG_FATAL(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  90. #if UA_LOGLEVEL <= 600
  91. va_list args; va_start(args, msg);
  92. logger(UA_LOGLEVEL_FATAL, category, msg, args);
  93. va_end(args);
  94. #endif
  95. }
  96. _UA_END_DECLS
  97. #endif /* UA_PLUGIN_LOG_H_ */