ua_log.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef UA_LOG_H_
  5. #define UA_LOG_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <stdarg.h>
  10. #include "ua_config.h"
  11. /**
  12. * Logging
  13. * -------
  14. *
  15. * Servers and clients may contain a logger. Every logger needs to implement the
  16. * `UA_Logger` signature. An example logger that writes to stdout is provided in
  17. * the plugins folder.
  18. *
  19. * Every log-message consists of a log-level, a log-category and a string
  20. * message content. The timestamp of the log-message is created within the
  21. * logger.
  22. */
  23. typedef enum {
  24. UA_LOGLEVEL_TRACE,
  25. UA_LOGLEVEL_DEBUG,
  26. UA_LOGLEVEL_INFO,
  27. UA_LOGLEVEL_WARNING,
  28. UA_LOGLEVEL_ERROR,
  29. UA_LOGLEVEL_FATAL
  30. } UA_LogLevel;
  31. typedef enum {
  32. UA_LOGCATEGORY_NETWORK,
  33. UA_LOGCATEGORY_SECURECHANNEL,
  34. UA_LOGCATEGORY_SESSION,
  35. UA_LOGCATEGORY_SERVER,
  36. UA_LOGCATEGORY_CLIENT,
  37. UA_LOGCATEGORY_USERLAND
  38. } UA_LogCategory;
  39. /**
  40. * The signature of the logger. The msg string and following varargs are
  41. * formatted according to the rules of the printf command.
  42. *
  43. * Do not use the logger directly but make use of the following macros that take
  44. * the minimum log-level defined in ua_config.h into account. */
  45. typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category,
  46. const char *msg, va_list args);
  47. static UA_INLINE void
  48. UA_LOG_TRACE(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  49. #if UA_LOGLEVEL <= 100
  50. if(logger) {
  51. va_list args; va_start(args, msg);
  52. logger(UA_LOGLEVEL_TRACE, category, msg, args);
  53. va_end(args);
  54. }
  55. #endif
  56. }
  57. static UA_INLINE void
  58. UA_LOG_DEBUG(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  59. #if UA_LOGLEVEL <= 200
  60. if(logger) {
  61. va_list args; va_start(args, msg);
  62. logger(UA_LOGLEVEL_DEBUG, category, msg, args);
  63. va_end(args);
  64. }
  65. #endif
  66. }
  67. static UA_INLINE void
  68. UA_LOG_INFO(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  69. #if UA_LOGLEVEL <= 300
  70. if(logger) {
  71. va_list args; va_start(args, msg);
  72. logger(UA_LOGLEVEL_INFO, category, msg, args);
  73. va_end(args);
  74. }
  75. #endif
  76. }
  77. static UA_INLINE void
  78. UA_LOG_WARNING(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  79. #if UA_LOGLEVEL <= 400
  80. if(logger) {
  81. va_list args; va_start(args, msg);
  82. logger(UA_LOGLEVEL_WARNING, category, msg, args);
  83. va_end(args);
  84. }
  85. #endif
  86. }
  87. static UA_INLINE void
  88. UA_LOG_ERROR(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  89. #if UA_LOGLEVEL <= 500
  90. if(logger) {
  91. va_list args; va_start(args, msg);
  92. logger(UA_LOGLEVEL_ERROR, category, msg, args);
  93. va_end(args);
  94. }
  95. #endif
  96. }
  97. static UA_INLINE void
  98. UA_LOG_FATAL(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  99. #if UA_LOGLEVEL <= 600
  100. if(logger) {
  101. va_list args; va_start(args, msg);
  102. logger(UA_LOGLEVEL_FATAL, category, msg, args);
  103. va_end(args);
  104. }
  105. #endif
  106. }
  107. /**
  108. * Convenience macros for complex types
  109. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  110. #define UA_PRINTF_GUID_FORMAT "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"
  111. #define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
  112. (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
  113. (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
  114. #define UA_PRINTF_STRING_FORMAT "\"%.*s\""
  115. #define UA_PRINTF_STRING_DATA(STRING) (STRING).length, (STRING).data
  116. #ifdef __cplusplus
  117. } // extern "C"
  118. #endif
  119. #endif /* UA_LOG_H_ */