summaryrefslogtreecommitdiffstats
path: root/sys-utils/dmesg.c
diff options
context:
space:
mode:
authorSami Kerola2013-06-16 20:53:51 +0200
committerKarel Zak2013-07-01 11:07:39 +0200
commit8a8be309d4d04318baff0a488b3f54862f3f0cc4 (patch)
tree40d8b54f85e8acd045a49ec0b4d401304b74f3b2 /sys-utils/dmesg.c
parentdmesg: add --time-format option (diff)
downloadkernel-qcow2-util-linux-8a8be309d4d04318baff0a488b3f54862f3f0cc4.tar.gz
kernel-qcow2-util-linux-8a8be309d4d04318baff0a488b3f54862f3f0cc4.tar.xz
kernel-qcow2-util-linux-8a8be309d4d04318baff0a488b3f54862f3f0cc4.zip
dmesg: add iso-8601 time format
The ISO-8601 should be welcomed by anyone who tries to parse dmesg time stamp, and compare them across servers. Time format tries to imitate coreutils 'date --iso-8601=ns' output, but instead of having nanoseconds the dmesg is using microseconds. Reference: http://www.cs.tut.fi/~jkorpela/iso8601.html Addresses: http://xkcd.com/1179/ Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'sys-utils/dmesg.c')
-rw-r--r--sys-utils/dmesg.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index 2199e7fea..be08f6c64 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -133,7 +133,8 @@ enum {
DMESG_TIMEFTM_DELTA, /* [<delta>] */
DMESG_TIMEFTM_RELTIME, /* [relative] */
DMESG_TIMEFTM_TIME, /* [time] */
- DMESG_TIMEFTM_TIME_DELTA /* [time <delta>] */
+ DMESG_TIMEFTM_TIME_DELTA, /* [time <delta>] */
+ DMESG_TIMEFTM_ISO8601 /* 2013-06-13T22:11:00,123456+0100 */
};
#define is_timefmt(c, f) (c->time_fmt == (DMESG_TIMEFTM_ ##f))
@@ -258,7 +259,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
fputs(_(" -w, --follow wait for new messages\n"), out);
fputs(_(" -x, --decode decode facility and level to readable string\n"), out);
fputs(_(" --time-format <format> show time stamp using format:\n"
- " [delta|reltime|ctime|notime]\n"), out);
+ " [delta|reltime|ctime|notime|iso]\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
fputs(USAGE_VERSION, out);
@@ -794,6 +795,23 @@ static char *short_ctime(struct tm *tm, char *buf, size_t bufsiz)
return buf;
}
+static char *iso_8601_time(struct dmesg_control *ctl, struct dmesg_record *rec,
+ char *buf, size_t bufsiz)
+{
+ struct tm tm;
+ size_t len;
+ record_localtime(ctl, rec, &tm);
+ if (strftime(buf, bufsiz, "%Y-%m-%dT%H:%M:%S", &tm) == 0) {
+ *buf = '\0';
+ return buf;
+ }
+ len = strlen(buf);
+ snprintf(buf + len, bufsiz - len, ",%06d", (int)rec->tv.tv_usec);
+ len = strlen(buf);
+ strftime(buf + len, bufsiz - len, "%z", &tm);
+ return buf;
+}
+
static double record_count_delta(struct dmesg_control *ctl,
struct dmesg_record *rec)
{
@@ -904,6 +922,9 @@ static void print_record(struct dmesg_control *ctl,
printf("[%5d.%06d <%12.06f>] ", (int)rec->tv.tv_sec,
(int)rec->tv.tv_usec, record_count_delta(ctl, rec));
break;
+ case DMESG_TIMEFTM_ISO8601:
+ printf("%s ", iso_8601_time(ctl, rec, buf, sizeof(buf)));
+ break;
default:
abort();
}
@@ -1131,6 +1152,8 @@ static int which_time_format(const char *optarg)
return DMESG_TIMEFTM_DELTA;
if (!strcmp(optarg, "reltime"))
return DMESG_TIMEFTM_RELTIME;
+ if (!strcmp(optarg, "iso"))
+ return DMESG_TIMEFTM_ISO8601;
errx(EXIT_FAILURE, _("unknown time format: %s"), optarg);
}
@@ -1314,7 +1337,7 @@ int main(int argc, char *argv[])
if (argc > 1)
usage(stderr);
- if (is_timefmt(ctl, RELTIME) || is_timefmt(ctl, CTIME)) {
+ if (is_timefmt(ctl, RELTIME) || is_timefmt(ctl, CTIME) || is_timefmt(ctl, ISO8601)) {
ctl.boot_time = get_boot_time();
if (!ctl.boot_time)
ctl.time_fmt = DMESG_TIMEFTM_NONE;