Skip to content

Commit 19eb394

Browse files
authored
Merge pull request #5325 from armink/ulog
[ulog] Add ulog backend filter.
2 parents 8f2a8ef + 4399aed commit 19eb394

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

components/utilities/ulog/backend/console_be.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#error "The thread stack size must more than 384 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)"
1818
#endif
1919

20-
static struct ulog_backend console;
20+
static struct ulog_backend console = { 0 };
2121

2222
void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
2323
const char *log, size_t len)

components/utilities/ulog/ulog.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta
274274
/* add time info */
275275
{
276276
#ifdef ULOG_TIME_USING_TIMESTAMP
277+
extern struct tm* localtime_r(const time_t* t, struct tm* r);
277278
static struct timeval now;
278279
static struct tm *tm, tm_tmp;
279280
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
439440
#if !defined(ULOG_USING_COLOR) || defined(ULOG_USING_SYSLOG)
440441
backend->output(backend, level, tag, is_raw, log, size);
441442
#else
443+
if (backend->filter && backend->filter(backend, level, tag, is_raw, log, size) == RT_FALSE)
444+
{
445+
/* backend's filter is not match, so skip output */
446+
continue;
447+
}
442448
if (backend->support_color || is_raw)
443449
{
444450
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
447453
{
448454
/* recalculate the log start address and log size when backend not supported color */
449455
rt_size_t color_info_len = 0, output_size = size;
450-
char *output_log = log;
456+
const char *output_log = log;
451457

452458
if (color_output_info[level] != RT_NULL)
453459
color_info_len = rt_strlen(color_output_info[level]);
@@ -1300,6 +1306,41 @@ rt_err_t ulog_backend_unregister(ulog_backend_t backend)
13001306
return RT_EOK;
13011307
}
13021308

1309+
rt_err_t ulog_backend_set_filter(ulog_backend_t backend, ulog_backend_filter_t filter)
1310+
{
1311+
rt_base_t level;
1312+
RT_ASSERT(backend);
1313+
1314+
level = rt_hw_interrupt_disable();
1315+
backend->filter = filter;
1316+
rt_hw_interrupt_enable(level);
1317+
1318+
return RT_EOK;
1319+
}
1320+
1321+
ulog_backend_t ulog_backend_find(const char *name)
1322+
{
1323+
rt_base_t level;
1324+
rt_slist_t *node;
1325+
ulog_backend_t backend;
1326+
1327+
RT_ASSERT(ulog.init_ok);
1328+
1329+
level = rt_hw_interrupt_disable();
1330+
for (node = rt_slist_first(&ulog.backend_list); node; node = rt_slist_next(node))
1331+
{
1332+
backend = rt_slist_entry(node, struct ulog_backend, list);
1333+
if (rt_strncmp(backend->name, name, RT_NAME_MAX) == 0)
1334+
{
1335+
rt_hw_interrupt_enable(level);
1336+
return backend;
1337+
}
1338+
}
1339+
1340+
rt_hw_interrupt_enable(level);
1341+
return RT_NULL;
1342+
}
1343+
13031344
#ifdef ULOG_USING_ASYNC_OUTPUT
13041345
/**
13051346
* asynchronous output logs to all backends

components/utilities/ulog/ulog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ void ulog_deinit(void);
5454
*/
5555
rt_err_t ulog_backend_register(ulog_backend_t backend, const char *name, rt_bool_t support_color);
5656
rt_err_t ulog_backend_unregister(ulog_backend_t backend);
57+
rt_err_t ulog_backend_set_filter(ulog_backend_t backend, ulog_backend_filter_t filter);
58+
ulog_backend_t ulog_backend_find(const char *name);
5759

5860
#ifdef ULOG_USING_FILTER
5961
/*

components/utilities/ulog/ulog_def.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,12 @@ struct ulog_backend
211211
void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, size_t len);
212212
void (*flush) (struct ulog_backend *backend);
213213
void (*deinit)(struct ulog_backend *backend);
214+
/* The filter will be call before output. It will return TRUE when the filter condition is math. */
215+
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);
214216
rt_slist_t list;
215217
};
216218
typedef struct ulog_backend *ulog_backend_t;
219+
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);
217220

218221
#ifdef __cplusplus
219222
}

0 commit comments

Comments
 (0)