[PATCH] commands: dmesg: add -p option for setting maximum log level

Ahmad Fatoum a.fatoum at barebox.org
Sun Jun 8 23:34:59 PDT 2025


The existing -l option requires the user to specify all log levels they
are interested in, e.g. dmesg -l warn,err,crit,alert,emerg, which is
cumbersome.

Add a new -p option that automatically includes more severe log levels,
so the above can be realized by just dmesg -p warn

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 commands/dmesg.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/commands/dmesg.c b/commands/dmesg.c
index 8a60f075076e..a93ad5b359fb 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -81,7 +81,7 @@ static int do_dmesg(int argc, char *argv[])
 	unsigned flags = 0, levels = 0;
 	char *set = NULL;
 
-	while ((opt = getopt(argc, argv, "ctderl:n:")) > 0) {
+	while ((opt = getopt(argc, argv, "ctderl:p:n:")) > 0) {
 		switch (opt) {
 		case 'c':
 			delete_buf = 1;
@@ -96,9 +96,16 @@ static int do_dmesg(int argc, char *argv[])
 			emit = 1;
 			break;
 		case 'l':
+		case 'p':
 			levels = dmesg_get_levels(optarg);
 			if (!levels)
 				return COMMAND_ERROR;
+			if (opt == 'p') {
+				unsigned maxbit = fls(levels) - 1;
+				if (levels & ~BIT(maxbit))
+					return COMMAND_ERROR_USAGE;
+				levels = GENMASK(maxbit, 0);
+			}
 			break;
 		case 'r':
 			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
@@ -170,6 +177,7 @@ BAREBOX_CMD_HELP_OPT ("-c",		"Delete messages after printing them")
 BAREBOX_CMD_HELP_OPT ("-d",		"Show a time delta to the last message")
 BAREBOX_CMD_HELP_OPT ("-e <msg>",	"Emit a log message")
 BAREBOX_CMD_HELP_OPT ("-l <loglevel>", "Restrict output to the given (comma-separated) list of levels")
+BAREBOX_CMD_HELP_OPT ("-p <loglevel>", "Restrict output to specified log level at most")
 BAREBOX_CMD_HELP_OPT ("-n <loglevel>", "Set level at which printing of messages is done to the console")
 BAREBOX_CMD_HELP_OPT ("-r",		"Print timestamp and log-level prefixes.")
 BAREBOX_CMD_HELP_OPT ("-t",		"Show timestamp informations")
@@ -178,7 +186,7 @@ BAREBOX_CMD_HELP_END
 BAREBOX_CMD_START(dmesg)
 	.cmd	= do_dmesg,
 	BAREBOX_CMD_DESC("Print or control log messages")
-	BAREBOX_CMD_OPTS("[-cdert]")
+	BAREBOX_CMD_OPTS("[-cdelprt]")
 	BAREBOX_CMD_GROUP(CMD_GRP_INFO)
 	BAREBOX_CMD_HELP(cmd_dmesg_help)
 BAREBOX_CMD_END
-- 
2.39.5




More information about the barebox mailing list