ua_log.h 3.8 KB

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