ua_log.h 3.8 KB

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