From d8d15017bcdf0c57f5c39060df732c62b8a6122e Mon Sep 17 00:00:00 2001 From: armink Date: Fri, 3 Dec 2021 12:04:20 +0800 Subject: [PATCH 1/2] [ulog] Add ulog backend filter. --- .../utilities/ulog/backend/console_be.c | 2 +- components/utilities/ulog/ulog.c | 43 ++++++++++++++++++- components/utilities/ulog/ulog.h | 2 + components/utilities/ulog/ulog_def.h | 3 ++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/components/utilities/ulog/backend/console_be.c b/components/utilities/ulog/backend/console_be.c index b84cd95d09..47a7603cad 100644 --- a/components/utilities/ulog/backend/console_be.c +++ b/components/utilities/ulog/backend/console_be.c @@ -17,7 +17,7 @@ #error "The thread stack size must more than 384 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)" #endif -static struct ulog_backend console; +static struct ulog_backend console = { 0 }; void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, size_t len) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index cd7a418c3b..00ab383676 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -244,6 +244,7 @@ static char *get_log_buf(void) } } +extern struct tm* localtime_r(const time_t* t, struct tm* r); RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args) { @@ -439,6 +440,11 @@ void ulog_output_to_all_backend(rt_uint32_t level, const char *tag, rt_bool_t is #if !defined(ULOG_USING_COLOR) || defined(ULOG_USING_SYSLOG) backend->output(backend, level, tag, is_raw, log, size); #else + if (backend->filter && backend->filter(backend, level, tag, is_raw, log, size) == RT_FALSE) + { + /* backend's filter is not match, so skip output */ + continue; + } if (backend->support_color || is_raw) { backend->output(backend, level, tag, is_raw, log, size); @@ -447,7 +453,7 @@ void ulog_output_to_all_backend(rt_uint32_t level, const char *tag, rt_bool_t is { /* recalculate the log start address and log size when backend not supported color */ rt_size_t color_info_len = 0, output_size = size; - char *output_log = log; + const char *output_log = log; if (color_output_info[level] != RT_NULL) color_info_len = rt_strlen(color_output_info[level]); @@ -1300,6 +1306,41 @@ rt_err_t ulog_backend_unregister(ulog_backend_t backend) return RT_EOK; } +rt_err_t ulog_backend_set_filter(ulog_backend_t backend, ulog_backend_filter_t filter) +{ + rt_base_t level; + RT_ASSERT(backend); + + level = rt_hw_interrupt_disable(); + backend->filter = filter; + rt_hw_interrupt_enable(level); + + return RT_EOK; +} + +ulog_backend_t ulog_backend_find(const char *name) +{ + rt_base_t level; + rt_slist_t *node; + ulog_backend_t backend; + + RT_ASSERT(ulog.init_ok); + + level = rt_hw_interrupt_disable(); + for (node = rt_slist_first(&ulog.backend_list); node; node = rt_slist_next(node)) + { + backend = rt_slist_entry(node, struct ulog_backend, list); + if (rt_strncmp(backend->name, name, RT_NAME_MAX) == 0) + { + rt_hw_interrupt_enable(level); + return backend; + } + } + + rt_hw_interrupt_enable(level); + return RT_NULL; +} + #ifdef ULOG_USING_ASYNC_OUTPUT /** * asynchronous output logs to all backends diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index 1e139222aa..193dbf38f3 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -54,6 +54,8 @@ void ulog_deinit(void); */ rt_err_t ulog_backend_register(ulog_backend_t backend, const char *name, rt_bool_t support_color); rt_err_t ulog_backend_unregister(ulog_backend_t backend); +rt_err_t ulog_backend_set_filter(ulog_backend_t backend, ulog_backend_filter_t filter); +ulog_backend_t ulog_backend_find(const char *name); #ifdef ULOG_USING_FILTER /* diff --git a/components/utilities/ulog/ulog_def.h b/components/utilities/ulog/ulog_def.h index 73c78365d7..0ddaa70da6 100644 --- a/components/utilities/ulog/ulog_def.h +++ b/components/utilities/ulog/ulog_def.h @@ -211,9 +211,12 @@ struct ulog_backend void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, size_t len); void (*flush) (struct ulog_backend *backend); void (*deinit)(struct ulog_backend *backend); + /* The filter will be call before output. It will return TRUE when the filter condition is math. */ + rt_bool_t (*filter)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, size_t len); rt_slist_t list; }; typedef struct ulog_backend *ulog_backend_t; +typedef rt_bool_t (*ulog_backend_filter_t)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, size_t len); #ifdef __cplusplus } From 4399aed64192f4a312a42eda06b871c2b1387357 Mon Sep 17 00:00:00 2001 From: armink Date: Fri, 3 Dec 2021 13:34:03 +0800 Subject: [PATCH 2/2] fix build error. --- components/utilities/ulog/ulog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 00ab383676..fb268abf35 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -244,7 +244,6 @@ static char *get_log_buf(void) } } -extern struct tm* localtime_r(const time_t* t, struct tm* r); RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args) { @@ -275,6 +274,7 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta /* add time info */ { #ifdef ULOG_TIME_USING_TIMESTAMP + extern struct tm* localtime_r(const time_t* t, struct tm* r); static struct timeval now; static struct tm *tm, tm_tmp; static rt_bool_t check_usec_support = RT_FALSE, usec_is_support = RT_FALSE;