ulog_def.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef _ULOG_DEF_H_
  2. #define _ULOG_DEF_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* logger level, the number is compatible for syslog */
  7. #define LOG_LVL_ASSERT 0
  8. #define LOG_LVL_FATAL 1
  9. #define LOG_LVL_ERROR 3
  10. #define LOG_LVL_WARNING 4
  11. #define LOG_LVL_INFO 6
  12. #define LOG_LVL_DBG 7
  13. /* the output silent level and all level for filter setting */
  14. #ifndef ULOG_USING_SYSLOG
  15. #define LOG_FILTER_LVL_SILENT 0
  16. #define LOG_FILTER_LVL_ALL 7
  17. #else
  18. #define LOG_FILTER_LVL_SILENT 1
  19. #define LOG_FILTER_LVL_ALL 255
  20. #endif /* ULOG_USING_SYSLOG */
  21. /* compatible for trace */
  22. #undef TRACE_DEBUG
  23. #undef TRACE_INFO
  24. #undef TRACE_WARNING
  25. #undef TRACE_ERROR
  26. #undef TRACE_FATAL
  27. #undef TRACE_LEVEL_DEBUG
  28. #undef TRACE_LEVEL_INFO
  29. #undef TRACE_LEVEL_WARNING
  30. #undef TRACE_LEVEL_ERROR
  31. #undef TRACE_LEVEL_FATAL
  32. #define TRACE_LEVEL_FATAL LOG_LVL_FATAL
  33. #define TRACE_LEVEL_ERROR LOG_LVL_ERROR
  34. #define TRACE_LEVEL_WARNING LOG_LVL_WARNING
  35. #define TRACE_LEVEL_INFO LOG_LVL_INFO
  36. #define TRACE_LEVEL_DEBUG LOG_LVL_DBG
  37. #define dbg_log(level, ...) \
  38. if ((level) <= LOG_LVL) \
  39. { \
  40. ulog_output(level, LOG_TAG, pdFALSE, __VA_ARGS__);\
  41. }
  42. #if !defined(LOG_TAG)
  43. /* compatible for trace */
  44. #if defined(TRACE_TAG)
  45. #define LOG_TAG TRACE_TAG
  46. #else
  47. #define LOG_TAG "NO_TAG"
  48. #endif
  49. #endif /* !defined(LOG_TAG) */
  50. #if !defined(TRACE_LEVEL)
  51. #define TRACE_LEVEL TRACE_LEVEL_INFO
  52. #endif
  53. /* setting static output log level */
  54. #ifndef ULOG_OUTPUT_LVL
  55. #define ULOG_OUTPUT_LVL LOG_LVL_INFO
  56. #endif
  57. #if !defined(LOG_LVL)
  58. /* compatible for trace */
  59. #if defined(TRACE_LEVEL)
  60. #define LOG_LVL TRACE_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, pdTRUE, __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, pdTRUE, __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, pdTRUE, __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, pdTRUE, __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_FATAL) && (ULOG_OUTPUT_LVL >= LOG_LVL_FATAL)
  86. #define ulog_f(TAG, ...) {ulog_output(LOG_LVL_FATAL, TAG, pdTRUE, __VA_ARGS__); while(1);}
  87. #else
  88. #define ulog_f(TAG, ...)
  89. #endif /* (LOG_LVL >= LOG_LVL_FATAL) && (ULOG_OUTPUT_LVL >= LOG_LVL_FATAL) */
  90. #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
  91. #define ulog_hex(TAG, width, buf, size) ulog_hexdump(TAG, width, buf, size)
  92. #else
  93. #define ulog_hex(TAG, width, buf, size)
  94. #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
  95. /* assert for developer. */
  96. #ifdef ULOG_ASSERT_ENABLE
  97. #define ULOG_ASSERT(EXPR) \
  98. if (!(EXPR)) \
  99. { \
  100. ulog_output(LOG_LVL_ASSERT, LOG_TAG, pdTRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
  101. ulog_flush(); \
  102. while (1); \
  103. }
  104. #else
  105. #define ULOG_ASSERT(EXPR)
  106. #endif
  107. /* ASSERT API definition */
  108. #if !defined(ASSERT)
  109. #define ASSERT ULOG_ASSERT
  110. #endif
  111. /* buffer size for every line's log */
  112. #ifndef ULOG_LINE_BUF_SIZE
  113. #define ULOG_LINE_BUF_SIZE 128
  114. #endif
  115. /* output filter's tag max length */
  116. #ifndef ULOG_FILTER_TAG_MAX_LEN
  117. #define ULOG_FILTER_TAG_MAX_LEN 23
  118. #endif
  119. /* output filter's keyword max length */
  120. #ifndef ULOG_FILTER_KW_MAX_LEN
  121. #define ULOG_FILTER_KW_MAX_LEN 15
  122. #endif
  123. #ifndef ULOG_NEWLINE_SIGN
  124. #define ULOG_NEWLINE_SIGN "\r\n"
  125. #endif
  126. #define ULOG_FRAME_MAGIC 0x10
  127. /* tag's level filter */
  128. struct ulog_tag_lvl_filter
  129. {
  130. char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
  131. uint32_t level;
  132. ListItem_t list;
  133. };
  134. typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t;
  135. struct ulog_frame
  136. {
  137. /* magic word is 0x10 ('lo') */
  138. uint32_t magic:8;
  139. uint32_t is_raw:1;
  140. uint32_t log_len:23;
  141. uint32_t level;
  142. const char *log;
  143. const char *tag;
  144. };
  145. typedef struct ulog_frame *ulog_frame_t;
  146. struct ulog_backend
  147. {
  148. char name[ULOG_NAME_MAX];
  149. int support_color;
  150. void (*init) (struct ulog_backend *backend);
  151. void (*output)(struct ulog_backend *backend, uint32_t level, const char *tag, int is_raw, const char *log, size_t len);
  152. void (*flush) (struct ulog_backend *backend);
  153. void (*deinit)(struct ulog_backend *backend);
  154. ListItem_t list;
  155. };
  156. typedef struct ulog_backend *ulog_backend_t;
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif /* _ULOG_DEF_H_ */