1
0
mirror of https://github.com/armink/EasyLogger.git synced 2025-01-31 21:42:53 +08:00

Merge pull request #60 from MRkuan/master

feature: add filter tag lvl fuction
This commit is contained in:
朱天龙 (Armink) 2019-11-30 20:36:54 +08:00 committed by GitHub
commit 7708f7693f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 176 additions and 4 deletions

View File

@ -28,3 +28,4 @@
- `elog_set_filter_lvl(ELOG_LVL_WARN);` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>
- `elog_set_filter_tag("main");` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˱<EFBFBD>ǩ
- `elog_set_filter_kw("Hello");` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˹ؼ<CBB9><D8BC><EFBFBD>
- `elog_set_filter_tag_lvl("main", ELOG_LVL_WARN);` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˹ؼ<CBB9><D8BC>ʼ<EFBFBD><CABC><EFBFBD>

View File

@ -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 */

View File

@ -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"

View File

@ -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);` :动态设置过滤关键词级别

View File

@ -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 */

View File

@ -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();

View File

@ -22,3 +22,4 @@
- `elog_set_filter_lvl(ELOG_LVL_WARN);` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>
- `elog_set_filter_tag("main");` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˱<EFBFBD>ǩ
- `elog_set_filter_kw("Hello");` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˹ؼ<CBB9><D8BC><EFBFBD>
- `elog_set_filter_tag_lvl("main", ELOG_LVL_WARN);` <20><><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˹ؼ<CBB9><D8BC>ʼ<EFBFBD><CABC><EFBFBD>

View File

@ -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"

View File

@ -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();

View File

@ -338,6 +338,32 @@ void elog_set_filter(uint8_t level, const char *tag, const char *keyword)
|tag |标签|
|keyword |关键词|
#### 1.7.3 设置过滤关键词等级
> 注对于配置较低的MCU建议不开启关键词过滤默认为不过滤增加关键字过滤等级将会在很大程度上减低日志的输出效率。实际上当需要实时查看日志时过滤关键词功能交给上位机做会更轻松所以后期的跨平台日志助手开发完成后就无需该功能。
```
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 使能/失能缓冲输出模式

View File

@ -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则直接编译运行观察测试结果即可。无需关注下面的步骤。

View File

@ -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, ...);

View File

@ -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"
/*---------------------------------------------------------------------------*/

View File

@ -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();