Module Name:    src
Committed By:   kre
Date:           Sat Apr 14 01:37:34 UTC 2018

Modified Files:
        src/sbin/dmesg: dmesg.c

Log Message:
Make timestamp reading code adapt to whatever precision (up to
nanoseconds, 9 digits) the kernel happens to send in the timestamps
in log messages.

Output (numeric) timestamps (when produced) are unchanged and always
in microseconds (for now).


To generate a diff of this commit:
cvs rdiff -u -r1.32 -r1.33 src/sbin/dmesg/dmesg.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/dmesg/dmesg.c
diff -u src/sbin/dmesg/dmesg.c:1.32 src/sbin/dmesg/dmesg.c:1.33
--- src/sbin/dmesg/dmesg.c:1.32	Wed Apr 11 06:41:23 2018
+++ src/sbin/dmesg/dmesg.c	Sat Apr 14 01:37:34 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: dmesg.c,v 1.32 2018/04/11 06:41:23 wiz Exp $	*/
+/*	$NetBSD: dmesg.c,v 1.33 2018/04/14 01:37:34 kre Exp $	*/
 /*-
  * Copyright (c) 1991, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -38,7 +38,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 19
 #if 0
 static char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
 #else
-__RCSID("$NetBSD: dmesg.c,v 1.32 2018/04/11 06:41:23 wiz Exp $");
+__RCSID("$NetBSD: dmesg.c,v 1.33 2018/04/14 01:37:34 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -84,8 +84,10 @@ main(int argc, char *argv[])
 	struct timeval boottime;
 	struct timespec lasttime;
 	intmax_t sec;
-	long nsec;
+	long nsec, fsec;
+	int scale;
 	int deltas, quiet, humantime;
+	bool frac;
 	
 	static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
 	size = sizeof(boottime);
@@ -183,16 +185,23 @@ main(int argc, char *argv[])
 	 * over cur.msg_bufs times.  Unused area is skipped since it
 	 * contains nul.
 	 */
+#ifndef SMALL
+	frac = false;
+	scale = 0;
+#endif
 	for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
 	    i < cur.msg_bufs; i++, p++) {
+
 #ifndef SMALL
 		if (p == bufdata + cur.msg_bufs)
 			p = bufdata;
 #define ADDC(c)				\
-    do 					\
+    do {				\
 	if (tstamp < sizeof(tbuf) - 1)	\
 		tbuf[tstamp++] = (c);	\
-    while (/*CONSTCOND*/0)
+	if (frac)			\
+		scale++;		\
+    } while (/*CONSTCOND*/0)
 #else
 #define ADDC(c)
 #endif
@@ -202,8 +211,16 @@ main(int argc, char *argv[])
 		/* Skip "\n<.*>" syslog sequences. */
 		/* Gather timestamp sequences */
 		if (newl) {
+#ifndef SMALL
+			int j;
+#endif
+
 			switch (ch) {
 			case '[':
+#ifndef SMALL
+				frac = false;
+				scale = 0;
+#endif
 				ADDC(ch);
 				continue;
 			case '<':
@@ -213,16 +230,37 @@ main(int argc, char *argv[])
 				log = 0;
 				continue;
 			case ']':
+#ifndef SMALL
+				frac = false;
+#endif
 				ADDC(ch);
 				ADDC('\0');
 				tstamp = 0;
 #ifndef SMALL
-				sscanf(tbuf, "[%jd.%ld]", &sec, &nsec);
+				sec = fsec = 0;
+				switch (sscanf(tbuf, "[%jd.%ld]", &sec, &fsec)){
+				case EOF:
+				case 0:
+					/*???*/
+					continue;
+				case 1:
+					fsec = 0;
+					break;
+				case 2:
+					break;
+				default:
+					/* Help */
+					continue;
+				}
+
+				for (nsec = fsec, j = 9 - scale; --j >= 0; )
+					nsec *= 10;
 				if (!quiet || deltas)
 					printf("[");
 				if (humantime) {
 					time_t t;
 					struct tm tm;
+
 					t = boottime.tv_sec + sec;
 					if (localtime_r(&t, &tm) != NULL) {
 						strftime(tbuf, sizeof(tbuf),
@@ -231,17 +269,23 @@ main(int argc, char *argv[])
 						printf("%s", tbuf);
 					}
 				} else if (!quiet) {
-					printf("% 9jd.%06ld",
-					    sec, nsec / 1000);
+					if (scale > 6)
+						printf("% 5jd.%6.6ld",
+						    sec, (nsec + 499) / 1000);
+					else
+						printf("% 5jd.%*.*ld%.*s",
+						    sec, scale, scale, fsec,
+						    6 - scale, "000000");
 				}
 				if (deltas) {
 					struct timespec nt = { sec, nsec };
 					struct timespec dt;
+
 					timespecsub(&nt, &lasttime, &dt);
 					if (humantime || !quiet)
 						printf(" ");
 					printf("<% 4jd.%06ld>", (intmax_t)
-					    dt.tv_sec, dt.tv_nsec / 1000);
+					    dt.tv_sec, (dt.tv_nsec+499) / 1000);
 					lasttime = nt;
 				}
 				if (!quiet || deltas)
@@ -255,6 +299,10 @@ main(int argc, char *argv[])
 			default:
 				if (tstamp) {
 				    ADDC(ch);
+#ifndef SMALL
+				    if (ch == '.')
+					frac = true;
+#endif
 				    continue;
 				}
 				if (log)

Reply via email to