ua_plugin_log.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. */
  8. #ifndef UA_PLUGIN_LOG_H_
  9. #define UA_PLUGIN_LOG_H_
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #include <stdarg.h>
  14. #include "ua_config.h"
  15. #include "ua_types.h"
  16. #include "ua_types_generated_handling.h"
  17. /**
  18. * Logging Plugin API
  19. * ==================
  20. *
  21. * Servers and clients must define a logger in their configuration. The logger
  22. * is just a function pointer. Every log-message consists of a log-level, a
  23. * log-category and a string message content. The timestamp of the log-message
  24. * is created within the logger. */
  25. typedef enum {
  26. UA_LOGLEVEL_TRACE,
  27. UA_LOGLEVEL_DEBUG,
  28. UA_LOGLEVEL_INFO,
  29. UA_LOGLEVEL_WARNING,
  30. UA_LOGLEVEL_ERROR,
  31. UA_LOGLEVEL_FATAL
  32. } UA_LogLevel;
  33. typedef enum {
  34. UA_LOGCATEGORY_NETWORK,
  35. UA_LOGCATEGORY_SECURECHANNEL,
  36. UA_LOGCATEGORY_SESSION,
  37. UA_LOGCATEGORY_SERVER,
  38. UA_LOGCATEGORY_CLIENT,
  39. UA_LOGCATEGORY_USERLAND,
  40. UA_LOGCATEGORY_SECURITYPOLICY
  41. } UA_LogCategory;
  42. /**
  43. * The message string and following varargs are formatted according to the rules
  44. * of the printf command. Do not call the logger directly. Instead, make use of
  45. * the convenience macros that take the minimum log-level defined in ua_config.h
  46. * into account. */
  47. typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category,
  48. const char *msg, va_list args);
  49. static UA_INLINE UA_FORMAT(3,4) void
  50. UA_LOG_TRACE(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  51. #if UA_LOGLEVEL <= 100
  52. va_list args; va_start(args, msg);
  53. logger(UA_LOGLEVEL_TRACE, category, msg, args);
  54. va_end(args);
  55. #endif
  56. }
  57. static UA_INLINE UA_FORMAT(3,4) void
  58. UA_LOG_DEBUG(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  59. #if UA_LOGLEVEL <= 200
  60. va_list args; va_start(args, msg);
  61. logger(UA_LOGLEVEL_DEBUG, category, msg, args);
  62. va_end(args);
  63. #endif
  64. }
  65. static UA_INLINE UA_FORMAT(3,4) void
  66. UA_LOG_INFO(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  67. #if UA_LOGLEVEL <= 300
  68. va_list args; va_start(args, msg);
  69. logger(UA_LOGLEVEL_INFO, category, msg, args);
  70. va_end(args);
  71. #endif
  72. }
  73. static UA_INLINE UA_FORMAT(3,4) void
  74. UA_LOG_WARNING(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  75. #if UA_LOGLEVEL <= 400
  76. va_list args; va_start(args, msg);
  77. logger(UA_LOGLEVEL_WARNING, category, msg, args);
  78. va_end(args);
  79. #endif
  80. }
  81. static UA_INLINE UA_FORMAT(3,4) void
  82. UA_LOG_ERROR(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  83. #if UA_LOGLEVEL <= 500
  84. va_list args; va_start(args, msg);
  85. logger(UA_LOGLEVEL_ERROR, category, msg, args);
  86. va_end(args);
  87. #endif
  88. }
  89. static UA_INLINE UA_FORMAT(3,4) void
  90. UA_LOG_FATAL(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {
  91. #if UA_LOGLEVEL <= 600
  92. va_list args; va_start(args, msg);
  93. logger(UA_LOGLEVEL_FATAL, category, msg, args);
  94. va_end(args);
  95. #endif
  96. }
  97. /**
  98. * Convenience macros for complex types
  99. * ------------------------------------ */
  100. #define UA_PRINTF_GUID_FORMAT "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"
  101. #define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
  102. (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
  103. (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
  104. #define UA_PRINTF_STRING_FORMAT "\"%.*s\""
  105. #define UA_PRINTF_STRING_DATA(STRING) (int)(STRING).length, (STRING).data
  106. //TODO remove when we merge architectures pull request
  107. #ifndef UA_snprintf
  108. # include <stdio.h>
  109. # if defined(_WIN32)
  110. # define UA_snprintf(source, size, string, ...) _snprintf_s(source, size, _TRUNCATE, string, __VA_ARGS__)
  111. # else
  112. # define UA_snprintf snprintf
  113. # endif
  114. #endif
  115. static UA_INLINE UA_StatusCode
  116. UA_ByteString_toString(const UA_ByteString *byteString, UA_String *str) {
  117. if (str->length != 0) {
  118. UA_free(str->data);
  119. str->data = NULL;
  120. str->length = 0;
  121. }
  122. if (byteString == NULL || byteString->data == NULL)
  123. return UA_STATUSCODE_GOOD;
  124. if (byteString == str)
  125. return UA_STATUSCODE_BADINVALIDARGUMENT;
  126. str->length = byteString->length*2;
  127. str->data = (UA_Byte*)UA_malloc(str->length+1);
  128. if (str->data == NULL)
  129. return UA_STATUSCODE_BADOUTOFMEMORY;
  130. for (size_t i=0; i<byteString->length; i++)
  131. UA_snprintf((char*)&str->data[i*2], 2+1, "%02x", byteString->data[i]);
  132. return UA_STATUSCODE_GOOD;
  133. }
  134. static UA_INLINE UA_StatusCode
  135. UA_NodeId_toString(const UA_NodeId *nodeId, UA_String *nodeIdStr) {
  136. if (nodeIdStr->length != 0) {
  137. UA_free(nodeIdStr->data);
  138. nodeIdStr->data = NULL;
  139. nodeIdStr->length = 0;
  140. }
  141. if (nodeId == NULL)
  142. return UA_STATUSCODE_GOOD;
  143. UA_ByteString byteStr = UA_BYTESTRING_NULL;
  144. switch (nodeId->identifierType) {
  145. /* for all the lengths below we add the constant for: */
  146. /* strlen("ns=XXXXXX;i=")=11 */
  147. case UA_NODEIDTYPE_NUMERIC:
  148. /* ns (2 byte, 65535) = 5 chars, numeric (4 byte, 4294967295) = 10 chars, delim = 1 , nullbyte = 1-> 17 chars */
  149. nodeIdStr->length = 11 + 10 + 1;
  150. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  151. if (nodeIdStr->data == NULL)
  152. return UA_STATUSCODE_BADOUTOFMEMORY;
  153. UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "ns=%d;i=%lu",
  154. nodeId->namespaceIndex, (unsigned long )nodeId->identifier.numeric);
  155. break;
  156. case UA_NODEIDTYPE_STRING:
  157. /* ns (16bit) = 5 chars, strlen + nullbyte */
  158. nodeIdStr->length = 11 + nodeId->identifier.string.length + 1;
  159. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  160. if (nodeIdStr->data == NULL)
  161. return UA_STATUSCODE_BADOUTOFMEMORY;
  162. UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "ns=%d;i=%.*s", nodeId->namespaceIndex,
  163. (int)nodeId->identifier.string.length, nodeId->identifier.string.data);
  164. break;
  165. case UA_NODEIDTYPE_GUID:
  166. /* ns (16bit) = 5 chars + strlen(A123456C-0ABC-1A2B-815F-687212AAEE1B)=36 + nullbyte */
  167. nodeIdStr->length = 11 + 36 + 1;
  168. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  169. if (nodeIdStr->data == NULL)
  170. return UA_STATUSCODE_BADOUTOFMEMORY;
  171. UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "ns=%d;i=" UA_PRINTF_GUID_FORMAT,
  172. nodeId->namespaceIndex, UA_PRINTF_GUID_DATA(nodeId->identifier.guid));
  173. break;
  174. case UA_NODEIDTYPE_BYTESTRING:
  175. UA_ByteString_toString(&nodeId->identifier.byteString, &byteStr);
  176. /* ns (16bit) = 5 chars + LEN + nullbyte */
  177. nodeIdStr->length = 11 + byteStr.length + 1;
  178. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  179. if (nodeIdStr->data == NULL)
  180. return UA_STATUSCODE_BADOUTOFMEMORY;
  181. UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "ns=%d;i=%.*s", nodeId->namespaceIndex, (int)byteStr.length, byteStr.data);
  182. UA_String_deleteMembers(&byteStr);
  183. break;
  184. }
  185. return UA_STATUSCODE_GOOD;
  186. }
  187. #ifdef __cplusplus
  188. } // extern "C"
  189. #endif
  190. #endif /* UA_PLUGIN_LOG_H_ */