ulog_def.h 6.5 KB

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