From: "corentin.labbe" <corentin.labbe@geomatys.fr>
To: util-linux@vger.kernel.org
Subject: [PATCH] dmesg.c: print human readable timestamp
Date: Fri, 20 May 2011 14:06:21 +0200 [thread overview]
Message-ID: <4DD6593D.2090202@geomatys.fr> (raw)
[-- Attachment #1: Type: text/plain, Size: 146 bytes --]
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,
[-- Attachment #2: dmesg.patch --]
[-- Type: text/x-patch, Size: 2743 bytes --]
--- 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;
}
next reply other threads:[~2011-05-20 12:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-20 12:06 corentin.labbe [this message]
2011-06-29 14:59 ` [PATCH] dmesg.c: print human readable timestamp Karel Zak
2011-07-18 14:56 ` corentin.labbe
2011-07-20 12:48 ` Karel Zak
2011-07-20 13:33 ` Voelker, Bernhard
2011-07-20 13:51 ` Karel Zak
2011-07-20 14:25 ` Voelker, Bernhard
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=4DD6593D.2090202@geomatys.fr \
--to=corentin.labbe@geomatys.fr \
--cc=util-linux@vger.kernel.org \
/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