From: Jonas Rebmann <jre@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
BAREBOX <barebox@lists.infradead.org>
Cc: Jonas Rebmann <jre@pengutronix.de>
Subject: [PATCH] commands: dmesg: clear logbuffer fully if not requested otherwise
Date: Fri, 17 Oct 2025 13:04:36 +0200 [thread overview]
Message-ID: <20251017-dmesg-v1-1-ac9c2fb021f6@pengutronix.de> (raw)
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, move the deleteion of log lines to the log_print function
to prevent a corner case of lost log message if they arrive between the
log_print() and log_clean() in dmesg.
If loglevels are selected using -l or -p in addition to cleaning being
selected using -c, only clean those messages shown by the loglevel
selection.
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
commands/dmesg.c | 17 ++++++++++-------
common/console_common.c | 20 +++++++++++++++++---
include/linux/printk.h | 2 +-
3 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/commands/dmesg.c b/commands/dmesg.c
index a93ad5b359..6853f3afc9 100644
--- a/commands/dmesg.c
+++ b/commands/dmesg.c
@@ -77,14 +77,20 @@ static unsigned dmesg_get_levels(const char *__args)
static int do_dmesg(int argc, char *argv[])
{
int opt, ret, i;
- int delete_buf = 0, emit = 0;
+ int delete_buf = -1, emit = 0;
unsigned 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;
+ if (optarg) {
+ ret = kstrtoint(optarg, 10, &delete_buf);
+ if (ret || delete_buf < 0)
+ return COMMAND_ERROR_USAGE;
+ } else {
+ delete_buf = 0;
+ }
break;
case 't':
flags |= BAREBOX_LOG_PRINT_TIME;
@@ -155,13 +161,10 @@ static int do_dmesg(int argc, char *argv[])
return 0;
}
- ret = log_print(flags, levels);
+ ret = log_print(flags, levels, delete_buf);
if (ret)
return 1;
- if (delete_buf)
- log_clean(10);
-
return 0;
}
diff --git a/common/console_common.c b/common/console_common.c
index 5b7a64c99c..23646d8f81 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -210,12 +210,23 @@ int log_writefile(const char *filepath)
return ret < 0 ? ret : nbytes;
}
-int log_print(unsigned flags, unsigned levels)
+/**
+ * log_print - print and optionally clear the log buffer
+ *
+ * @flags: Flags selecting output formatting
+ * @levels: bitmask of loglevels to print, 0 for all
+ * @delete_buf: Number of printed lines to keep in the buffer,
+ * -1 to keep all.
+ *
+ * This function deletes all messages in the logbuf exceeding
+ * the limit.
+ */
+int log_print(unsigned flags, unsigned levels, int delete_buf)
{
- struct log_entry *log;
+ struct log_entry *log, *tmp;
unsigned long last = 0;
- list_for_each_entry(log, &barebox_logbuf, list) {
+ list_for_each_entry_safe(log, tmp, &barebox_logbuf, list) {
uint64_t time_ns = log->timestamp;
unsigned long time;
@@ -250,6 +261,9 @@ int log_print(unsigned flags, unsigned levels)
printf("] ");
printf("%s", log->msg);
+
+ if (delete_buf >= 0 && barebox_logbuf_num_messages > delete_buf)
+ log_del(log);
}
return 0;
diff --git a/include/linux/printk.h b/include/linux/printk.h
index 0d7180f0eb..78ab3e6d16 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -192,7 +192,7 @@ extern void log_clean(unsigned int limit);
#define BAREBOX_LOG_PRINT_TIME BIT(0)
int log_writefile(const char *filepath);
-int log_print(unsigned flags, unsigned levels);
+int log_print(unsigned flags, unsigned levels, int delete_buf);
struct va_format {
const char *fmt;
---
base-commit: 8defba1d0ab1aef9dd5d57710e18d0d02e2c48e2
change-id: 20251017-dmesg-05b9e462e8a1
Best regards,
--
Jonas Rebmann <jre@pengutronix.de>
next reply other threads:[~2025-10-17 11:05 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-17 11:04 Jonas Rebmann [this message]
2025-10-17 11:17 ` Ahmad Fatoum
2025-10-20 9:36 ` Sascha Hauer
2025-10-20 12:36 ` Jonas Rebmann
2025-10-20 13:23 ` Sascha Hauer
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=20251017-dmesg-v1-1-ac9c2fb021f6@pengutronix.de \
--to=jre@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=s.hauer@pengutronix.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