1
0

ulog_def.h 6.6 KB

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