[PATCH] dmesg.c: print human readable timestamp

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello

This patch add the -H option to dmesg which allow to print human readable time instead of the number of seconds since boot.

Best regards,
--- dmesg.c.orig	2011-05-20 13:32:58.000000000 +0200
+++ dmesg.c	2011-05-20 13:58:09.000000000 +0200
@@ -35,6 +35,11 @@
 #include <stdlib.h>
 #include <sys/klog.h>
 #include <ctype.h>
+#include <sys/sysinfo.h>
+#include <time.h>
+#include <sys/time.h>
+#include <error.h>
+#include <errno.h>
 
 #include "c.h"
 #include "nls.h"
@@ -44,7 +49,7 @@
 static void __attribute__ ((noreturn)) usage(void)
 {
 	fprintf(stderr,
-		_("Usage: %s [-c] [-n level] [-r] [-s bufsize]\n"),
+		_("Usage: %s [-c] [-n level] [-r] [-s bufsize] [-H]\n"),
 		program_invocation_short_name);
 	exit(EXIT_FAILURE);
 
@@ -62,12 +67,16 @@
 	int  lastc;
 	int  cmd = 3;		/* Read all messages in the ring buffer */
 	int  raw = 0;
+	int hr = 0;/* hr for human readable*/
+	char *hr_date = NULL;
+	struct sysinfo info;
+	struct timeval mytv;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	while ((c = getopt(argc, argv, "crn:s:")) != -1) {
+	while ((c = getopt(argc, argv, "Hcrn:s:")) != -1) {
 		switch (c) {
 		case 'c':
 			cmd = 4;	/* Read and clear all messages */
@@ -79,6 +88,9 @@
 		case 'r':
 			raw = 1;
 			break;
+		case 'H':
+			hr = 1;
+			break;
 		case 's':
 			bufsize = strtol_or_err(optarg, _("failed to parse buffer size"));
 			if (bufsize < 4096)
@@ -140,12 +152,58 @@
 			if (buf[i] == '>')
 				i++;
 		}
+		if (hr > 0 && i > 0 && buf[i] == '[' && buf[i-1] == '>' && (buf[i+1] == ' ' || (buf[i+1] >= '0' && buf[i+1] <= '9'))) {
+			i++;
+			/* we will read all between those [] and convert them */
+			/* zap all spaces */
+			while (buf[i] == ' ')
+				i++;
+			hr = i;/* start of timestamp*/
+			while (buf[i] != '.')
+				i++;
+			/* end of timestamp */
+			buf[i] = '\0';
+			if (sysinfo(&info) != 0) {
+				error(0, errno, "sysinfo error\n");
+				free(buf);
+				free(hr_date);
+				exit(EXIT_FAILURE);
+			}
+			if (gettimeofday(&mytv, NULL) != 0) {
+				error(0, errno, "gettimeofday error\n");
+				free(buf);
+				free(hr_date);
+				exit(EXIT_FAILURE);
+			}
+			mytv.tv_sec -= info.uptime;
+			mytv.tv_sec += strtol(buf + hr, NULL, 10);
+			if (hr_date == NULL)
+				hr_date = malloc(256);/* ctime_r expect a buffer of 26 at minmum, 256 is enought */
+			if (hr_date == NULL) {
+				error(0, errno, "malloc error\n");
+				free(buf);
+				exit(EXIT_FAILURE);
+			}
+			if (ctime_r(&mytv.tv_sec, hr_date) == NULL) {
+				error(0, errno, "ctime_r error\n");
+				free(buf);
+				free(hr_date);
+				exit(EXIT_FAILURE);
+			}
+			hr_date[strlen(hr_date) - 1] = '\0';
+			printf("[%s]", hr_date);
+			i++;
+			while (buf[i] != ']')
+				i++;
+			i++;
+		}
 		lastc = buf[i];
 		putchar(lastc);
 	}
 	if (lastc != '\n')
 		putchar('\n');
 	free(buf);
+	free(hr_date);
 
 	return EXIT_SUCCESS;
 }

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux