协能can协议
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

elog.h 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * This file is part of the EasyLogger Library.
  3. *
  4. * Copyright (c) 2015-2019, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: It is an head file for this library. You can see all be called functions.
  26. * Created on: 2015-04-28
  27. */
  28. #ifndef __ELOG_H__
  29. #define __ELOG_H__
  30. #include <elog_cfg.h>
  31. #include <stdint.h>
  32. #include <stddef.h>
  33. #include <stdbool.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /* output log's level */
  38. #define ELOG_LVL_ASSERT 0
  39. #define ELOG_LVL_ERROR 1
  40. #define ELOG_LVL_WARN 2
  41. #define ELOG_LVL_INFO 3
  42. #define ELOG_LVL_DEBUG 4
  43. #define ELOG_LVL_VERBOSE 5
  44. /* the output silent level and all level for filter setting */
  45. #define ELOG_FILTER_LVL_SILENT ELOG_LVL_ASSERT
  46. #define ELOG_FILTER_LVL_ALL ELOG_LVL_VERBOSE
  47. /* output log's level total number */
  48. #define ELOG_LVL_TOTAL_NUM 6
  49. /* EasyLogger software version number */
  50. #define ELOG_SW_VERSION "2.2.99"
  51. /* EasyLogger assert for developer. */
  52. #ifdef ELOG_ASSERT_ENABLE
  53. #define ELOG_ASSERT(EXPR) \
  54. if (!(EXPR)) \
  55. { \
  56. if (elog_assert_hook == NULL) { \
  57. elog_a("elog", "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
  58. while (1); \
  59. } else { \
  60. elog_assert_hook(#EXPR, __FUNCTION__, __LINE__); \
  61. } \
  62. }
  63. #else
  64. #define ELOG_ASSERT(EXPR) ((void)0);
  65. #endif
  66. #ifndef ELOG_OUTPUT_ENABLE
  67. #define elog_assert(tag, ...)
  68. #define elog_error(tag, ...)
  69. #define elog_warn(tag, ...)
  70. #define elog_info(tag, ...)
  71. #define elog_debug(tag, ...)
  72. #define elog_verbose(tag, ...)
  73. #else /* ELOG_OUTPUT_ENABLE */
  74. #if ELOG_OUTPUT_LVL >= ELOG_LVL_ASSERT
  75. #define elog_assert(tag, ...) \
  76. elog_output(ELOG_LVL_ASSERT, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  77. #else
  78. #define elog_assert(tag, ...)
  79. #endif /* ELOG_OUTPUT_LVL >= ELOG_LVL_ASSERT */
  80. #if ELOG_OUTPUT_LVL >= ELOG_LVL_ERROR
  81. #define elog_error(tag, ...) \
  82. elog_output(ELOG_LVL_ERROR, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  83. #else
  84. #define elog_error(tag, ...)
  85. #endif /* ELOG_OUTPUT_LVL >= ELOG_LVL_ERROR */
  86. #if ELOG_OUTPUT_LVL >= ELOG_LVL_WARN
  87. #define elog_warn(tag, ...) \
  88. elog_output(ELOG_LVL_WARN, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  89. #else
  90. #define elog_warn(tag, ...)
  91. #endif /* ELOG_OUTPUT_LVL >= ELOG_LVL_WARN */
  92. #if ELOG_OUTPUT_LVL >= ELOG_LVL_INFO
  93. #define elog_info(tag, ...) \
  94. elog_output(ELOG_LVL_INFO, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  95. #else
  96. #define elog_info(tag, ...)
  97. #endif /* ELOG_OUTPUT_LVL >= ELOG_LVL_INFO */
  98. #if ELOG_OUTPUT_LVL >= ELOG_LVL_DEBUG
  99. #define elog_debug(tag, ...) \
  100. elog_output(ELOG_LVL_DEBUG, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  101. #else
  102. #define elog_debug(tag, ...)
  103. #endif /* ELOG_OUTPUT_LVL >= ELOG_LVL_DEBUG */
  104. #if ELOG_OUTPUT_LVL == ELOG_LVL_VERBOSE
  105. #define elog_verbose(tag, ...) \
  106. elog_output(ELOG_LVL_VERBOSE, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  107. #else
  108. #define elog_verbose(tag, ...)
  109. #endif /* ELOG_OUTPUT_LVL == ELOG_LVL_VERBOSE */
  110. #endif /* ELOG_OUTPUT_ENABLE */
  111. /* all formats index */
  112. typedef enum {
  113. ELOG_FMT_LVL = 1 << 0, /**< level */
  114. ELOG_FMT_TAG = 1 << 1, /**< tag */
  115. ELOG_FMT_TIME = 1 << 2, /**< current time */
  116. ELOG_FMT_P_INFO = 1 << 3, /**< process info */
  117. ELOG_FMT_T_INFO = 1 << 4, /**< thread info */
  118. ELOG_FMT_DIR = 1 << 5, /**< file directory and name */
  119. ELOG_FMT_FUNC = 1 << 6, /**< function name */
  120. ELOG_FMT_LINE = 1 << 7, /**< line number */
  121. } ElogFmtIndex;
  122. /* macro definition for all formats */
  123. #define ELOG_FMT_ALL (ELOG_FMT_LVL|ELOG_FMT_TAG|ELOG_FMT_TIME|ELOG_FMT_P_INFO|ELOG_FMT_T_INFO| \
  124. ELOG_FMT_DIR|ELOG_FMT_FUNC|ELOG_FMT_LINE)
  125. /* output log's tag filter */
  126. typedef struct {
  127. uint8_t level;
  128. char tag[ELOG_FILTER_TAG_MAX_LEN + 1];
  129. bool tag_use_flag; /**< false : tag is no used true: tag is used */
  130. } ElogTagLvlFilter, *ElogTagLvlFilter_t;
  131. /* output log's filter */
  132. typedef struct {
  133. uint8_t level;
  134. char tag[ELOG_FILTER_TAG_MAX_LEN + 1];
  135. char keyword[ELOG_FILTER_KW_MAX_LEN + 1];
  136. ElogTagLvlFilter tag_lvl[ELOG_FILTER_TAG_LVL_MAX_NUM];
  137. } ElogFilter, *ElogFilter_t;
  138. /* easy logger */
  139. typedef struct {
  140. ElogFilter filter;
  141. size_t enabled_fmt_set[ELOG_LVL_TOTAL_NUM];
  142. bool init_ok;
  143. bool output_enabled;
  144. bool output_lock_enabled;
  145. bool output_is_locked_before_enable;
  146. bool output_is_locked_before_disable;
  147. #ifdef ELOG_COLOR_ENABLE
  148. bool text_color_enabled;
  149. #endif
  150. }EasyLogger, *EasyLogger_t;
  151. /* EasyLogger error code */
  152. typedef enum {
  153. ELOG_NO_ERR,
  154. } ElogErrCode;
  155. /* elog.c */
  156. ElogErrCode elog_init(void);
  157. void elog_start(void);
  158. void elog_set_output_enabled(bool enabled);
  159. bool elog_get_output_enabled(void);
  160. void elog_set_text_color_enabled(bool enabled);
  161. bool elog_get_text_color_enabled(void);
  162. void elog_set_fmt(uint8_t level, size_t set);
  163. void elog_set_filter(uint8_t level, const char *tag, const char *keyword);
  164. void elog_set_filter_lvl(uint8_t level);
  165. void elog_set_filter_tag(const char *tag);
  166. void elog_set_filter_kw(const char *keyword);
  167. void elog_set_filter_tag_lvl(const char *tag, uint8_t level);
  168. uint8_t elog_get_filter_tag_lvl(const char *tag);
  169. void elog_raw(const char *format, ...);
  170. void elog_output(uint8_t level, const char *tag, const char *file, const char *func,
  171. const long line, const char *format, ...);
  172. void elog_output_lock_enabled(bool enabled);
  173. extern void (*elog_assert_hook)(const char* expr, const char* func, size_t line);
  174. void elog_assert_set_hook(void (*hook)(const char* expr, const char* func, size_t line));
  175. int8_t elog_find_lvl(const char *log);
  176. const char *elog_find_tag(const char *log, uint8_t lvl, size_t *tag_len);
  177. void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size);
  178. #define elog_a(tag, ...) elog_assert(tag, __VA_ARGS__)
  179. #define elog_e(tag, ...) elog_error(tag, __VA_ARGS__)
  180. #define elog_w(tag, ...) elog_warn(tag, __VA_ARGS__)
  181. #define elog_i(tag, ...) elog_info(tag, __VA_ARGS__)
  182. #define elog_d(tag, ...) elog_debug(tag, __VA_ARGS__)
  183. #define elog_v(tag, ...) elog_verbose(tag, __VA_ARGS__)
  184. /**
  185. * log API short definition
  186. * NOTE: The `LOG_TAG` and `LOG_LVL` must defined before including the <elog.h> when you want to use log_x API.
  187. */
  188. #if !defined(LOG_TAG)
  189. #define LOG_TAG "NO_TAG"
  190. #endif
  191. #if !defined(LOG_LVL)
  192. #define LOG_LVL ELOG_LVL_VERBOSE
  193. #endif
  194. #if LOG_LVL >= ELOG_LVL_ASSERT
  195. #define log_a(...) elog_a(LOG_TAG, __VA_ARGS__)
  196. #else
  197. #define log_a(...) ((void)0);
  198. #endif
  199. #if LOG_LVL >= ELOG_LVL_ERROR
  200. #define log_e(...) elog_e(LOG_TAG, __VA_ARGS__)
  201. #else
  202. #define log_e(...) ((void)0);
  203. #endif
  204. #if LOG_LVL >= ELOG_LVL_WARN
  205. #define log_w(...) elog_w(LOG_TAG, __VA_ARGS__)
  206. #else
  207. #define log_w(...) ((void)0);
  208. #endif
  209. #if LOG_LVL >= ELOG_LVL_INFO
  210. #define log_i(...) elog_i(LOG_TAG, __VA_ARGS__)
  211. #else
  212. #define log_i(...) ((void)0);
  213. #endif
  214. #if LOG_LVL >= ELOG_LVL_DEBUG
  215. #define log_d(...) elog_d(LOG_TAG, __VA_ARGS__)
  216. #else
  217. #define log_d(...) ((void)0);
  218. #endif
  219. #if LOG_LVL >= ELOG_LVL_VERBOSE
  220. #define log_v(...) elog_v(LOG_TAG, __VA_ARGS__)
  221. #else
  222. #define log_v(...) ((void)0);
  223. #endif
  224. /* assert API short definition */
  225. #if !defined(assert)
  226. #define assert ELOG_ASSERT
  227. #endif
  228. /* elog_buf.c */
  229. void elog_buf_enabled(bool enabled);
  230. void elog_flush(void);
  231. /* elog_async.c */
  232. void elog_async_enabled(bool enabled);
  233. size_t elog_async_get_log(char *log, size_t size);
  234. size_t elog_async_get_line_log(char *log, size_t size);
  235. /* elog_utils.c */
  236. size_t elog_strcpy(size_t cur_len, char *dst, const char *src);
  237. size_t elog_cpyln(char *line, const char *log, size_t len);
  238. void *elog_memcpy(void *dst, const void *src, size_t count);
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif /* __ELOG_H__ */