ua_log.h 4.0 KB

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