Skip to content

Commit

Permalink
Merge pull request #60 from MRkuan/master
Browse files Browse the repository at this point in the history
feature: add filter tag lvl fuction
  • Loading branch information
armink authored Nov 30, 2019
2 parents b596fbe + 26ad8f1 commit 7708f76
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 4 deletions.
1 change: 1 addition & 0 deletions demo/non_os/stm32f10x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);` :动态设置过滤关键词级别
2 changes: 2 additions & 0 deletions demo/non_os/stm32f10x/app/src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions demo/non_os/stm32f10x/components/easylogger/inc/elog_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions demo/os/linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);` :动态设置过滤关键词级别
2 changes: 2 additions & 0 deletions demo/os/linux/easylogger/inc/elog_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions demo/os/linux/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions demo/os/windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);` :动态设置过滤关键词级别
2 changes: 2 additions & 0 deletions demo/os/windows/easylogger/inc/elog_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 2 additions & 0 deletions demo/os/windows/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions docs/zh/api/kernel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 使能/失能缓冲输出模式
Expand Down
7 changes: 7 additions & 0 deletions docs/zh/port/kernel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,则直接编译运行,观察测试结果即可。无需关注下面的步骤。
Expand Down
17 changes: 16 additions & 1 deletion easylogger/inc/elog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, ...);
Expand Down
2 changes: 2 additions & 0 deletions easylogger/inc/elog_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/*---------------------------------------------------------------------------*/
Expand Down
113 changes: 110 additions & 3 deletions easylogger/src/elog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 7708f76

Please sign in to comment.