elog.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /* EasyLogger software version number */
  38. #define ELOG_SW_VERSION "2.2.99"
  39. /* output log's level */
  40. #define ELOG_LVL_ASSERT 0
  41. #define ELOG_LVL_ERROR 1
  42. #define ELOG_LVL_WARN 2
  43. #define ELOG_LVL_INFO 3
  44. #define ELOG_LVL_DEBUG 4
  45. #define ELOG_LVL_VERBOSE 5
  46. /* the output silent level and all level for filter setting */
  47. #define ELOG_FILTER_LVL_SILENT ELOG_LVL_ASSERT
  48. #define ELOG_FILTER_LVL_ALL ELOG_LVL_VERBOSE
  49. /* output log's level total number */
  50. #define ELOG_LVL_TOTAL_NUM 6
  51. /* all formats index */
  52. typedef enum {
  53. ELOG_FMT_LVL = 1 << 0, /**< level */
  54. ELOG_FMT_TAG = 1 << 1, /**< tag */
  55. ELOG_FMT_TIME = 1 << 2, /**< current time */
  56. ELOG_FMT_P_INFO = 1 << 3, /**< process info */
  57. ELOG_FMT_T_INFO = 1 << 4, /**< thread info */
  58. ELOG_FMT_DIR = 1 << 5, /**< file directory and name */
  59. ELOG_FMT_FUNC = 1 << 6, /**< function name */
  60. ELOG_FMT_LINE = 1 << 7, /**< line number */
  61. } ElogFmtIndex;
  62. /* macro definition for all formats */
  63. #define ELOG_FMT_ALL (ELOG_FMT_LVL|ELOG_FMT_TAG|ELOG_FMT_TIME|ELOG_FMT_P_INFO|ELOG_FMT_T_INFO|ELOG_FMT_DIR|ELOG_FMT_FUNC|ELOG_FMT_LINE)
  64. /* output log's tag filter */
  65. typedef struct {
  66. uint8_t level;
  67. char tag[ELOG_FILTER_TAG_MAX_LEN + 1];
  68. bool tag_use_flag; /**< false : tag is no used true: tag is used */
  69. } ElogTagLvlFilter, *ElogTagLvlFilter_t;
  70. /* output log's filter */
  71. typedef struct {
  72. uint8_t level;
  73. char tag[ELOG_FILTER_TAG_MAX_LEN + 1];
  74. char keyword[ELOG_FILTER_KW_MAX_LEN + 1];
  75. ElogTagLvlFilter tag_lvl[ELOG_FILTER_TAG_LVL_MAX_NUM];
  76. } ElogFilter, *ElogFilter_t;
  77. /* easy logger */
  78. typedef struct {
  79. ElogFilter filter;
  80. size_t enabled_fmt_set[ELOG_LVL_TOTAL_NUM];
  81. bool init_ok;
  82. bool output_enabled;
  83. bool output_lock_enabled;
  84. bool output_is_locked_before_enable;
  85. bool output_is_locked_before_disable;
  86. #ifdef ELOG_COLOR_ENABLE
  87. bool text_color_enabled;
  88. #endif
  89. }EasyLogger, *EasyLogger_t;
  90. /* elog.c */
  91. int elog_init(void);
  92. void elog_deinit(void);
  93. void elog_start(void);
  94. void elog_stop(void);
  95. void elog_set_output_enabled(bool enabled);
  96. bool elog_get_output_enabled(void);
  97. void elog_set_text_color_enabled(bool enabled);
  98. bool elog_get_text_color_enabled(void);
  99. void elog_set_fmt(uint8_t level, size_t set);
  100. void elog_set_filter(uint8_t level, const char *tag, const char *keyword);
  101. void elog_set_filter_lvl(uint8_t level);
  102. void elog_set_filter_tag(const char *tag);
  103. void elog_set_filter_kw(const char *keyword);
  104. void elog_set_filter_tag_lvl(const char *tag, uint8_t level);
  105. uint8_t elog_get_filter_tag_lvl(const char *tag);
  106. void elog_raw_output(const char *format, ...);
  107. void elog_output(uint8_t level, const char *tag, const char *dirfile, const char *funcname, const long line, const char *format, ...);
  108. void elog_output_lock_enabled(bool enabled);
  109. int8_t elog_find_lvl(const char *log);
  110. const char *elog_find_tag(const char *log, uint8_t lvl, size_t *tag_len);
  111. void elog_hexdump(const char *name, uint8_t width, const void *buf, uint16_t size);
  112. /* elog_buf.c */
  113. void elog_buf_enabled(bool enabled);
  114. void elog_flush(void);
  115. /* elog_async.c */
  116. void elog_async_enabled(bool enabled);
  117. size_t elog_async_get_log(char *log, size_t size);
  118. size_t elog_async_get_line_log(char *log, size_t size);
  119. /* elog_utils.c */
  120. size_t elog_strcpy(size_t cur_len, char *dst, const char *src);
  121. size_t elog_cpyln(char *line, const char *log, size_t len);
  122. void *elog_memcpy(void *dst, const void *src, size_t count);
  123. #define elog_a(tag, ...) elog_output(ELOG_LVL_ASSERT, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  124. #define elog_e(tag, ...) elog_output(ELOG_LVL_ERROR, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  125. #define elog_w(tag, ...) elog_output(ELOG_LVL_WARN, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  126. #define elog_i(tag, ...) elog_output(ELOG_LVL_INFO, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  127. #define elog_d(tag, ...) elog_output(ELOG_LVL_DEBUG, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  128. #define elog_v(tag, ...) elog_output(ELOG_LVL_VERBOSE, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  129. /**
  130. * log API short definition
  131. * NOTE: The `LOG_TAG` and `LOG_LVL` must defined before including the <elog.h> when you want to use log_x API.
  132. */
  133. #if !defined(LOG_TAG)
  134. #define LOG_TAG "NO_TAG"
  135. #endif
  136. #define log_a(...) elog_output(ELOG_LVL_ASSERT, LOG_TAG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  137. #define log_e(...) elog_output(ELOG_LVL_ERROR, LOG_TAG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  138. #define log_w(...) elog_output(ELOG_LVL_WARN, LOG_TAG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  139. #define log_i(...) elog_output(ELOG_LVL_INFO, LOG_TAG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  140. #define log_d(...) elog_output(ELOG_LVL_DEBUG, LOG_TAG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  141. #define log_v(...) elog_output(ELOG_LVL_VERBOSE, LOG_TAG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
  142. /* assert API short definition */
  143. #if !defined(assert)
  144. #define assert(EXPR) if (!(EXPR)) { elog_a("elog", "(%s) has assert failed at %s:%ld.", #EXPR, __FILE__, __LINE__); while (1); }
  145. #endif
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif /* __ELOG_H__ */