From 69529f9840f5de01a5865d2b55cd741713676956 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Tue, 27 Oct 2020 19:55:20 -0400 Subject: log: Fix missing negation of ENOMEM Errors returned should be negative. Fixes: 45fac9fc18 ("log: Correct missing free() on error in log_add_filter()") Signed-off-by: Sean Anderson Reviewed-by: Heinrich Schuchardt --- common/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/log.c') diff --git a/common/log.c b/common/log.c index 9f98e9aff8b..f1de922b366 100644 --- a/common/log.c +++ b/common/log.c @@ -294,7 +294,7 @@ int log_add_filter(const char *drv_name, enum log_category_t cat_list[], if (file_list) { filt->file_list = strdup(file_list); if (!filt->file_list) { - ret = ENOMEM; + ret = -ENOMEM; goto err; } } -- cgit v1.2.3 From 43381401d541c2d3245e4f210f962494b1533914 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Tue, 27 Oct 2020 19:55:22 -0400 Subject: log: Add additional const qualifier to arrays Both these arrays and their members are const. Fixes checkpatch complaint. Signed-off-by: Sean Anderson Reviewed-by: Simon Glass --- common/log.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'common/log.c') diff --git a/common/log.c b/common/log.c index f1de922b366..47bb9b544e1 100644 --- a/common/log.c +++ b/common/log.c @@ -13,7 +13,7 @@ DECLARE_GLOBAL_DATA_PTR; -static const char *log_cat_name[] = { +static const char *const log_cat_name[] = { "none", "arch", "board", @@ -31,7 +31,7 @@ static const char *log_cat_name[] = { _Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE, "log_cat_name size"); -static const char *log_level_name[] = { +static const char *const log_level_name[] = { "EMERG", "ALERT", "CRIT", -- cgit v1.2.3 From 3102c1d2c21762889e90ec68fd0b3ce5209528a6 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Tue, 27 Oct 2020 19:55:24 -0400 Subject: log: Expose some helper functions These functions are required by "cmd: log: Add commands to manipulate filters" and "test: Add a test for log filter-*". Signed-off-by: Sean Anderson Reviewed-by: Simon Glass --- common/log.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) (limited to 'common/log.c') diff --git a/common/log.c b/common/log.c index 47bb9b544e1..c5de99a8d16 100644 --- a/common/log.c +++ b/common/log.c @@ -99,7 +99,7 @@ enum log_level_t log_get_level_by_name(const char *name) return LOGL_NONE; } -static struct log_device *log_device_find_by_name(const char *drv_name) +struct log_device *log_device_find_by_name(const char *drv_name) { struct log_device *ldev; @@ -111,15 +111,7 @@ static struct log_device *log_device_find_by_name(const char *drv_name) return NULL; } -/** - * log_has_cat() - check if a log category exists within a list - * - * @cat_list: List of categories to check, at most LOGF_MAX_CATEGORIES entries - * long, terminated by LC_END if fewer - * @cat: Category to search for - * @return true if @cat is in @cat_list, else false - */ -static bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat) +bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat) { int i; @@ -131,16 +123,7 @@ static bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat) return false; } -/** - * log_has_file() - check if a file is with a list - * - * @file_list: List of files to check, separated by comma - * @file: File to check for. This string is matched against the end of each - * file in the list, i.e. ignoring any preceding path. The list is - * intended to consist of relative pathnames, e.g. common/main.c,cmd/log.c - * @return true if @file is in @file_list, else false - */ -static bool log_has_file(const char *file_list, const char *file) +bool log_has_file(const char *file_list, const char *file) { int file_len = strlen(file); const char *s, *p; -- cgit v1.2.3 From a02f84ee9c6f42137671b74ec3bb9d159b10b7ee Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Tue, 27 Oct 2020 19:55:25 -0400 Subject: log: Add function to create a filter with flags This function exposes a way to specify flags when creating a filter. Signed-off-by: Sean Anderson Reviewed-by: Simon Glass --- common/log.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'common/log.c') diff --git a/common/log.c b/common/log.c index c5de99a8d16..b7b144fa5d2 100644 --- a/common/log.c +++ b/common/log.c @@ -246,8 +246,9 @@ int _log(enum log_category_t cat, enum log_level_t level, const char *file, return 0; } -int log_add_filter(const char *drv_name, enum log_category_t cat_list[], - enum log_level_t max_level, const char *file_list) +int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[], + enum log_level_t max_level, const char *file_list, + int flags) { struct log_filter *filt; struct log_device *ldev; @@ -261,6 +262,7 @@ int log_add_filter(const char *drv_name, enum log_category_t cat_list[], if (!filt) return -ENOMEM; + filt->flags = flags; if (cat_list) { filt->flags |= LOGFF_HAS_CAT; for (i = 0; ; i++) { -- cgit v1.2.3 From fe3b1a2d21fb35ac212066dd74b7cc4b6bfbccc9 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Tue, 27 Oct 2020 19:55:26 -0400 Subject: log: Add filter flag to deny on match Without this flag, log filters can only explicitly accept messages. Allowing denial makes it easier to filter certain subsystems. Unlike allow-ing filters, deny-ing filters are added to the beginning of the filter list. This should do the Right Thing most of the time, but it's less-universal than allowing filters to be inserted anywhere. If this becomes a problem, then perhaps log_filter_add* should take a filter number to insert before/after. Signed-off-by: Sean Anderson Reviewed-by: Simon Glass --- common/log.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'common/log.c') diff --git a/common/log.c b/common/log.c index b7b144fa5d2..c1b6b6105fb 100644 --- a/common/log.c +++ b/common/log.c @@ -170,7 +170,11 @@ static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec) if (filt->file_list && !log_has_file(filt->file_list, rec->file)) continue; - return true; + + if (filt->flags & LOGFF_DENY) + return false; + else + return true; } return false; @@ -284,7 +288,11 @@ int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[], } } filt->filter_num = ldev->next_filter_num++; - list_add_tail(&filt->sibling_node, &ldev->filter_head); + /* Add deny filters to the beginning of the list */ + if (flags & LOGFF_DENY) + list_add(&filt->sibling_node, &ldev->filter_head); + else + list_add_tail(&filt->sibling_node, &ldev->filter_head); return filt->filter_num; -- cgit v1.2.3 From 40455a6915e1f08e74a71d5c859ef93b6d815899 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Tue, 27 Oct 2020 19:55:30 -0400 Subject: log: Add filter flag to match greater than a log level This is the complement of the existing behavior to match only messages with a log level less than a threshold. This is primarily useful in conjunction with LOGFF_DENY. Signed-off-by: Sean Anderson Reviewed-by: Simon Glass --- common/log.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'common/log.c') diff --git a/common/log.c b/common/log.c index c1b6b6105fb..4b6f3fcd04a 100644 --- a/common/log.c +++ b/common/log.c @@ -162,11 +162,17 @@ static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec) } list_for_each_entry(filt, &ldev->filter_head, sibling_node) { - if (rec->level > filt->max_level) + if (filt->flags & LOGFF_LEVEL_MIN) { + if (rec->level < filt->level) + continue; + } else if (rec->level > filt->level) { continue; + } + if ((filt->flags & LOGFF_HAS_CAT) && !log_has_cat(filt->cat_list, rec->cat)) continue; + if (filt->file_list && !log_has_file(filt->file_list, rec->file)) continue; @@ -251,7 +257,7 @@ int _log(enum log_category_t cat, enum log_level_t level, const char *file, } int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[], - enum log_level_t max_level, const char *file_list, + enum log_level_t level, const char *file_list, int flags) { struct log_filter *filt; @@ -279,7 +285,7 @@ int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[], break; } } - filt->max_level = max_level; + filt->level = level; if (file_list) { filt->file_list = strdup(file_list); if (!filt->file_list) { -- cgit v1.2.3