util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] dmesg: add --follow-new
@ 2020-05-30 14:07 Konstantin Khlebnikov
  2020-05-31 11:49 ` Sami Kerola
  2020-06-01  7:56 ` Karel Zak
  0 siblings, 2 replies; 5+ messages in thread
From: Konstantin Khlebnikov @ 2020-05-30 14:07 UTC (permalink / raw)
  To: util-linux

Option --follow-new (-W) works the same as --follow (-w) but initially
seeks to the end of kernel ring buffer, so it prints only new messages.
Useful for capturing kernel messages during actions without past log.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
---
 bash-completion/dmesg |    1 +
 sys-utils/dmesg.1     |    3 +++
 sys-utils/dmesg.c     |   11 +++++++++--
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/bash-completion/dmesg b/bash-completion/dmesg
index 319605f8ee39..02f2fc7a62d4 100644
--- a/bash-completion/dmesg
+++ b/bash-completion/dmesg
@@ -55,6 +55,7 @@ _dmesg_module()
 		--time-format
 		--userspace
 		--follow
+		--follow-new
 		--decode
 		--help
 		--version"
diff --git a/sys-utils/dmesg.1 b/sys-utils/dmesg.1
index 31bfb56f3f5e..61a6ce89465d 100644
--- a/sys-utils/dmesg.1
+++ b/sys-utils/dmesg.1
@@ -193,6 +193,9 @@ Print userspace messages.
 Wait for new messages.  This feature is supported only on systems with
 a readable /dev/kmsg (since kernel 3.5.0).
 .TP
+.BR \-W , " \-\-follow-new"
+Wait and print only new messages.
+.TP
 .BR \-x , " \-\-decode"
 Decode facility and level (priority) numbers to human-readable prefixes.
 .TP
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index 1eb7cde3c1b2..ae1ebc74a2d9 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -190,6 +190,7 @@ struct dmesg_control {
 	unsigned int	time_fmt;	/* time format */
 
 	unsigned int	follow:1,	/* wait for new messages */
+			end:1,		/* seek to the of buffer */
 			raw:1,		/* raw mode */
 			noesc:1,	/* no escape */
 			fltr_lev:1,	/* filter out by levels[] */
@@ -292,6 +293,7 @@ static void __attribute__((__noreturn__)) usage(void)
 	fputs(_(" -s, --buffer-size <size>    buffer size to query the kernel ring buffer\n"), out);
 	fputs(_(" -u, --userspace             display userspace messages\n"), out);
 	fputs(_(" -w, --follow                wait for new messages\n"), out);
+	fputs(_(" -W, --follow-new            wait and print only new messages\n"), out);
 	fputs(_(" -x, --decode                decode facility and level to readable string\n"), out);
 	fputs(_(" -d, --show-delta            show time delta between printed messages\n"), out);
 	fputs(_(" -e, --reltime               show local time and time delta in readable format\n"), out);
@@ -1127,7 +1129,7 @@ static int init_kmsg(struct dmesg_control *ctl)
 	 *
 	 * ... otherwise SYSLOG_ACTION_CLEAR will have no effect for kmsg.
 	 */
-	lseek(ctl->kmsg, 0, SEEK_DATA);
+	lseek(ctl->kmsg, 0, ctl->end ? SEEK_END : SEEK_DATA);
 
 	/*
 	 * Old kernels (<3.5) allow to successfully open /dev/kmsg for
@@ -1336,6 +1338,7 @@ int main(int argc, char *argv[])
 		{ "file",          required_argument, NULL, 'F' },
 		{ "facility",      required_argument, NULL, 'f' },
 		{ "follow",        no_argument,       NULL, 'w' },
+		{ "follow-new",    no_argument,       NULL, 'W' },
 		{ "human",         no_argument,       NULL, 'H' },
 		{ "help",          no_argument,	      NULL, 'h' },
 		{ "kernel",        no_argument,       NULL, 'k' },
@@ -1375,7 +1378,7 @@ int main(int argc, char *argv[])
 	textdomain(PACKAGE);
 	close_stdout_atexit();
 
-	while ((c = getopt_long(argc, argv, "CcDdEeF:f:HhkL::l:n:iPprSs:TtuVwx",
+	while ((c = getopt_long(argc, argv, "CcDdEeF:f:HhkL::l:n:iPprSs:TtuVWwx",
 				longopts, NULL)) != -1) {
 
 		err_exclusive_options(c, longopts, excl, excl_st);
@@ -1466,6 +1469,10 @@ int main(int argc, char *argv[])
 		case 'w':
 			ctl.follow = 1;
 			break;
+		case 'W':
+			ctl.follow = 1;
+			ctl.end = 1;
+			break;
 		case 'x':
 			ctl.decode = 1;
 			break;


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

* Re: [PATCH] dmesg: add --follow-new
  2020-05-30 14:07 [PATCH] dmesg: add --follow-new Konstantin Khlebnikov
@ 2020-05-31 11:49 ` Sami Kerola
  2020-05-31 12:18   ` Konstantin Khlebnikov
  2020-06-01  7:56 ` Karel Zak
  1 sibling, 1 reply; 5+ messages in thread
From: Sami Kerola @ 2020-05-31 11:49 UTC (permalink / raw)
  To: Konstantin Khlebnikov; +Cc: util-linux

On Sat, 30 May 2020 at 15:08, Konstantin Khlebnikov
<khlebnikov@yandex-team.ru> wrote:
> Option --follow-new (-W) works the same as --follow (-w) but initially
> seeks to the end of kernel ring buffer, so it prints only new messages.
> Useful for capturing kernel messages during actions without past log.

Hello Konstantin,

I wonder if it would be more useful to add '-n, --lines=[+]NUM' that
would be similar
to tail(1) option and argument. The --follow-new with an option that
lists NUM last
messages would be --lines=0

That said should --since and --until options similar to journalctl(1)
be considered
as well?

-- 
Sami Kerola
http://www.iki.fi/kerolasa/

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

* Re: [PATCH] dmesg: add --follow-new
  2020-05-31 11:49 ` Sami Kerola
@ 2020-05-31 12:18   ` Konstantin Khlebnikov
  2020-06-01  7:53     ` Karel Zak
  0 siblings, 1 reply; 5+ messages in thread
From: Konstantin Khlebnikov @ 2020-05-31 12:18 UTC (permalink / raw)
  To: kerolasa; +Cc: util-linux

On 31/05/2020 14.49, Sami Kerola wrote:
> On Sat, 30 May 2020 at 15:08, Konstantin Khlebnikov
> <khlebnikov@yandex-team.ru> wrote:
>> Option --follow-new (-W) works the same as --follow (-w) but initially
>> seeks to the end of kernel ring buffer, so it prints only new messages.
>> Useful for capturing kernel messages during actions without past log.
> 
> Hello Konstantin,
> 
> I wonder if it would be more useful to add '-n, --lines=[+]NUM' that
> would be similar
> to tail(1) option and argument. The --follow-new with an option that
> lists NUM last
> messages would be --lines=0
> 
> That said should --since and --until options similar to journalctl(1)
> be considered
> as well?
> 

I don't see how this could be useful. If anybody interested in past
messages then showing whole buffer isn't a big deal - it's only few kb.
Anyway, without --follow this is simply 'dmesg | tail -n NUM'.

Implementation is also non-trivial - currently dmesg reads and prints
messages from /dev/kmsg in a loop. It doesn't know how many lines left.
/dev/kmsg doesn't support lseek with non-zero offset, only SET/DATA/END.

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

* Re: [PATCH] dmesg: add --follow-new
  2020-05-31 12:18   ` Konstantin Khlebnikov
@ 2020-06-01  7:53     ` Karel Zak
  0 siblings, 0 replies; 5+ messages in thread
From: Karel Zak @ 2020-06-01  7:53 UTC (permalink / raw)
  To: Konstantin Khlebnikov; +Cc: kerolasa, util-linux

On Sun, May 31, 2020 at 03:18:47PM +0300, Konstantin Khlebnikov wrote:
> On 31/05/2020 14.49, Sami Kerola wrote:
> > On Sat, 30 May 2020 at 15:08, Konstantin Khlebnikov
> > <khlebnikov@yandex-team.ru> wrote:
> > > Option --follow-new (-W) works the same as --follow (-w) but initially
> > > seeks to the end of kernel ring buffer, so it prints only new messages.
> > > Useful for capturing kernel messages during actions without past log.

IMHO good idea.

> > I wonder if it would be more useful to add '-n, --lines=[+]NUM' that
> > would be similar
> > to tail(1) option and argument. The --follow-new with an option that
> > lists NUM last
> > messages would be --lines=0

It does not seem use-friendly ;-) I guess type "-W" is pretty 
straightforward.
	
> > That said should --since and --until options similar to journalctl(1)
> > be considered
> > as well?

 --since '1 hour ago' would be nice, but I have doubts it's will so
 easy to implement due to crazy timestamps in the log.

> Implementation is also non-trivial - currently dmesg reads and prints
> messages from /dev/kmsg in a loop. It doesn't know how many lines left.
> /dev/kmsg doesn't support lseek with non-zero offset, only SET/DATA/END.

 Yes.

    Karel


-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


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

* Re: [PATCH] dmesg: add --follow-new
  2020-05-30 14:07 [PATCH] dmesg: add --follow-new Konstantin Khlebnikov
  2020-05-31 11:49 ` Sami Kerola
@ 2020-06-01  7:56 ` Karel Zak
  1 sibling, 0 replies; 5+ messages in thread
From: Karel Zak @ 2020-06-01  7:56 UTC (permalink / raw)
  To: Konstantin Khlebnikov; +Cc: util-linux

On Sat, May 30, 2020 at 05:07:53PM +0300, Konstantin Khlebnikov wrote:
>  bash-completion/dmesg |    1 +
>  sys-utils/dmesg.1     |    3 +++
>  sys-utils/dmesg.c     |   11 +++++++++--
>  3 files changed, 13 insertions(+), 2 deletions(-)

Applied, thanks.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


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

end of thread, other threads:[~2020-06-01  7:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-30 14:07 [PATCH] dmesg: add --follow-new Konstantin Khlebnikov
2020-05-31 11:49 ` Sami Kerola
2020-05-31 12:18   ` Konstantin Khlebnikov
2020-06-01  7:53     ` Karel Zak
2020-06-01  7:56 ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).