* [PATCH v2] commands: dmesg: clear logbuffer fully if not requested otherwise
@ 2025-10-29 13:18 Jonas Rebmann
0 siblings, 0 replies; only message in thread
From: Jonas Rebmann @ 2025-10-29 13:18 UTC (permalink / raw)
To: Sascha Hauer, BAREBOX; +Cc: Ahmad Fatoum, Jonas Rebmann
Previously, `dmesg -c` would clear the logbuffer except for the last 10
lines. This comes with a fair amount of surprise given linux `dmesg -c`
clears the whole logbuffer.
Clear the complete logbuffer given `dmesg -c` but with an optional
argument `dmesg -c [<num>]` to keep <num> lines.
While at it, add documentation for log_print(), and switch it from
ctrlc() to ctrlc_non_interruptible(). This ensures that the complete
execution of the dmesg command has no resched() point so that no other
thread can intermittently modify the logbuffer, and that no message
could arrive from another thread during log_print() and be interleaved
with the dmesg output.
If loglevels are selected using -l or -p in addition to clearing being
selected using -c, then messages of any loglevel are cleared.
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
Changes in v2:
- Switch from ctrlc() to ctrlc_non_interruptible() instead of clearing
in log_print() (Thanks, Ahmad)
- Improve documentation
- Always clear all loglevels regardless of -l/-p (Thanks, Sascha)
- Style improvements (Thanks, Ahmad)
- Link to v1: https://lore.barebox.org/barebox/20251017-dmesg-v1-1-ac9c2fb021f6@pengutronix.de
---
.../migration-guides/migration-2025.11.0.rst | 9 ++++++++
commands/dmesg.c | 25 ++++++++++++----------
common/console_common.c | 12 ++++++++++-
3 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/Documentation/migration-guides/migration-2025.11.0.rst b/Documentation/migration-guides/migration-2025.11.0.rst
index a9c7abb66e..2649923bbc 100644
--- a/Documentation/migration-guides/migration-2025.11.0.rst
+++ b/Documentation/migration-guides/migration-2025.11.0.rst
@@ -18,3 +18,12 @@ Karo TX6X
The barebox update handler for this SoM no longer unconditionally updates
/dev/mmc3.boot0, but instead it now updates the inactive boot partition
on /dev/mmc3 and then sets it as active allowing for power-fail safety.
+
+Shell
+-----
+
+* An optional parameter was added to the `-c` option of `dmesg` allowing
+ configuration of the number of lines to remain in the log buffer after
+ clearing. When no parameter is provided to `-c`, zero is assumed, and no
+ lines are retained. Earlier versions always left 10 lines of logs remain in
+ the log buffer.
diff --git a/commands/dmesg.c b/commands/dmesg.c
index a93ad5b359..0313c8cdea 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -76,15 +76,18 @@ static unsigned dmesg_get_levels(const char *__args)
static int do_dmesg(int argc, char *argv[])
{
+ int emit = 0;
int opt, ret, i;
- int delete_buf = 0, emit = 0;
- unsigned flags = 0, levels = 0;
+ unsigned int keep_lines = UINT_MAX;
+ unsigned int flags = 0, levels = 0;
char *set = NULL;
- while ((opt = getopt(argc, argv, "ctderl:p:n:")) > 0) {
+ while ((opt = getopt(argc, argv, "c::tderl:p:n:")) > 0) {
switch (opt) {
case 'c':
- delete_buf = 1;
+ keep_lines = 0;
+ if (optarg && kstrtouint(optarg, 10, &keep_lines))
+ return COMMAND_ERROR_USAGE;
break;
case 't':
flags |= BAREBOX_LOG_PRINT_TIME;
@@ -159,8 +162,8 @@ static int do_dmesg(int argc, char *argv[])
if (ret)
return 1;
- if (delete_buf)
- log_clean(10);
+ if (keep_lines < UINT_MAX)
+ log_clean(keep_lines);
return 0;
}
@@ -173,13 +176,13 @@ BAREBOX_CMD_HELP_TEXT("Known debug loglevels are: emerg, alert, crit, err, warn,
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 ("-c [<num>]", "Delete messages after printing them, optionally keep <num> lines")
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 ("-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")
BAREBOX_CMD_HELP_END
diff --git a/common/console_common.c b/common/console_common.c
index 5b7a64c99c..3494a3850f 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -5,6 +5,7 @@
* (C) Copyright 2000 Paolo Scaffardi, AIRVENT SAM s.p.a -
* RIMINI(ITALY), arsenio@tin.it
*/
+#include "stdio.h"
#include <common.h>
#include <fs.h>
#include <errno.h>
@@ -210,6 +211,15 @@ int log_writefile(const char *filepath)
return ret < 0 ? ret : nbytes;
}
+/**
+ * log_print - print the log buffer
+ *
+ * @flags: Flags selecting output formatting
+ * @levels: bitmask of loglevels to print, 0 for all
+ *
+ * This function prints the messages of the selected levels; optionally with
+ * additional information and formatting.
+ */
int log_print(unsigned flags, unsigned levels)
{
struct log_entry *log;
@@ -221,7 +231,7 @@ int log_print(unsigned flags, unsigned levels)
if (levels && !(levels & (1 << log->level)))
continue;
- if (ctrlc())
+ if (ctrlc_non_interruptible())
return -EINTR;
if (!(flags & (BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME
---
base-commit: 946cf6bbbe2bdaac56b17185af673a1fa3288635
change-id: 20251017-dmesg-05b9e462e8a1
Best regards,
--
Jonas Rebmann <jre@pengutronix.de>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-10-29 13:19 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-29 13:18 [PATCH v2] commands: dmesg: clear logbuffer fully if not requested otherwise Jonas Rebmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox