ulog_def.h 6.4 KB

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