From 26ad8f19b8ffe3c8dd46ad89f059dd41a2b274b2 Mon Sep 17 00:00:00 2001 From: KUAN <1062220953@qq.com> Date: Sat, 30 Nov 2019 20:18:00 +0800 Subject: [PATCH] feature: add filter tag lvl fuction 1. add the filter tag lvl fuction 2. update the doc about filter tag lvl fuction --- demo/non_os/stm32f10x/README.md | 1 + demo/non_os/stm32f10x/app/src/app.c | 2 + .../components/easylogger/inc/elog_cfg.h | 2 + demo/os/linux/README.md | 1 + demo/os/linux/easylogger/inc/elog_cfg.h | 2 + demo/os/linux/main.c | 2 + demo/os/windows/README.md | 1 + demo/os/windows/easylogger/inc/elog_cfg.h | 2 + demo/os/windows/main.c | 2 + docs/zh/api/kernel.md | 26 ++++ docs/zh/port/kernel.md | 7 ++ easylogger/inc/elog.h | 17 ++- easylogger/inc/elog_cfg.h | 2 + easylogger/src/elog.c | 113 +++++++++++++++++- 14 files changed, 176 insertions(+), 4 deletions(-) diff --git a/demo/non_os/stm32f10x/README.md b/demo/non_os/stm32f10x/README.md index 8ed7637..120e537 100644 --- a/demo/non_os/stm32f10x/README.md +++ b/demo/non_os/stm32f10x/README.md @@ -28,3 +28,4 @@ - `elog_set_filter_lvl(ELOG_LVL_WARN);` :动态设置过滤优先级 - `elog_set_filter_tag("main");` :动态设置过滤标签 - `elog_set_filter_kw("Hello");` :动态设置过滤关键词 +- `elog_set_filter_tag_lvl("main", ELOG_LVL_WARN);` :动态设置过滤关键词级别 \ No newline at end of file diff --git a/demo/non_os/stm32f10x/app/src/app.c b/demo/non_os/stm32f10x/app/src/app.c index 508ba0c..9beaa79 100644 --- a/demo/non_os/stm32f10x/app/src/app.c +++ b/demo/non_os/stm32f10x/app/src/app.c @@ -59,6 +59,8 @@ int main(void){ // elog_set_filter_tag("main"); /* dynamic set output logs's filter for keyword */ // elog_set_filter_kw("Hello"); + /* dynamic set output logs's tag filter */ +// elog_set_filter_tag_lvl("main", ELOG_LVL_WARN); while(1) { /* test logger output */ diff --git a/demo/non_os/stm32f10x/components/easylogger/inc/elog_cfg.h b/demo/non_os/stm32f10x/components/easylogger/inc/elog_cfg.h index c89b671..f58e578 100644 --- a/demo/non_os/stm32f10x/components/easylogger/inc/elog_cfg.h +++ b/demo/non_os/stm32f10x/components/easylogger/inc/elog_cfg.h @@ -43,6 +43,8 @@ #define ELOG_FILTER_TAG_MAX_LEN 16 /* output filter's keyword max length */ #define ELOG_FILTER_KW_MAX_LEN 16 +/* output filter's tag level max num */ +#define ELOG_FILTER_TAG_LVL_MAX_NUM 5 /* output newline sign */ #define ELOG_NEWLINE_SIGN "\r\n" diff --git a/demo/os/linux/README.md b/demo/os/linux/README.md index 452f983..8143650 100644 --- a/demo/os/linux/README.md +++ b/demo/os/linux/README.md @@ -23,3 +23,4 @@ - `elog_set_filter_lvl(ELOG_LVL_WARN);` 锛氬姩鎬佽缃繃婊や紭鍏堢骇 - `elog_set_filter_tag("main");` 锛氬姩鎬佽缃繃婊ゆ爣绛 - `elog_set_filter_kw("Hello");` 锛氬姩鎬佽缃繃婊ゅ叧閿瘝 +- `elog_set_filter_tag_lvl("main", ELOG_LVL_WARN);` 锛氬姩鎬佽缃繃婊ゅ叧閿瘝绾у埆 diff --git a/demo/os/linux/easylogger/inc/elog_cfg.h b/demo/os/linux/easylogger/inc/elog_cfg.h index 8f3fa20..094558c 100644 --- a/demo/os/linux/easylogger/inc/elog_cfg.h +++ b/demo/os/linux/easylogger/inc/elog_cfg.h @@ -47,6 +47,8 @@ #define ELOG_FILTER_TAG_MAX_LEN 16 /* output filter's keyword max length */ #define ELOG_FILTER_KW_MAX_LEN 16 +/* output filter's tag level max num */ +#define ELOG_FILTER_TAG_LVL_MAX_NUM 5 /* output newline sign */ #define ELOG_NEWLINE_SIGN "\n" /* enable log color */ diff --git a/demo/os/linux/main.c b/demo/os/linux/main.c index 4fcd0ce..76b40f5 100644 --- a/demo/os/linux/main.c +++ b/demo/os/linux/main.c @@ -61,6 +61,8 @@ int main(void) { // elog_set_filter_tag("main"); /* dynamic set output logs's filter for keyword */ // elog_set_filter_kw("Hello"); + /* dynamic set output logs's tag filter */ +// elog_set_filter_tag_lvl("main", ELOG_LVL_WARN); /* test logger output */ test_elog(); diff --git a/demo/os/windows/README.md b/demo/os/windows/README.md index a7edd52..b557405 100644 --- a/demo/os/windows/README.md +++ b/demo/os/windows/README.md @@ -22,3 +22,4 @@ - `elog_set_filter_lvl(ELOG_LVL_WARN);` :动态设置过滤优先级 - `elog_set_filter_tag("main");` :动态设置过滤标签 - `elog_set_filter_kw("Hello");` :动态设置过滤关键词 +- `elog_set_filter_tag_lvl("main", ELOG_LVL_WARN);` :动态设置过滤关键词级别 \ No newline at end of file diff --git a/demo/os/windows/easylogger/inc/elog_cfg.h b/demo/os/windows/easylogger/inc/elog_cfg.h index 5e97773..54301bd 100644 --- a/demo/os/windows/easylogger/inc/elog_cfg.h +++ b/demo/os/windows/easylogger/inc/elog_cfg.h @@ -43,6 +43,8 @@ #define ELOG_FILTER_TAG_MAX_LEN 16 /* output filter's keyword max length */ #define ELOG_FILTER_KW_MAX_LEN 16 +/* output filter's tag level max num */ +#define ELOG_FILTER_TAG_LVL_MAX_NUM 5 /* output newline sign */ #define ELOG_NEWLINE_SIGN "\n" diff --git a/demo/os/windows/main.c b/demo/os/windows/main.c index 7bfbd40..3a860bf 100644 --- a/demo/os/windows/main.c +++ b/demo/os/windows/main.c @@ -58,6 +58,8 @@ int main(void) { // elog_set_filter_tag("main"); /* dynamic set output logs's filter for keyword */ // elog_set_filter_kw("Hello"); + /* dynamic set output logs's tag filter */ +// elog_set_filter_tag_lvl("main", ELOG_LVL_WARN); /* test logger output */ test_elog(); diff --git a/docs/zh/api/kernel.md b/docs/zh/api/kernel.md index 899fe46..7ba57b4 100644 --- a/docs/zh/api/kernel.md +++ b/docs/zh/api/kernel.md @@ -338,6 +338,32 @@ void elog_set_filter(uint8_t level, const char *tag, const char *keyword) |tag |鏍囩| |keyword |鍏抽敭璇峾 +#### 1.7.3 璁剧疆杩囨护鍏抽敭璇嶇瓑绾 + +> 娉細瀵逛簬閰嶇疆杈冧綆鐨凪CU寤鸿涓嶅紑鍚叧閿瘝杩囨护锛堥粯璁や负涓嶈繃婊わ級锛屽鍔犲叧閿瓧杩囨护绛夌骇灏嗕細鍦ㄥ緢澶х▼搴︿笂鍑忎綆鏃ュ織鐨勮緭鍑烘晥鐜囥傚疄闄呬笂褰撻渶瑕佸疄鏃舵煡鐪嬫棩蹇楁椂锛岃繃婊ゅ叧閿瘝鍔熻兘浜ょ粰涓婁綅鏈哄仛浼氭洿杞绘澗锛屾墍浠ュ悗鏈熺殑璺ㄥ钩鍙版棩蹇楀姪鎵嬪紑鍙戝畬鎴愬悗锛屽氨鏃犻渶璇ュ姛鑳姐 + +``` +void elog_set_filter_tag_lvl(const char *tag, uint8_t level); +``` + +|鍙傛暟 |鎻忚堪| +|:----- |:----| +|tag |鏍囩| +|level |绾у埆| + +鍙傛暟 level 鏃ュ織绾у埆鍙彇濡備笅鍊硷細 +``` +绾у埆 鏍囪瘑 鎻忚堪 +0 [A] 鏂█(Assert) +1 [E] 閿欒(Error) +2 [W] 璀﹀憡(Warn) +3 [I] 淇℃伅(Info) +4 [D] 璋冭瘯(Debug) +5 [V] 璇︾粏(Verbose) +0 [A] 闈欓粯鍋滄杈撳嚭 +5 [V] 鍏ㄩ儴 +``` + ### 1.8 缂撳啿杈撳嚭妯″紡 #### 1.8.1 浣胯兘/澶辫兘缂撳啿杈撳嚭妯″紡 diff --git a/docs/zh/port/kernel.md b/docs/zh/port/kernel.md index 45f9ee3..7085d12 100644 --- a/docs/zh/port/kernel.md +++ b/docs/zh/port/kernel.md @@ -229,6 +229,13 @@ const char *elog_port_get_t_info(void) - 榛樿澶у皬锛歚(ELOG_LINE_BUF_SIZE * 10)` 锛屼笉瀹氫箟姝ゅ畯锛屽皢浼氳嚜鍔ㄦ寜鐓ч粯璁ゅ艰缃 - 鎿嶄綔鏂规硶锛氫慨鏀筦ELOG_BUF_OUTPUT_BUF_SIZE`瀹忓搴斿煎嵆鍙 +### 4.6 杩囨护鏍囩绛夌骇鏈澶ф暟鐩 + +鏃ュ織涓爣绛惧唴瀹瑰強鐢ㄦ埛璁剧疆杩囨护鏍囩绛夌骇鐨勬渶澶ф暟鐩 + +- 鎿嶄綔鏂规硶锛氫慨鏀筦ELOG_FILTER_TAG_LVL_MAX_NUM`瀹忓搴斿煎嵆鍙 + + ## 5銆佹祴璇曢獙璇 濡傛灉`\demo\`鏂囦欢澶逛笅鏈変笌椤圭洰骞冲彴涓鑷寸殑Demo锛屽垯鐩存帴缂栬瘧杩愯锛岃瀵熸祴璇曠粨鏋滃嵆鍙傛棤闇鍏虫敞涓嬮潰鐨勬楠ゃ diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index 6d0f971..74aba16 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -46,11 +46,15 @@ extern "C" { #define ELOG_LVL_DEBUG 4 #define ELOG_LVL_VERBOSE 5 +/* the output silent level and all level for filter setting */ +#define LOG_FILTER_LVL_SILENT 0 +#define LOG_FILTER_LVL_ALL 5 + /* output log's level total number */ #define ELOG_LVL_TOTAL_NUM 6 /* EasyLogger software version number */ -#define ELOG_SW_VERSION "2.1.99" +#define ELOG_SW_VERSION "2.2.0" /* EasyLogger assert for developer. */ #ifdef ELOG_ASSERT_ENABLE @@ -135,17 +139,26 @@ typedef enum { #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) +/* output log's tag filter */ +typedef struct { + uint8_t level; + char tag[ELOG_FILTER_TAG_MAX_LEN + 1]; + bool tag_use_flag; /**< false : tag is no used true: tag is used */ +} ElogTagLvlFilter, *ElogTagLvlFilter_t; + /* output log's filter */ typedef struct { uint8_t level; char tag[ELOG_FILTER_TAG_MAX_LEN + 1]; char keyword[ELOG_FILTER_KW_MAX_LEN + 1]; + ElogTagLvlFilter tag_lvl_filter[ELOG_FILTER_TAG_LVL_MAX_NUM]; } ElogFilter, *ElogFilter_t; /* easy logger */ typedef struct { ElogFilter filter; size_t enabled_fmt_set[ELOG_LVL_TOTAL_NUM]; + bool init_ok; bool output_enabled; bool output_lock_enabled; bool output_is_locked_before_enable; @@ -174,6 +187,8 @@ void elog_set_filter(uint8_t level, const char *tag, const char *keyword); void elog_set_filter_lvl(uint8_t level); void elog_set_filter_tag(const char *tag); void elog_set_filter_kw(const char *keyword); +void elog_set_filter_tag_lvl(const char *tag, uint8_t level); +uint8_t elog_get_filter_tag_lvl(const char *tag); void elog_raw(const char *format, ...); void elog_output(uint8_t level, const char *tag, const char *file, const char *func, const long line, const char *format, ...); diff --git a/easylogger/inc/elog_cfg.h b/easylogger/inc/elog_cfg.h index cf62f15..91faf67 100644 --- a/easylogger/inc/elog_cfg.h +++ b/easylogger/inc/elog_cfg.h @@ -43,6 +43,8 @@ #define ELOG_FILTER_TAG_MAX_LEN 30 /* output filter's keyword max length */ #define ELOG_FILTER_KW_MAX_LEN 16 +/* output filter's tag level max num */ +#define ELOG_FILTER_TAG_LVL_MAX_NUM 5 /* output newline sign */ #define ELOG_NEWLINE_SIGN "\n" /*---------------------------------------------------------------------------*/ diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index 945072e..8aed267 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -136,6 +136,7 @@ static const char *color_output_info[] = { #endif /* ELOG_COLOR_ENABLE */ static bool get_fmt_enabled(uint8_t level, size_t set); +static void elog_set_filter_tag_lvl_default(); /* EasyLogger assert hook */ void (*elog_assert_hook)(const char* expr, const char* func, size_t line); @@ -155,6 +156,10 @@ ElogErrCode elog_init(void) { ElogErrCode result = ELOG_NO_ERR; + if (elog.init_ok == true) { + return result; + } + /* port initialize */ result = elog_port_init(); if (result != ELOG_NO_ERR) { @@ -182,6 +187,11 @@ ElogErrCode elog_init(void) { /* set level is ELOG_LVL_VERBOSE */ elog_set_filter_lvl(ELOG_LVL_VERBOSE); + /* set tag_level to default val */ + elog_set_filter_tag_lvl_default(); + + elog.init_ok = true; + return result; } @@ -299,7 +309,7 @@ void elog_set_filter_kw(const char *keyword) { } /** - * lock output + * lock output */ void elog_output_lock(void) { if (elog.output_lock_enabled) { @@ -322,6 +332,103 @@ void elog_output_unlock(void) { } } +/** + * set log filter's tag level val to default + */ +static void elog_set_filter_tag_lvl_default() +{ + uint8_t i = 0; + + for (i =0; i< ELOG_FILTER_TAG_LVL_MAX_NUM; i++){ + memset(elog.filter.tag_lvl_filter[i].tag, '\0', ELOG_FILTER_TAG_MAX_LEN + 1); + elog.filter.tag_lvl_filter[i].level = LOG_FILTER_LVL_SILENT; + elog.filter.tag_lvl_filter[i].tag_use_flag = false; + } +} + +/** + * set log filter's tag level + * + * @param tag tag + * @param level level + */ +void elog_set_filter_tag_lvl(const char *tag, uint8_t level) +{ + ELOG_ASSERT(level <= ELOG_LVL_VERBOSE); + ELOG_ASSERT(tag != ((void *)0)); + uint8_t i = 0; + + if (!elog.init_ok) { + return; + } + + elog_port_output_lock(); + /* find the tag in arr */ + for (i =0; i< ELOG_FILTER_TAG_LVL_MAX_NUM; i++){ + if (elog.filter.tag_lvl_filter[i].tag_use_flag == true && + !strncmp(tag, elog.filter.tag_lvl_filter[i].tag,ELOG_FILTER_TAG_MAX_LEN)){ + break; + } + } + + if (i < ELOG_FILTER_TAG_LVL_MAX_NUM){ + /* find OK */ + if (level == LOG_FILTER_LVL_ALL){ + /* remove current tag's level filter when input level is the lowest level */ + elog.filter.tag_lvl_filter[i].tag_use_flag = false; + memset(elog.filter.tag_lvl_filter[i].tag, '\0', ELOG_FILTER_TAG_MAX_LEN + 1); + elog.filter.tag_lvl_filter[i].level = LOG_FILTER_LVL_SILENT; + } else{ + elog.filter.tag_lvl_filter[i].level = level; + } + } else{ + /* only add the new tag's level filer when level is not LOG_FILTER_LVL_ALL */ + if (level != LOG_FILTER_LVL_ALL){ + for (i =0; i< ELOG_FILTER_TAG_LVL_MAX_NUM; i++){ + if (elog.filter.tag_lvl_filter[i].tag_use_flag == false){ + strncpy(elog.filter.tag_lvl_filter[i].tag, tag, ELOG_FILTER_TAG_MAX_LEN); + elog.filter.tag_lvl_filter[i].level = level; + elog.filter.tag_lvl_filter[i].tag_use_flag = true; + break; + } + } + } + } + elog_output_unlock(); +} + +/** + * get the level on tag's level filer + * + * @param tag tag + * + * @return It will return the lowest level when tag was not found. + * Other level will return when tag was found. + */ +uint8_t elog_get_filter_tag_lvl(const char *tag) +{ + ELOG_ASSERT(tag != ((void *)0)); + uint8_t i = 0; + uint8_t level = LOG_FILTER_LVL_ALL; + + if (!elog.init_ok) { + return level; + } + + elog_port_output_lock(); + /* find the tag in arr */ + for (i =0; i< ELOG_FILTER_TAG_LVL_MAX_NUM; i++){ + if (elog.filter.tag_lvl_filter[i].tag_use_flag == true && + !strncmp(tag, elog.filter.tag_lvl_filter[i].tag,ELOG_FILTER_TAG_MAX_LEN)){ + level = elog.filter.tag_lvl_filter[i].level; + break; + } + } + elog_output_unlock(); + + return level; +} + /** * output RAW format log * @@ -401,7 +508,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f return; } /* level filter */ - if (level > elog.filter.level) { + if (level > elog.filter.level || level > elog_get_filter_tag_lvl(tag)) { return; } else if (!strstr(tag, elog.filter.tag)) { /* tag filter */ return; @@ -693,7 +800,7 @@ void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size) } else if (!strstr(name, elog.filter.tag)) { /* tag filter */ return; } - + /* lock output */ elog_output_lock();