* [PATCH v1 2/2] commands: dmesg: add -l option to restrict output level
2019-02-21 13:26 [PATCH v1 1/2] commands: dmesg: add print raw parameter Oleksij Rempel
@ 2019-02-21 13:26 ` Oleksij Rempel
2019-02-25 8:10 ` [PATCH v1 1/2] commands: dmesg: add print raw parameter Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Oleksij Rempel @ 2019-02-21 13:26 UTC (permalink / raw)
To: barebox; +Cc: Oleksij Rempel
Same as linux dmesg, barebox dmesg will be able to restrict output level
by using -l option. For example "dmesg -l err,warn"
This functionality can be used for test automation.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
commands/dmesg.c | 48 ++++++++++++++++++++++++++++++++++++++---
common/console_common.c | 5 ++++-
include/printk.h | 20 ++++++++++++-----
3 files changed, 64 insertions(+), 9 deletions(-)
diff --git a/commands/dmesg.c b/commands/dmesg.c
index 5c2728581d..a7def2f158 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -24,13 +24,49 @@
#include <getopt.h>
#include <clock.h>
+static unsigned dmesg_get_levels(const char *__args)
+{
+ char *args = xstrdup(__args);
+ char *str, *levels = args;
+ unsigned flags = 0;
+
+ while (1) {
+ str = strsep(&levels, ",");
+ if (!str)
+ break;
+
+ if(!strcmp(str, "vdebug"))
+ flags |= BAREBOX_LOG_PRINT_VDEBUG;
+ else if(!strcmp(str, "debug"))
+ flags |= BAREBOX_LOG_PRINT_DEBUG;
+ else if(!strcmp(str, "info"))
+ flags |= BAREBOX_LOG_PRINT_INFO;
+ else if(!strcmp(str, "notice"))
+ flags |= BAREBOX_LOG_PRINT_NOTICE;
+ else if(!strcmp(str, "warn"))
+ flags |= BAREBOX_LOG_PRINT_WARNING;
+ else if(!strcmp(str, "err"))
+ flags |= BAREBOX_LOG_PRINT_ERR;
+ else if(!strcmp(str, "crit"))
+ flags |= BAREBOX_LOG_PRINT_CRIT;
+ else if(!strcmp(str, "alert"))
+ flags |= BAREBOX_LOG_PRINT_ALERT;
+ else if(!strcmp(str, "emerg"))
+ flags |= BAREBOX_LOG_PRINT_EMERG;
+ }
+
+ free(args);
+
+ return flags;
+}
+
static int do_dmesg(int argc, char *argv[])
{
int opt, i;
int delete_buf = 0, emit = 0;
- unsigned flags = 0;
+ unsigned flags = 0, levels = 0;
- while ((opt = getopt(argc, argv, "ctder")) > 0) {
+ while ((opt = getopt(argc, argv, "ctderl:")) > 0) {
switch (opt) {
case 'c':
delete_buf = 1;
@@ -44,6 +80,11 @@ static int do_dmesg(int argc, char *argv[])
case 'e':
emit = 1;
break;
+ case 'l':
+ levels = dmesg_get_levels(optarg);
+ if (!levels)
+ return COMMAND_ERROR_USAGE;
+ break;
case 'r':
flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
break;
@@ -78,7 +119,7 @@ static int do_dmesg(int argc, char *argv[])
return 0;
}
- log_print(flags);
+ log_print(flags, levels);
if (delete_buf)
log_clean(10);
@@ -91,6 +132,7 @@ BAREBOX_CMD_HELP_TEXT("Options:")
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 <vdebug|debug|info|notice|warn|err|crit|alert|emerg>", "Restrict output to the given (comma-separated) list of levels")
BAREBOX_CMD_HELP_OPT ("-r", "Print timestamp and log-level prefixes.")
BAREBOX_CMD_HELP_OPT ("-t", "Show timestamp informations")
BAREBOX_CMD_HELP_END
diff --git a/common/console_common.c b/common/console_common.c
index bc3a305b27..4aa54de97a 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -193,7 +193,7 @@ static int console_common_init(void)
}
device_initcall(console_common_init);
-void log_print(unsigned flags)
+void log_print(unsigned flags, unsigned levels)
{
struct log_entry *log;
unsigned long last = 0;
@@ -202,6 +202,9 @@ void log_print(unsigned flags)
uint64_t diff = log->timestamp - time_beginning;
unsigned long difful;
+ if (levels && !(levels & (1 << log->level)))
+ continue;
+
if (flags & (BAREBOX_LOG_PRINT_RAW))
printf("<%i>", log->level);
diff --git a/include/printk.h b/include/printk.h
index 64205b2880..b0d5d09f83 100644
--- a/include/printk.h
+++ b/include/printk.h
@@ -134,11 +134,21 @@ extern struct list_head barebox_logbuf;
extern void log_clean(unsigned int limit);
-#define BAREBOX_LOG_PRINT_TIME (1 << 0)
-#define BAREBOX_LOG_DIFF_TIME (1 << 1)
-#define BAREBOX_LOG_PRINT_RAW (1 << 2)
-
-void log_print(unsigned flags);
+#define BAREBOX_LOG_PRINT_RAW BIT(2)
+#define BAREBOX_LOG_DIFF_TIME BIT(1)
+#define BAREBOX_LOG_PRINT_TIME BIT(0)
+
+#define BAREBOX_LOG_PRINT_VDEBUG BIT(8)
+#define BAREBOX_LOG_PRINT_DEBUG BIT(7)
+#define BAREBOX_LOG_PRINT_INFO BIT(6)
+#define BAREBOX_LOG_PRINT_NOTICE BIT(5)
+#define BAREBOX_LOG_PRINT_WARNING BIT(4)
+#define BAREBOX_LOG_PRINT_ERR BIT(3)
+#define BAREBOX_LOG_PRINT_CRIT BIT(2)
+#define BAREBOX_LOG_PRINT_ALERT BIT(1)
+#define BAREBOX_LOG_PRINT_EMERG BIT(0)
+
+void log_print(unsigned flags, unsigned levels);
struct va_format {
const char *fmt;
--
2.20.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v1 1/2] commands: dmesg: add print raw parameter
2019-02-21 13:26 [PATCH v1 1/2] commands: dmesg: add print raw parameter Oleksij Rempel
2019-02-21 13:26 ` [PATCH v1 2/2] commands: dmesg: add -l option to restrict output level Oleksij Rempel
@ 2019-02-25 8:10 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2019-02-25 8:10 UTC (permalink / raw)
To: Oleksij Rempel; +Cc: barebox
On Thu, Feb 21, 2019 at 02:26:02PM +0100, Oleksij Rempel wrote:
> Add -r option to mimic functionality of linux dmesg.
> It will prefix log level and timestamp to each buffer:
> <6>[ 460us] barebox 2019.02.0-00266-g6aea757067-dirty #355 Thu Feb 21 11:51:43 CET 2019
> <6>[ 6279us] Board: DPTechnics DPT-Module
> <6>[ 209281us] mdio_bus: miibus0: probed
> <6>[ 210184us] ag71xx-gmac 18070000.mac@19000000.of: network device registered
> <6>[ 216051us] m25p80 w25q128@00: w25q128 (16384 Kbytes)
> <6>[ 219913us] netconsole: registered as netconsole-1
> <6>[ 223312us] malloc space: 0x80c00000 -> 0x80ffffff (size 4 MiB)
> <6>[ 228255us] eth0: got preset MAC address: c4:93:00:00:ae:89
> <6>[ 246363us] running /env/bin/init...
>
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
Applied, thanks
Sascha
> commands/dmesg.c | 8 ++++++--
> common/console_common.c | 3 +++
> include/printk.h | 1 +
> 3 files changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 510bc16594..5c2728581d 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -30,7 +30,7 @@ static int do_dmesg(int argc, char *argv[])
> int delete_buf = 0, emit = 0;
> unsigned flags = 0;
>
> - while ((opt = getopt(argc, argv, "ctde")) > 0) {
> + while ((opt = getopt(argc, argv, "ctder")) > 0) {
> switch (opt) {
> case 'c':
> delete_buf = 1;
> @@ -44,6 +44,9 @@ static int do_dmesg(int argc, char *argv[])
> case 'e':
> emit = 1;
> break;
> + case 'r':
> + flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
> + break;
> default:
> return COMMAND_ERROR_USAGE;
> }
> @@ -88,13 +91,14 @@ BAREBOX_CMD_HELP_TEXT("Options:")
> 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 ("-r", "Print timestamp and log-level prefixes.")
> BAREBOX_CMD_HELP_OPT ("-t", "Show timestamp informations")
> BAREBOX_CMD_HELP_END
>
> BAREBOX_CMD_START(dmesg)
> .cmd = do_dmesg,
> BAREBOX_CMD_DESC("Print or control log messages")
> - BAREBOX_CMD_OPTS("[-cdet]")
> + BAREBOX_CMD_OPTS("[-cdert]")
> BAREBOX_CMD_GROUP(CMD_GRP_INFO)
> BAREBOX_CMD_HELP(cmd_dmesg_help)
> BAREBOX_CMD_END
> diff --git a/common/console_common.c b/common/console_common.c
> index 0131a1190a..bc3a305b27 100644
> --- a/common/console_common.c
> +++ b/common/console_common.c
> @@ -202,6 +202,9 @@ void log_print(unsigned flags)
> uint64_t diff = log->timestamp - time_beginning;
> unsigned long difful;
>
> + if (flags & (BAREBOX_LOG_PRINT_RAW))
> + printf("<%i>", log->level);
> +
> do_div(diff, 1000);
> difful = diff;
>
> diff --git a/include/printk.h b/include/printk.h
> index ab2c64cf3c..64205b2880 100644
> --- a/include/printk.h
> +++ b/include/printk.h
> @@ -136,6 +136,7 @@ extern void log_clean(unsigned int limit);
>
> #define BAREBOX_LOG_PRINT_TIME (1 << 0)
> #define BAREBOX_LOG_DIFF_TIME (1 << 1)
> +#define BAREBOX_LOG_PRINT_RAW (1 << 2)
>
> void log_print(unsigned flags);
>
> --
> 2.20.1
>
>
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
>
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread