public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH v2 19/22] cmd: log: Add commands to manipulate filters
Date: Sat, 10 Oct 2020 15:43:44 -0400	[thread overview]
Message-ID: <20201010194347.90096-20-seanga2@gmail.com> (raw)
In-Reply-To: <20201010194347.90096-1-seanga2@gmail.com>

This adds several commands to add, list, and remove log filters. Due to the
complexity of adding a filter, `log filter-list` uses options instead of
positional arguments.

These commands have been added as subcommands to log by using a dash to
join the subcommand and subsubcommand. This is stylistic, and they could be
converted to proper subsubcommands if it is wished.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

Changes in v2:
- Add option to remove all filters to filter-remove
- Clarify filter-* help text

 cmd/Kconfig |   1 +
 cmd/log.c   | 240 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 241 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 4cb171790b..e7e5d88bdb 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -2147,6 +2147,7 @@ config CMD_KGDB
 config CMD_LOG
 	bool "log - Generation, control and access to logging"
 	select LOG
+	select GETOPT
 	help
 	  This provides access to logging features. It allows the output of
 	  log data to be controlled to a limited extent (setting up the default
diff --git a/cmd/log.c b/cmd/log.c
index f3a265798c..3c27b35025 100644
--- a/cmd/log.c
+++ b/cmd/log.c
@@ -7,7 +7,9 @@
 #include <common.h>
 #include <command.h>
 #include <dm.h>
+#include <getopt.h>
 #include <log.h>
+#include <malloc.h>
 
 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
 
@@ -85,6 +87,220 @@ static int do_log_drivers(struct cmd_tbl *cmdtp, int flag, int argc,
 	return CMD_RET_SUCCESS;
 }
 
+static int do_log_filter_list(struct cmd_tbl *cmdtp, int flag, int argc,
+			      char *const argv[])
+{
+	int opt;
+	const char *drv_name = "console";
+	struct getopt_state gs;
+	struct log_filter *filt;
+	struct log_device *ldev;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
+		switch (opt) {
+		case 'd':
+			drv_name = gs.arg;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.index != argc)
+		return CMD_RET_USAGE;
+
+	ldev = log_device_find_by_name(drv_name);
+	if (!ldev) {
+		printf("Could not find log device for \"%s\"\n", drv_name);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("num policy level           categories files\n");
+	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
+		printf("%3d %6.6s %s%-7.7s ", filt->filter_num,
+		       filt->flags & LOGFF_DENY ? "deny" : "allow",
+		       filt->flags & LOGFF_LEVEL_MIN ? ">=" : "<=",
+		       log_get_level_name(filt->level));
+
+		if (filt->flags & LOGFF_HAS_CAT) {
+			int i;
+
+			if (filt->cat_list[0] != LOGC_END)
+				printf("%16.16s %s\n",
+				       log_get_cat_name(filt->cat_list[0]),
+				       filt->file_list ? filt->file_list : "");
+
+			for (i = 1; i < LOGF_MAX_CATEGORIES &&
+				    filt->cat_list[i] != LOGC_END; i++)
+				printf("%20c %16.16s\n", ' ',
+				       log_get_cat_name(filt->cat_list[i]));
+		} else {
+			printf("%16c %s\n", ' ',
+			       filt->file_list ? filt->file_list : "");
+		}
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
+static int do_log_filter_add(struct cmd_tbl *cmdtp, int flag, int argc,
+			     char *const argv[])
+{
+	bool level_set = false;
+	bool print_num = false;
+	bool type_set = false;
+	char *file_list = NULL;
+	const char *drv_name = "console";
+	int opt, err;
+	int cat_count = 0;
+	int flags = 0;
+	enum log_category_t cat_list[LOGF_MAX_CATEGORIES + 1];
+	enum log_level_t level = LOGL_MAX;
+	struct getopt_state gs;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "Ac:d:Df:l:L:p")) > 0) {
+		switch (opt) {
+		case 'A':
+#define do_type() do { \
+			if (type_set) { \
+				printf("Allow or deny set twice\n"); \
+				return CMD_RET_USAGE; \
+			} \
+			type_set = true; \
+} while (0)
+			do_type();
+			break;
+		case 'c': {
+			enum log_category_t cat;
+
+			if (cat_count >= LOGF_MAX_CATEGORIES) {
+				printf("Too many categories\n");
+				return CMD_RET_FAILURE;
+			}
+
+			cat = log_get_cat_by_name(gs.arg);
+			if (cat == LOGC_NONE) {
+				printf("Unknown category \"%s\"\n", gs.arg);
+				return CMD_RET_FAILURE;
+			}
+
+			cat_list[cat_count++] = cat;
+			break;
+		}
+		case 'd':
+			drv_name = gs.arg;
+			break;
+		case 'D':
+			do_type();
+			flags |= LOGFF_DENY;
+			break;
+		case 'f':
+			file_list = gs.arg;
+			break;
+		case 'l':
+#define do_level() do { \
+			if (level_set) { \
+				printf("Log level set twice\n"); \
+				return CMD_RET_USAGE; \
+			} \
+			level = parse_log_level(gs.arg); \
+			if (level == LOGL_NONE) \
+				return CMD_RET_FAILURE; \
+			level_set = true; \
+} while (0)
+			do_level();
+			break;
+		case 'L':
+			do_level();
+			flags |= LOGFF_LEVEL_MIN;
+			break;
+		case 'p':
+			print_num = true;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.index != argc)
+		return CMD_RET_USAGE;
+
+	cat_list[cat_count] = LOGC_END;
+	err = log_add_filter_flags(drv_name, cat_count ? cat_list : NULL, level,
+				   file_list, flags);
+	if (err < 0) {
+		printf("Could not add filter (err = %d)\n", err);
+		return CMD_RET_FAILURE;
+	} else if (print_num) {
+		printf("%d\n", err);
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
+static int do_log_filter_remove(struct cmd_tbl *cmdtp, int flag, int argc,
+				char *const argv[])
+{
+	bool all = false;
+	int opt, err;
+	ulong filter_num;
+	const char *drv_name = "console";
+	struct getopt_state gs;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "ad:")) > 0) {
+		switch (opt) {
+		case 'a':
+			all = true;
+			break;
+		case 'd':
+			drv_name = gs.arg;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (all) {
+		struct log_filter *filt, *tmp_filt;
+		struct log_device *ldev;
+
+		if (gs.index != argc)
+			return CMD_RET_USAGE;
+
+		ldev = log_device_find_by_name(drv_name);
+		if (!ldev) {
+			printf("Could not find log device for \"%s\"\n",
+			       drv_name);
+			return CMD_RET_FAILURE;
+		}
+
+		list_for_each_entry_safe(filt, tmp_filt, &ldev->filter_head,
+					 sibling_node) {
+			list_del(&filt->sibling_node);
+			free(filt);
+		}
+	} else {
+		if (gs.index + 1 != argc)
+			return CMD_RET_USAGE;
+
+		if (strict_strtoul(argv[gs.index], 10, &filter_num)) {
+			printf("Invalid filter number \"%s\"\n", argv[gs.index]);
+			return CMD_RET_FAILURE;
+		}
+
+		err = log_remove_filter(drv_name, filter_num);
+		if (err) {
+			printf("Could not remove filter (err = %d)\n", err);
+			return CMD_RET_FAILURE;
+		}
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
 			 char *const argv[])
 {
@@ -163,6 +379,26 @@ static char log_help_text[] =
 	"level [<level>] - get/set log level\n"
 	"categories - list log categories\n"
 	"drivers - list log drivers\n"
+	"log filter-list [OPTIONS] - list all filters for a log driver\n"
+	"\t-d <driver> - Specify the log driver to list filters from; defaults\n"
+	"\t              to console\n"
+	"log filter-add [OPTIONS] - add a new filter to a driver\n"
+	"\t-A - Allow messages matching this filter; mutually exclusive with -D\n"
+	"\t     This is the default.\n"
+	"\t-c <category> - Category to match; may be specified multiple times\n"
+	"\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
+	"\t              to console\n"
+	"\t-D - Deny messages matching this filter; mutually exclusive with -A\n"
+	"\t-f <files_list> - A comma-separated list of files to match\n"
+	"\t-l <level> - Match log levels less than or equal to <level>;\n"
+	"\t             mutually-exclusive with -L\n"
+	"\t-L <level> - Match log levels greather than or equal to <level>;\n"
+	"\t             mutually-exclusive with -l\n"
+	"\t-p - Print the filter number on success\n"
+	"log filter-remove [OPTIONS] [<num>] - Remove filter number <num>\n"
+	"\t-a - Remove ALL filters\n"
+	"\t-d <driver> - Specify the log driver to remove the filter from;\n"
+	"\t              defaults to console\n"
 	"log format <fmt> - set log output format. <fmt> is a string where\n"
 	"\teach letter indicates something that should be displayed:\n"
 	"\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
@@ -179,6 +415,10 @@ U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
 	U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
 	U_BOOT_SUBCMD_MKENT(categories, 1, 1, do_log_categories),
 	U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_log_drivers),
+	U_BOOT_SUBCMD_MKENT(filter-list, 3, 1, do_log_filter_list),
+	U_BOOT_SUBCMD_MKENT(filter-add, CONFIG_SYS_MAXARGS, 1,
+			    do_log_filter_add),
+	U_BOOT_SUBCMD_MKENT(filter-remove, 4, 1, do_log_filter_remove),
 	U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
 	U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),
 #if CONFIG_IS_ENABLED(LOG_TEST)
-- 
2.28.0

  parent reply	other threads:[~2020-10-10 19:43 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-10 19:43 [PATCH v2 00/22] log: Add commands for manipulating filters Sean Anderson
2020-10-10 19:43 ` [PATCH v2 01/22] log: Fix missing negation of ENOMEM Sean Anderson
2020-10-10 19:43 ` [PATCH v2 02/22] log: Fix incorrect documentation of log_filter.cat_list Sean Anderson
2020-10-10 19:43 ` [PATCH v2 03/22] log: Add additional const qualifier to arrays Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 04/22] log: Add new category names to log_cat_name Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 05/22] log: Use CONFIG_IS_ENABLED() for LOG_TEST Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-12 16:56     ` Sean Anderson
2020-10-14 13:07       ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 06/22] log: Expose some helper functions Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 07/22] log: Add function to create a filter with flags Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 08/22] log: Add filter flag to deny on match Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 09/22] test: Add tests for LOGFF_DENY Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 10/22] log: Add filter flag to match greater than a log level Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 11/22] test: Add test for LOGFF_MIN Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 12/22] cmd: log: Use sub-commands for log Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 13/22] cmd: log: Split off log level parsing Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 14/22] cmd: log: Move log test to end of help string Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 15/22] cmd: log: Add commands to list categories and drivers Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 16/22] cmd: log: Make "log level" print all log levels Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 17/22] lib: Add getopt Sean Anderson
2020-10-10 19:43 ` [PATCH v2 18/22] test: Add a test for getopt Sean Anderson
2020-10-10 19:43 ` Sean Anderson [this message]
2020-10-12  3:35   ` [PATCH v2 19/22] cmd: log: Add commands to manipulate filters Simon Glass
2020-10-10 19:43 ` [PATCH v2 20/22] test: Add a test for log filter-* Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 21/22] doc: Add log kerneldocs to documentation Sean Anderson
2020-10-12  3:35   ` Simon Glass
2020-10-10 19:43 ` [PATCH v2 22/22] doc: Update logging documentation Sean Anderson
2020-10-12  3:35   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201010194347.90096-20-seanga2@gmail.com \
    --to=seanga2@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox