ulog_def.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-25 armink the first version
  9. */
  10. #ifndef _ULOG_DEF_H_
  11. #define _ULOG_DEF_H_
  12. #include <rtdef.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /* logger level, the number is compatible for syslog */
  17. #define LOG_LVL_ASSERT 0
  18. #define LOG_LVL_ERROR 3
  19. #define LOG_LVL_WARNING 4
  20. #define LOG_LVL_INFO 6
  21. #define LOG_LVL_DBG 7
  22. /* the output silent level and all level for filter setting */
  23. #ifndef ULOG_USING_SYSLOG
  24. #define LOG_FILTER_LVL_SILENT 0
  25. #define LOG_FILTER_LVL_ALL 7
  26. #else
  27. #define LOG_FILTER_LVL_SILENT 1
  28. #define LOG_FILTER_LVL_ALL 255
  29. #endif /* ULOG_USING_SYSLOG */
  30. /* compatible for rtdbg */
  31. #undef LOG_D
  32. #undef LOG_I
  33. #undef LOG_W
  34. #undef LOG_E
  35. #undef LOG_RAW
  36. #undef DBG_ERROR
  37. #undef DBG_WARNING
  38. #undef DBG_INFO
  39. #undef DBG_LOG
  40. #define DBG_ERROR LOG_LVL_ERROR
  41. #define DBG_WARNING LOG_LVL_WARNING
  42. #define DBG_INFO LOG_LVL_INFO
  43. #define DBG_LOG LOG_LVL_DBG
  44. #if !defined(LOG_TAG)
  45. /* compatible for rtdbg */
  46. #if defined(DBG_TAG)
  47. #define LOG_TAG DBG_TAG
  48. #elif defined(DBG_SECTION_NAME)
  49. #define LOG_TAG DBG_SECTION_NAME
  50. #else
  51. #define LOG_TAG "NO_TAG"
  52. #endif
  53. #endif /* !defined(LOG_TAG) */
  54. #if !defined(LOG_LVL)
  55. /* compatible for rtdbg */
  56. #if defined(DBG_LVL)
  57. #define LOG_LVL DBG_LVL
  58. #elif defined(DBG_LEVEL)
  59. #define LOG_LVL DBG_LEVEL
  60. #else
  61. #define LOG_LVL LOG_LVL_DBG
  62. #endif
  63. #endif /* !defined(LOG_LVL) */
  64. #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
  65. #define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__)
  66. #else
  67. #define ulog_d(TAG, ...)
  68. #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
  69. #if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO)
  70. #define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__)
  71. #else
  72. #define ulog_i(TAG, ...)
  73. #endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */
  74. #if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING)
  75. #define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__)
  76. #else
  77. #define ulog_w(TAG, ...)
  78. #endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */
  79. #if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR)
  80. #define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__)
  81. #else
  82. #define ulog_e(TAG, ...)
  83. #endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */
  84. #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
  85. #define ulog_hex(TAG, width, buf, size) ulog_hexdump(TAG, width, buf, size)
  86. #else
  87. #define ulog_hex(TAG, width, buf, size)
  88. #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
  89. /* assert for developer. */
  90. #ifdef ULOG_ASSERT_ENABLE
  91. #define ULOG_ASSERT(EXPR) \
  92. if (!(EXPR)) \
  93. { \
  94. ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
  95. ulog_flush(); \
  96. while (1); \
  97. }
  98. #else
  99. #define ULOG_ASSERT(EXPR)
  100. #endif
  101. /* ASSERT API definition */
  102. #if !defined(ASSERT)
  103. #define ASSERT ULOG_ASSERT
  104. #endif
  105. /* compatible for elog */
  106. #undef assert
  107. #undef log_e
  108. #undef log_w
  109. #undef log_i
  110. #undef log_d
  111. #undef log_v
  112. #undef ELOG_LVL_ASSERT
  113. #undef ELOG_LVL_ERROR
  114. #undef ELOG_LVL_WARN
  115. #undef ELOG_LVL_INFO
  116. #undef ELOG_LVL_DEBUG
  117. #undef ELOG_LVL_VERBOSE
  118. #define assert ASSERT
  119. #define log_e LOG_E
  120. #define log_w LOG_W
  121. #define log_i LOG_I
  122. #define log_d LOG_D
  123. #define log_v LOG_D
  124. #define log_raw LOG_RAW
  125. #define log_hex LOG_HEX
  126. #define ELOG_LVL_ASSERT LOG_LVL_ASSERT
  127. #define ELOG_LVL_ERROR LOG_LVL_ERROR
  128. #define ELOG_LVL_WARN LOG_LVL_WARNING
  129. #define ELOG_LVL_INFO LOG_LVL_INFO
  130. #define ELOG_LVL_DEBUG LOG_LVL_DBG
  131. #define ELOG_LVL_VERBOSE LOG_LVL_DBG
  132. /* setting static output log level */
  133. #ifndef ULOG_OUTPUT_LVL
  134. #define ULOG_OUTPUT_LVL LOG_LVL_DBG
  135. #endif
  136. /* buffer size for every line's log */
  137. #ifndef ULOG_LINE_BUF_SIZE
  138. #define ULOG_LINE_BUF_SIZE 128
  139. #endif
  140. /* output filter's tag max length */
  141. #ifndef ULOG_FILTER_TAG_MAX_LEN
  142. #define ULOG_FILTER_TAG_MAX_LEN 23
  143. #endif
  144. /* output filter's keyword max length */
  145. #ifndef ULOG_FILTER_KW_MAX_LEN
  146. #define ULOG_FILTER_KW_MAX_LEN 15
  147. #endif
  148. #ifndef ULOG_NEWLINE_SIGN
  149. #define ULOG_NEWLINE_SIGN "\r\n"
  150. #endif
  151. #define ULOG_FRAME_MAGIC 0x10
  152. /* tag's level filter */
  153. struct ulog_tag_lvl_filter
  154. {
  155. char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
  156. rt_uint32_t level;
  157. rt_slist_t list;
  158. };
  159. typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t;
  160. struct ulog_frame
  161. {
  162. /* magic word is 0x10 ('lo') */
  163. rt_uint32_t magic:8;
  164. rt_uint32_t is_raw:1;
  165. rt_uint32_t log_len:23;
  166. rt_uint32_t level;
  167. const char *log;
  168. const char *tag;
  169. };
  170. typedef struct ulog_frame *ulog_frame_t;
  171. struct ulog_backend
  172. {
  173. char name[RT_NAME_MAX];
  174. rt_bool_t support_color;
  175. rt_uint32_t out_level;
  176. void (*init) (struct ulog_backend *backend);
  177. void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
  178. void (*flush) (struct ulog_backend *backend);
  179. void (*deinit)(struct ulog_backend *backend);
  180. /* The filter will be call before output. It will return TRUE when the filter condition is math. */
  181. rt_bool_t (*filter)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
  182. rt_slist_t list;
  183. };
  184. typedef struct ulog_backend *ulog_backend_t;
  185. typedef rt_bool_t (*ulog_backend_filter_t)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189. #endif /* _ULOG_DEF_H_ */