mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/5] implement dmesg -n
@ 2023-06-12 12:32 Sascha Hauer
  2023-06-12 12:32 ` [PATCH 1/5] dmesg: factor out str_to_loglevel() Sascha Hauer
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: Sascha Hauer @ 2023-06-12 12:32 UTC (permalink / raw)
  To: Barebox List

This series implements dmesg -n to specify the console loglevel
via dmesg command as an alternative to setting global.loglevel
directly. While at it allow specifying loglevels as numbers and
not only as strings.

Sascha Hauer (5):
  dmesg: factor out str_to_loglevel()
  dmesg: error out on unknown loglevels
  dmesg: allow loglevels specified as numbers
  dmesg: implement dmesg -n
  dmesg: udpate help

 commands/dmesg.c       | 89 ++++++++++++++++++++++++++++++++----------
 include/linux/printk.h | 10 -----
 2 files changed, 68 insertions(+), 31 deletions(-)

-- 
2.39.2




^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 1/5] dmesg: factor out str_to_loglevel()
  2023-06-12 12:32 [PATCH 0/5] implement dmesg -n Sascha Hauer
@ 2023-06-12 12:32 ` Sascha Hauer
  2023-06-13  6:41   ` Marco Felsch
  2023-06-12 12:32 ` [PATCH 2/5] dmesg: error out on unknown loglevels Sascha Hauer
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 14+ messages in thread
From: Sascha Hauer @ 2023-06-12 12:32 UTC (permalink / raw)
  To: Barebox List

dmesg open codes functionality to convert a string to a loglevel.
Make a separate function from it which will be useful in the next
patch.
While at it remove the BAREBOX_LOG_PRINT_* defines and use MSG_*
defines directly.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/dmesg.c       | 47 ++++++++++++++++++++++++++----------------
 include/linux/printk.h | 10 ---------
 2 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/commands/dmesg.c b/commands/dmesg.c
index 15ad449639..339910bb76 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -11,6 +11,30 @@
 #include <getopt.h>
 #include <clock.h>
 
+static int str_to_loglevel(const char *str)
+{
+	if (!strcmp(str, "vdebug"))
+		return MSG_VDEBUG;
+	if (!strcmp(str, "debug"))
+		return MSG_DEBUG;
+	if (!strcmp(str, "info"))
+		return MSG_INFO;
+	if (!strcmp(str, "notice"))
+		return MSG_NOTICE;
+	if (!strcmp(str, "warn"))
+		return MSG_WARNING;
+	if (!strcmp(str, "err"))
+		return MSG_ERR;
+	if (!strcmp(str, "crit"))
+		return MSG_CRIT;
+	if (!strcmp(str, "alert"))
+		return MSG_ALERT;
+	if (!strcmp(str, "emerg"))
+		return MSG_EMERG;
+
+	return -EINVAL;
+}
+
 static unsigned dmesg_get_levels(const char *__args)
 {
 	char *args = xstrdup(__args);
@@ -18,28 +42,15 @@ static unsigned dmesg_get_levels(const char *__args)
 	unsigned flags = 0;
 
 	while (1) {
+		int level;
+
 		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;
+		level = str_to_loglevel(str);
+		if (level >= 0)
+			flags |= BIT(level);
 	}
 
 	free(args);
diff --git a/include/linux/printk.h b/include/linux/printk.h
index 42c29e04dd..057b355aa1 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -159,16 +159,6 @@ extern void log_clean(unsigned int limit);
 #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)
-
 int log_writefile(const char *filepath);
 void log_print(unsigned flags, unsigned levels);
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 2/5] dmesg: error out on unknown loglevels
  2023-06-12 12:32 [PATCH 0/5] implement dmesg -n Sascha Hauer
  2023-06-12 12:32 ` [PATCH 1/5] dmesg: factor out str_to_loglevel() Sascha Hauer
@ 2023-06-12 12:32 ` Sascha Hauer
  2023-06-13  6:34   ` Marco Felsch
  2023-06-12 12:32 ` [PATCH 3/5] dmesg: allow loglevels specified as numbers Sascha Hauer
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 14+ messages in thread
From: Sascha Hauer @ 2023-06-12 12:32 UTC (permalink / raw)
  To: Barebox List

The user deserves an error when an unknown loglevel is given, so
print an error instead of silently ignoring unknown loglevels.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/dmesg.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/commands/dmesg.c b/commands/dmesg.c
index 339910bb76..2a36710b98 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
 	if (!strcmp(str, "emerg"))
 		return MSG_EMERG;
 
+	printf("dmesg: unknown loglevel %s\n", str);
+
 	return -EINVAL;
 }
 
@@ -51,6 +53,8 @@ static unsigned dmesg_get_levels(const char *__args)
 		level = str_to_loglevel(str);
 		if (level >= 0)
 			flags |= BIT(level);
+		else
+			return 0;
 	}
 
 	free(args);
@@ -81,7 +85,7 @@ static int do_dmesg(int argc, char *argv[])
 		case 'l':
 			levels = dmesg_get_levels(optarg);
 			if (!levels)
-				return COMMAND_ERROR_USAGE;
+				return COMMAND_ERROR;
 			break;
 		case 'r':
 			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 3/5] dmesg: allow loglevels specified as numbers
  2023-06-12 12:32 [PATCH 0/5] implement dmesg -n Sascha Hauer
  2023-06-12 12:32 ` [PATCH 1/5] dmesg: factor out str_to_loglevel() Sascha Hauer
  2023-06-12 12:32 ` [PATCH 2/5] dmesg: error out on unknown loglevels Sascha Hauer
@ 2023-06-12 12:32 ` Sascha Hauer
  2023-06-13  6:41   ` Marco Felsch
  2023-06-12 12:32 ` [PATCH 4/5] dmesg: implement dmesg -n Sascha Hauer
  2023-06-12 12:32 ` [PATCH 5/5] dmesg: udpate help Sascha Hauer
  4 siblings, 1 reply; 14+ messages in thread
From: Sascha Hauer @ 2023-06-12 12:32 UTC (permalink / raw)
  To: Barebox List

In Linux dmesg loglevels can be specified as strings or as numbers.
Do likewise in barebox.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/dmesg.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/commands/dmesg.c b/commands/dmesg.c
index 2a36710b98..00e5a159e0 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -13,6 +13,16 @@
 
 static int str_to_loglevel(const char *str)
 {
+	int ret;
+	unsigned long level;
+
+	ret = kstrtoul(str, 10, &level);
+	if (!ret) {
+		if (level > MSG_VDEBUG)
+			goto unknown;
+		return level;
+	}
+
 	if (!strcmp(str, "vdebug"))
 		return MSG_VDEBUG;
 	if (!strcmp(str, "debug"))
@@ -31,7 +41,7 @@ static int str_to_loglevel(const char *str)
 		return MSG_ALERT;
 	if (!strcmp(str, "emerg"))
 		return MSG_EMERG;
-
+unknown:
 	printf("dmesg: unknown loglevel %s\n", str);
 
 	return -EINVAL;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 4/5] dmesg: implement dmesg -n
  2023-06-12 12:32 [PATCH 0/5] implement dmesg -n Sascha Hauer
                   ` (2 preceding siblings ...)
  2023-06-12 12:32 ` [PATCH 3/5] dmesg: allow loglevels specified as numbers Sascha Hauer
@ 2023-06-12 12:32 ` Sascha Hauer
  2023-06-13  6:45   ` Marco Felsch
  2023-06-12 12:32 ` [PATCH 5/5] dmesg: udpate help Sascha Hauer
  4 siblings, 1 reply; 14+ messages in thread
From: Sascha Hauer @ 2023-06-12 12:32 UTC (permalink / raw)
  To: Barebox List

Under Linux dmesg -n can be used to set the console loglevel. Implement
the same for barebox.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/dmesg.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/commands/dmesg.c b/commands/dmesg.c
index 00e5a159e0..953bdb2068 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -77,8 +77,9 @@ static int do_dmesg(int argc, char *argv[])
 	int opt, i;
 	int delete_buf = 0, emit = 0;
 	unsigned flags = 0, levels = 0;
+	char *set = NULL;
 
-	while ((opt = getopt(argc, argv, "ctderl:")) > 0) {
+	while ((opt = getopt(argc, argv, "ctderl:n:")) > 0) {
 		switch (opt) {
 		case 'c':
 			delete_buf = 1;
@@ -100,11 +101,25 @@ static int do_dmesg(int argc, char *argv[])
 		case 'r':
 			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
 			break;
+		case 'n':
+			set = optarg;
+			break;
 		default:
 			return COMMAND_ERROR_USAGE;
 		}
 	}
 
+	if (set) {
+		int level = str_to_loglevel(set);
+
+		if (level < 0)
+			return COMMAND_ERROR;
+
+		barebox_loglevel = level;
+
+		return 0;
+	}
+
 	if (emit) {
 		char *buf;
 		int len = 0;
@@ -145,6 +160,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 <vdebug|debug|info|notice|warn|err|crit|alert|emerg>", "Restrict output to the given (comma-separated) list of levels")
+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")
 BAREBOX_CMD_HELP_END
-- 
2.39.2




^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 5/5] dmesg: udpate help
  2023-06-12 12:32 [PATCH 0/5] implement dmesg -n Sascha Hauer
                   ` (3 preceding siblings ...)
  2023-06-12 12:32 ` [PATCH 4/5] dmesg: implement dmesg -n Sascha Hauer
@ 2023-06-12 12:32 ` Sascha Hauer
  2023-06-13  6:40   ` Marco Felsch
  4 siblings, 1 reply; 14+ messages in thread
From: Sascha Hauer @ 2023-06-12 12:32 UTC (permalink / raw)
  To: Barebox List

Now that there are two options which take a loglevel as argument
describe the available loglevels in the command description and
not in the argument description.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/dmesg.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/commands/dmesg.c b/commands/dmesg.c
index 953bdb2068..ae85d98c1a 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -154,12 +154,18 @@ static int do_dmesg(int argc, char *argv[])
 	return 0;
 }
 
+
 BAREBOX_CMD_HELP_START(dmesg)
+BAREBOX_CMD_HELP_TEXT("print or control the barebox message buffer")
+BAREBOX_CMD_HELP_TEXT("Loglevels can be specified as number (0=emerg, 7=vdebug)")
+BAREBOX_CMD_HELP_TEXT("Known debug loglevels are: emerg, alert, crit, err, warn, notice, info, debug,")
+BAREBOX_CMD_HELP_TEXT("vdebug")
+BAREBOX_CMD_HELP_TEXT("")
 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 ("-l <loglevel>", "Restrict output to the given (comma-separated) list of levels")
 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")
-- 
2.39.2




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/5] dmesg: error out on unknown loglevels
  2023-06-12 12:32 ` [PATCH 2/5] dmesg: error out on unknown loglevels Sascha Hauer
@ 2023-06-13  6:34   ` Marco Felsch
  2023-06-13  6:49     ` Sascha Hauer
  0 siblings, 1 reply; 14+ messages in thread
From: Marco Felsch @ 2023-06-13  6:34 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

Hi Sascha,

On 23-06-12, Sascha Hauer wrote:
> The user deserves an error when an unknown loglevel is given, so
> print an error instead of silently ignoring unknown loglevels.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  commands/dmesg.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 339910bb76..2a36710b98 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
>  	if (!strcmp(str, "emerg"))
>  		return MSG_EMERG;
>  
> +	printf("dmesg: unknown loglevel %s\n", str);
> +
>  	return -EINVAL;
>  }
>  
> @@ -51,6 +53,8 @@ static unsigned dmesg_get_levels(const char *__args)
>  		level = str_to_loglevel(str);
>  		if (level >= 0)
>  			flags |= BIT(level);
> +		else
> +			return 0;

This is more explicit but wouldn't be required since we initialized
'flags = 0'.

>  	}
>  
>  	free(args);
> @@ -81,7 +85,7 @@ static int do_dmesg(int argc, char *argv[])
>  		case 'l':
>  			levels = dmesg_get_levels(optarg);
>  			if (!levels)
> -				return COMMAND_ERROR_USAGE;
> +				return COMMAND_ERROR;

Out of curiosity what's the difference between COMMAND_ERROR and
COMMAND_ERROR_USAGE?

Regards,
  Marco

>  			break;
>  		case 'r':
>  			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
> -- 
> 2.39.2
> 
> 
> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 5/5] dmesg: udpate help
  2023-06-12 12:32 ` [PATCH 5/5] dmesg: udpate help Sascha Hauer
@ 2023-06-13  6:40   ` Marco Felsch
  0 siblings, 0 replies; 14+ messages in thread
From: Marco Felsch @ 2023-06-13  6:40 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

Hi Sascha,

On 23-06-12, Sascha Hauer wrote:
> Now that there are two options which take a loglevel as argument
> describe the available loglevels in the command description and
> not in the argument description.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  commands/dmesg.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 953bdb2068..ae85d98c1a 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -154,12 +154,18 @@ static int do_dmesg(int argc, char *argv[])
>  	return 0;
>  }
>  
> +

Not required newline, with that fixed:

Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>

>  BAREBOX_CMD_HELP_START(dmesg)
> +BAREBOX_CMD_HELP_TEXT("print or control the barebox message buffer")
> +BAREBOX_CMD_HELP_TEXT("Loglevels can be specified as number (0=emerg, 7=vdebug)")
> +BAREBOX_CMD_HELP_TEXT("Known debug loglevels are: emerg, alert, crit, err, warn, notice, info, debug,")
> +BAREBOX_CMD_HELP_TEXT("vdebug")
> +BAREBOX_CMD_HELP_TEXT("")
>  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 ("-l <loglevel>", "Restrict output to the given (comma-separated) list of levels")
>  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")
> -- 
> 2.39.2
> 
> 
> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 3/5] dmesg: allow loglevels specified as numbers
  2023-06-12 12:32 ` [PATCH 3/5] dmesg: allow loglevels specified as numbers Sascha Hauer
@ 2023-06-13  6:41   ` Marco Felsch
  0 siblings, 0 replies; 14+ messages in thread
From: Marco Felsch @ 2023-06-13  6:41 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

On 23-06-12, Sascha Hauer wrote:
> In Linux dmesg loglevels can be specified as strings or as numbers.
> Do likewise in barebox.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>

> ---
>  commands/dmesg.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 2a36710b98..00e5a159e0 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -13,6 +13,16 @@
>  
>  static int str_to_loglevel(const char *str)
>  {
> +	int ret;
> +	unsigned long level;
> +
> +	ret = kstrtoul(str, 10, &level);
> +	if (!ret) {
> +		if (level > MSG_VDEBUG)
> +			goto unknown;
> +		return level;
> +	}
> +
>  	if (!strcmp(str, "vdebug"))
>  		return MSG_VDEBUG;
>  	if (!strcmp(str, "debug"))
> @@ -31,7 +41,7 @@ static int str_to_loglevel(const char *str)
>  		return MSG_ALERT;
>  	if (!strcmp(str, "emerg"))
>  		return MSG_EMERG;
> -
> +unknown:
>  	printf("dmesg: unknown loglevel %s\n", str);
>  
>  	return -EINVAL;
> -- 
> 2.39.2
> 
> 
> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/5] dmesg: factor out str_to_loglevel()
  2023-06-12 12:32 ` [PATCH 1/5] dmesg: factor out str_to_loglevel() Sascha Hauer
@ 2023-06-13  6:41   ` Marco Felsch
  0 siblings, 0 replies; 14+ messages in thread
From: Marco Felsch @ 2023-06-13  6:41 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

On 23-06-12, Sascha Hauer wrote:
> dmesg open codes functionality to convert a string to a loglevel.
> Make a separate function from it which will be useful in the next
> patch.
> While at it remove the BAREBOX_LOG_PRINT_* defines and use MSG_*
> defines directly.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>

> ---
>  commands/dmesg.c       | 47 ++++++++++++++++++++++++++----------------
>  include/linux/printk.h | 10 ---------
>  2 files changed, 29 insertions(+), 28 deletions(-)
> 
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 15ad449639..339910bb76 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -11,6 +11,30 @@
>  #include <getopt.h>
>  #include <clock.h>
>  
> +static int str_to_loglevel(const char *str)
> +{
> +	if (!strcmp(str, "vdebug"))
> +		return MSG_VDEBUG;
> +	if (!strcmp(str, "debug"))
> +		return MSG_DEBUG;
> +	if (!strcmp(str, "info"))
> +		return MSG_INFO;
> +	if (!strcmp(str, "notice"))
> +		return MSG_NOTICE;
> +	if (!strcmp(str, "warn"))
> +		return MSG_WARNING;
> +	if (!strcmp(str, "err"))
> +		return MSG_ERR;
> +	if (!strcmp(str, "crit"))
> +		return MSG_CRIT;
> +	if (!strcmp(str, "alert"))
> +		return MSG_ALERT;
> +	if (!strcmp(str, "emerg"))
> +		return MSG_EMERG;
> +
> +	return -EINVAL;
> +}
> +
>  static unsigned dmesg_get_levels(const char *__args)
>  {
>  	char *args = xstrdup(__args);
> @@ -18,28 +42,15 @@ static unsigned dmesg_get_levels(const char *__args)
>  	unsigned flags = 0;
>  
>  	while (1) {
> +		int level;
> +
>  		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;
> +		level = str_to_loglevel(str);
> +		if (level >= 0)
> +			flags |= BIT(level);
>  	}
>  
>  	free(args);
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> index 42c29e04dd..057b355aa1 100644
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -159,16 +159,6 @@ extern void log_clean(unsigned int limit);
>  #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)
> -
>  int log_writefile(const char *filepath);
>  void log_print(unsigned flags, unsigned levels);
>  
> -- 
> 2.39.2
> 
> 
> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/5] dmesg: implement dmesg -n
  2023-06-12 12:32 ` [PATCH 4/5] dmesg: implement dmesg -n Sascha Hauer
@ 2023-06-13  6:45   ` Marco Felsch
  2023-06-13  6:54     ` Sascha Hauer
  0 siblings, 1 reply; 14+ messages in thread
From: Marco Felsch @ 2023-06-13  6:45 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

Hi Sascha,

On 23-06-12, Sascha Hauer wrote:
> Under Linux dmesg -n can be used to set the console loglevel. Implement
> the same for barebox.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  commands/dmesg.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 00e5a159e0..953bdb2068 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -77,8 +77,9 @@ static int do_dmesg(int argc, char *argv[])
>  	int opt, i;
>  	int delete_buf = 0, emit = 0;
>  	unsigned flags = 0, levels = 0;
> +	char *set = NULL;
>  
> -	while ((opt = getopt(argc, argv, "ctderl:")) > 0) {
> +	while ((opt = getopt(argc, argv, "ctderl:n:")) > 0) {
>  		switch (opt) {
>  		case 'c':
>  			delete_buf = 1;
> @@ -100,11 +101,25 @@ static int do_dmesg(int argc, char *argv[])
>  		case 'r':
>  			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
>  			break;
> +		case 'n':
> +			set = optarg;
> +			break;
>  		default:
>  			return COMMAND_ERROR_USAGE;
>  		}
>  	}
>  
> +	if (set) {
> +		int level = str_to_loglevel(set);
> +
> +		if (level < 0)
> +			return COMMAND_ERROR;
> +
> +		barebox_loglevel = level;

Not really realted to this patch, but should we make 'barebox_loglevel'
private and use a setter like console_set_loglevel()? Asking because
console_common.c is the currently the only upstream user.

Regards,
  Marco

> +
> +		return 0;
> +	}
> +
>  	if (emit) {
>  		char *buf;
>  		int len = 0;
> @@ -145,6 +160,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 <vdebug|debug|info|notice|warn|err|crit|alert|emerg>", "Restrict output to the given (comma-separated) list of levels")
> +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")
>  BAREBOX_CMD_HELP_END
> -- 
> 2.39.2
> 
> 
> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/5] dmesg: error out on unknown loglevels
  2023-06-13  6:34   ` Marco Felsch
@ 2023-06-13  6:49     ` Sascha Hauer
  2023-06-13  6:57       ` Marco Felsch
  0 siblings, 1 reply; 14+ messages in thread
From: Sascha Hauer @ 2023-06-13  6:49 UTC (permalink / raw)
  To: Marco Felsch; +Cc: Barebox List

On Tue, Jun 13, 2023 at 08:34:39AM +0200, Marco Felsch wrote:
> Hi Sascha,
> 
> On 23-06-12, Sascha Hauer wrote:
> > The user deserves an error when an unknown loglevel is given, so
> > print an error instead of silently ignoring unknown loglevels.
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > ---
> >  commands/dmesg.c | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> > 
> > diff --git a/commands/dmesg.c b/commands/dmesg.c
> > index 339910bb76..2a36710b98 100644
> > --- a/commands/dmesg.c
> > +++ b/commands/dmesg.c
> > @@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
> >  	if (!strcmp(str, "emerg"))
> >  		return MSG_EMERG;
> >  
> > +	printf("dmesg: unknown loglevel %s\n", str);
> > +
> >  	return -EINVAL;
> >  }
> >  
> > @@ -51,6 +53,8 @@ static unsigned dmesg_get_levels(const char *__args)
> >  		level = str_to_loglevel(str);
> >  		if (level >= 0)
> >  			flags |= BIT(level);
> > +		else
> > +			return 0;
> 
> This is more explicit but wouldn't be required since we initialized
> 'flags = 0'.

It is required as flags might be != 0 already and we want to return 0
in error case. Nevertheless this needs fixing as this return looses
memory allocated with xstrdup above.

> 
> >  	}
> >  
> >  	free(args);
> > @@ -81,7 +85,7 @@ static int do_dmesg(int argc, char *argv[])
> >  		case 'l':
> >  			levels = dmesg_get_levels(optarg);
> >  			if (!levels)
> > -				return COMMAND_ERROR_USAGE;
> > +				return COMMAND_ERROR;
> 
> Out of curiosity what's the difference between COMMAND_ERROR and
> COMMAND_ERROR_USAGE?

When a command returns with COMMAND_ERROR_USAGE then the usage
for the command is printed. COMMAND_ERROR just returns an error
without printing the commands usage.

When a command prints a meaningful error message then I often
find it confusing when the usage is printed, because the error
is often buried in the long help text.

Sascha

---------------------------8<------------------------------

>From adadd6c0fcfd6b11076e0110497dd7ecdb44f99c Mon Sep 17 00:00:00 2001
From: Sascha Hauer <s.hauer@pengutronix.de>
Date: Mon, 12 Jun 2023 14:12:11 +0200
Subject: [PATCH] dmesg: error out on unknown loglevels

The user deserves an error when an unknown loglevel is given, so
print an error instead of silently ignoring unknown loglevels.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/dmesg.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/commands/dmesg.c b/commands/dmesg.c
index 339910bb76..0d46353fb5 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
 	if (!strcmp(str, "emerg"))
 		return MSG_EMERG;
 
+	printf("dmesg: unknown loglevel %s\n", str);
+
 	return -EINVAL;
 }
 
@@ -49,8 +51,12 @@ static unsigned dmesg_get_levels(const char *__args)
 			break;
 
 		level = str_to_loglevel(str);
-		if (level >= 0)
-			flags |= BIT(level);
+		if (level < 0) {
+			flags = 0;
+			break;
+		}
+
+		flags |= BIT(level);
 	}
 
 	free(args);
@@ -81,7 +87,7 @@ static int do_dmesg(int argc, char *argv[])
 		case 'l':
 			levels = dmesg_get_levels(optarg);
 			if (!levels)
-				return COMMAND_ERROR_USAGE;
+				return COMMAND_ERROR;
 			break;
 		case 'r':
 			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
-- 
2.39.2

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/5] dmesg: implement dmesg -n
  2023-06-13  6:45   ` Marco Felsch
@ 2023-06-13  6:54     ` Sascha Hauer
  0 siblings, 0 replies; 14+ messages in thread
From: Sascha Hauer @ 2023-06-13  6:54 UTC (permalink / raw)
  To: Marco Felsch; +Cc: Barebox List

On Tue, Jun 13, 2023 at 08:45:18AM +0200, Marco Felsch wrote:
> Hi Sascha,
> 
> On 23-06-12, Sascha Hauer wrote:
> > Under Linux dmesg -n can be used to set the console loglevel. Implement
> > the same for barebox.
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > ---
> >  commands/dmesg.c | 18 +++++++++++++++++-
> >  1 file changed, 17 insertions(+), 1 deletion(-)
> > 
> > diff --git a/commands/dmesg.c b/commands/dmesg.c
> > index 00e5a159e0..953bdb2068 100644
> > --- a/commands/dmesg.c
> > +++ b/commands/dmesg.c
> > @@ -77,8 +77,9 @@ static int do_dmesg(int argc, char *argv[])
> >  	int opt, i;
> >  	int delete_buf = 0, emit = 0;
> >  	unsigned flags = 0, levels = 0;
> > +	char *set = NULL;
> >  
> > -	while ((opt = getopt(argc, argv, "ctderl:")) > 0) {
> > +	while ((opt = getopt(argc, argv, "ctderl:n:")) > 0) {
> >  		switch (opt) {
> >  		case 'c':
> >  			delete_buf = 1;
> > @@ -100,11 +101,25 @@ static int do_dmesg(int argc, char *argv[])
> >  		case 'r':
> >  			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
> >  			break;
> > +		case 'n':
> > +			set = optarg;
> > +			break;
> >  		default:
> >  			return COMMAND_ERROR_USAGE;
> >  		}
> >  	}
> >  
> > +	if (set) {
> > +		int level = str_to_loglevel(set);
> > +
> > +		if (level < 0)
> > +			return COMMAND_ERROR;
> > +
> > +		barebox_loglevel = level;
> 
> Not really realted to this patch, but should we make 'barebox_loglevel'
> private and use a setter like console_set_loglevel()? Asking because
> console_common.c is the currently the only upstream user.

I asked myself the same question and would likely write it with a setter
nowadays, but I was too lazy to change it ;)

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/5] dmesg: error out on unknown loglevels
  2023-06-13  6:49     ` Sascha Hauer
@ 2023-06-13  6:57       ` Marco Felsch
  0 siblings, 0 replies; 14+ messages in thread
From: Marco Felsch @ 2023-06-13  6:57 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

On 23-06-13, Sascha Hauer wrote:
> On Tue, Jun 13, 2023 at 08:34:39AM +0200, Marco Felsch wrote:
> > Hi Sascha,
> > 
> > On 23-06-12, Sascha Hauer wrote:
> > > The user deserves an error when an unknown loglevel is given, so
> > > print an error instead of silently ignoring unknown loglevels.
> > > 
> > > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > > ---
> > >  commands/dmesg.c | 6 +++++-
> > >  1 file changed, 5 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/commands/dmesg.c b/commands/dmesg.c
> > > index 339910bb76..2a36710b98 100644
> > > --- a/commands/dmesg.c
> > > +++ b/commands/dmesg.c
> > > @@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
> > >  	if (!strcmp(str, "emerg"))
> > >  		return MSG_EMERG;
> > >  
> > > +	printf("dmesg: unknown loglevel %s\n", str);
> > > +
> > >  	return -EINVAL;
> > >  }
> > >  
> > > @@ -51,6 +53,8 @@ static unsigned dmesg_get_levels(const char *__args)
> > >  		level = str_to_loglevel(str);
> > >  		if (level >= 0)
> > >  			flags |= BIT(level);
> > > +		else
> > > +			return 0;
> > 
> > This is more explicit but wouldn't be required since we initialized
> > 'flags = 0'.
> 
> It is required as flags might be != 0 already and we want to return 0
> in error case. Nevertheless this needs fixing as this return looses
> memory allocated with xstrdup above.

Argh.. right I overlooked the while(1).

> > >  	}
> > >  
> > >  	free(args);
> > > @@ -81,7 +85,7 @@ static int do_dmesg(int argc, char *argv[])
> > >  		case 'l':
> > >  			levels = dmesg_get_levels(optarg);
> > >  			if (!levels)
> > > -				return COMMAND_ERROR_USAGE;
> > > +				return COMMAND_ERROR;
> > 
> > Out of curiosity what's the difference between COMMAND_ERROR and
> > COMMAND_ERROR_USAGE?
> 
> When a command returns with COMMAND_ERROR_USAGE then the usage
> for the command is printed. COMMAND_ERROR just returns an error
> without printing the commands usage.
> 
> When a command prints a meaningful error message then I often
> find it confusing when the usage is printed, because the error
> is often buried in the long help text.

Thanks for the clarification, make sense.

> ---------------------------8<------------------------------
> 
> From adadd6c0fcfd6b11076e0110497dd7ecdb44f99c Mon Sep 17 00:00:00 2001
> From: Sascha Hauer <s.hauer@pengutronix.de>
> Date: Mon, 12 Jun 2023 14:12:11 +0200
> Subject: [PATCH] dmesg: error out on unknown loglevels
> 
> The user deserves an error when an unknown loglevel is given, so
> print an error instead of silently ignoring unknown loglevels.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>

> ---
>  commands/dmesg.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 339910bb76..0d46353fb5 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
>  	if (!strcmp(str, "emerg"))
>  		return MSG_EMERG;
>  
> +	printf("dmesg: unknown loglevel %s\n", str);
> +
>  	return -EINVAL;
>  }
>  
> @@ -49,8 +51,12 @@ static unsigned dmesg_get_levels(const char *__args)
>  			break;
>  
>  		level = str_to_loglevel(str);
> -		if (level >= 0)
> -			flags |= BIT(level);
> +		if (level < 0) {
> +			flags = 0;
> +			break;
> +		}
> +
> +		flags |= BIT(level);
>  	}
>  
>  	free(args);
> @@ -81,7 +87,7 @@ static int do_dmesg(int argc, char *argv[])
>  		case 'l':
>  			levels = dmesg_get_levels(optarg);
>  			if (!levels)
> -				return COMMAND_ERROR_USAGE;
> +				return COMMAND_ERROR;
>  			break;
>  		case 'r':
>  			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
> -- 
> 2.39.2
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-06-13  6:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-12 12:32 [PATCH 0/5] implement dmesg -n Sascha Hauer
2023-06-12 12:32 ` [PATCH 1/5] dmesg: factor out str_to_loglevel() Sascha Hauer
2023-06-13  6:41   ` Marco Felsch
2023-06-12 12:32 ` [PATCH 2/5] dmesg: error out on unknown loglevels Sascha Hauer
2023-06-13  6:34   ` Marco Felsch
2023-06-13  6:49     ` Sascha Hauer
2023-06-13  6:57       ` Marco Felsch
2023-06-12 12:32 ` [PATCH 3/5] dmesg: allow loglevels specified as numbers Sascha Hauer
2023-06-13  6:41   ` Marco Felsch
2023-06-12 12:32 ` [PATCH 4/5] dmesg: implement dmesg -n Sascha Hauer
2023-06-13  6:45   ` Marco Felsch
2023-06-13  6:54     ` Sascha Hauer
2023-06-12 12:32 ` [PATCH 5/5] dmesg: udpate help Sascha Hauer
2023-06-13  6:40   ` Marco Felsch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox