Merge pull request #5325 from armink/ulog

[ulog] Add ulog backend filter.
This commit is contained in:
Bernard Xiong 2021-12-04 22:57:27 +08:00 committed by GitHub
commit 19eb3942d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 2 deletions

View File

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

View File

@ -274,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;
@ -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

View File

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

View File

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