ua_log.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2014-2016 the contributors as stated in the AUTHORS file
  3. *
  4. * This file is part of open62541. open62541 is free software: you can
  5. * redistribute it and/or modify it under the terms of the GNU Lesser General
  6. * Public License, version 3 (as published by the Free Software Foundation) with
  7. * a static linking exception as stated in the LICENSE file provided with
  8. * open62541.
  9. *
  10. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. * details.
  14. */
  15. #ifndef UA_LOG_H_
  16. #define UA_LOG_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <stdarg.h>
  21. #include "ua_config.h"
  22. /**
  23. * Logging
  24. * -------
  25. *
  26. * Servers and clients may contain a logger. Every logger needs to implement the
  27. * `UA_Logger` signature. An example logger that writes to stdout is provided in
  28. * the plugins folder.
  29. *
  30. * Every log-message consists of a log-level, a log-category and a string
  31. * message content. The timestamp of the log-message is created within the
  32. * logger.
  33. */
  34. typedef enum {
  35. UA_LOGLEVEL_TRACE,
  36. UA_LOGLEVEL_DEBUG,
  37. UA_LOGLEVEL_INFO,
  38. UA_LOGLEVEL_WARNING,
  39. UA_LOGLEVEL_ERROR,
  40. UA_LOGLEVEL_FATAL
  41. } UA_LogLevel;
  42. typedef enum {
  43. UA_LOGCATEGORY_NETWORK,
  44. UA_LOGCATEGORY_SECURECHANNEL,
  45. UA_LOGCATEGORY_SESSION,
  46. UA_LOGCATEGORY_SERVER,
  47. UA_LOGCATEGORY_CLIENT,
  48. UA_LOGCATEGORY_USERLAND
  49. } UA_LogCategory;
  50. /**
  51. * The signature of the logger. The msg string and following varargs are
  52. * formatted according to the rules of the printf command.
  53. *
  54. * Do not use the logger directly but make use of the following macros that take
  55. * the minimum log-level defined in ua_config.h into account. */
  56. typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category,
  57. const char *msg, ...);
  58. static inline void
  59. UA_LOG_TRACE(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  60. #if UA_LOGLEVEL <= 100
  61. if(logger) {
  62. va_list args; va_start(args, msg);
  63. logger(UA_LOGLEVEL_TRACE, category, msg, args);
  64. va_end(args);
  65. }
  66. #endif
  67. }
  68. static inline void
  69. UA_LOG_DEBUG(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  70. #if UA_LOGLEVEL <= 200
  71. if(logger) {
  72. va_list args; va_start(args, msg);
  73. logger(UA_LOGLEVEL_DEBUG, category, msg, args);
  74. va_end(args);
  75. }
  76. #endif
  77. }
  78. static inline void
  79. UA_LOG_INFO(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  80. #if UA_LOGLEVEL <= 300
  81. if(logger) {
  82. va_list args; va_start(args, msg);
  83. logger(UA_LOGLEVEL_INFO, category, msg, args);
  84. va_end(args);
  85. }
  86. #endif
  87. }
  88. static inline void
  89. UA_LOG_WARNING(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  90. #if UA_LOGLEVEL <= 400
  91. if(logger) {
  92. va_list args; va_start(args, msg);
  93. logger(UA_LOGLEVEL_WARNING, category, msg, args);
  94. va_end(args);
  95. }
  96. #endif
  97. }
  98. static inline void
  99. UA_LOG_ERROR(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  100. #if UA_LOGLEVEL <= 500
  101. if(logger) {
  102. va_list args; va_start(args, msg);
  103. logger(UA_LOGLEVEL_ERROR, category, msg, args);
  104. va_end(args);
  105. }
  106. #endif
  107. }
  108. static inline void
  109. UA_LOG_FATAL(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  110. #if UA_LOGLEVEL <= 600
  111. if(logger) {
  112. va_list args; va_start(args, msg);
  113. logger(UA_LOGLEVEL_FATAL, category, msg, args);
  114. va_end(args);
  115. }
  116. #endif
  117. }
  118. /**
  119. * Convenience macros for complex types
  120. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  121. #define UA_PRINTF_GUID_FORMAT "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"
  122. #define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
  123. (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
  124. (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
  125. #define UA_PRINTF_STRING_FORMAT "\"%.*s\""
  126. #define UA_PRINTF_STRING_DATA(STRING) (STRING).length, (STRING).data
  127. #ifdef __cplusplus
  128. } // extern "C"
  129. #endif
  130. #endif /* UA_LOG_H_ */