[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 1/1] Add option -T for timestamp
From:       walter harms <wharms () bfs ! de>
Date:       2019-06-18 15:12:24
Message-ID: 5D08FF58.7030100 () bfs ! de
[Download RAW message or body]


dmesg timestamps are beginning at system start. This is not really helpful
when you try to correlate with external sources.

Signed-off-by: walter <wharms@bfs.de>
---

 util-linux/dmesg.c | 80 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 67 insertions(+), 13 deletions(-)

diff --git a/util-linux/dmesg.c b/util-linux/dmesg.c
index b797c7b..deeaaac 100644
--- a/util-linux/dmesg.c
+++ b/util-linux/dmesg.c
@@ -57,6 +57,49 @@

 #include <sys/klog.h>
 #include "libbb.h"
+#ifdef __linux__
+# include <sys/sysinfo.h>
+#endif
+
+#if ENABLE_FEATURE_DMESG_PRETTY
+static time_t get_offset(void)
+{
+        struct sysinfo info;
+	time_t now;
+	sysinfo(&info);
+	time (&now);
+	return now-info.uptime;
+}
+
+/*
+ * convert "[  xxx.yyy"
+ */
+static void print_time(char *buf, time_t off)
+{
+  long sec,usec;
+  char out[23];
+  sscanf(buf,"[ %ld.%ld",&sec,&usec);
+  sec=sec+off;
+  strftime(out,sizeof(out),"[%Y-%m-%d %H:%M:%S]",gmtime(&sec));
+  printf("%s",out);
+}
+
+static int scan_time(char *buf, int len, int in,time_t off)
+{
+	char timebuf[80];
+	int tcnt=0;
+	while ( buf[in] != ']'
+		&& in < len
+		&& tcnt+1<sizeof(timebuf) ) {
+	  timebuf[tcnt++]=buf[in++];
+	}
+	timebuf[tcnt]=0;
+	tcnt=0;
+	print_time(timebuf,off);
+	return in;
+}
+
+#endif

 int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int dmesg_main(int argc UNUSED_PARAM, char **argv)
@@ -68,10 +111,11 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv)
 		OPT_c = 1 << 0,
 		OPT_s = 1 << 1,
 		OPT_n = 1 << 2,
-		OPT_r = 1 << 3
+		OPT_r = 1 << 3,
+		OPT_T = 1 << 4
 	};

-	opts = getopt32(argv, "cs:+n:+r", &len, &level);
+	opts = getopt32(argv, "cs:+n:+rT", &len, &level);
 	if (opts & OPT_n) {
 		if (klogctl(8, NULL, (long) level))
 			bb_perror_msg_and_die("klogctl");
@@ -94,20 +138,30 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv)


 	if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) {
+
 		int last = '\n';
 		int in = 0;
+		time_t off;
+
+		off=get_offset();
+		while (1) {		
+		  if (last == '\n') {
+		    /* Skip <[0-9]+> at the start of line */
+		    if (buf[in] == '<')
+		      while (buf[in++] != '>' && in < len)
+			;
+		    /* eval [xxx.yyy] at the start of line */
+		    if ( (opts &OPT_T) && in < len && buf[in] == '[') {
+		      in=scan_time(buf,len,in,off);
+		      last = buf[in++]; // skip ]
+		    }
+
+		  }
+		  if (in >= len)
+		    break;

-		/* Skip <[0-9]+> at the start of lines */
-		while (1) {
-			if (last == '\n' && buf[in] == '<') {
-				while (buf[in++] != '>' && in < len)
-					;
-			} else {
-				last = buf[in++];
-				putchar(last);
-			}
-			if (in >= len)
-				break;
+		  last = buf[in++];
+		  putchar(last);
 		}
 		/* Make sure we end with a newline */
 		if (last != '\n')
-- 
2.1.4

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic