ua_log.h 4.3 KB

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