From 3de2e556da09d7b08ffaca3513a7be606b290a86 Mon Sep 17 00:00:00 2001 From: Rishabh Thukral Date: Tue, 5 Dec 2023 11:53:33 -0800 Subject: [PATCH] dmesg: support for additional human readable timestamp The dmesg logs have timestamps in seconds since boot format which can be converted to other formats. However, there is no option to include both the original timestamp as present in the buffer along with the converted timestamp in the specified format. This change updates the --time-format option in dmesg to enable the user to specify it multiple times with different formats with each input providing a timestamp format among the currently supported choices. This enables seeing the dmesg logs in both seconds since boot and human readable format simultaneously in each row of the log output. The sequence of timestamp format follows the order in which the user provides the desired formats. Signed-off-by: Rishabh Thukral --- sys-utils/dmesg.1.adoc | 4 +- sys-utils/dmesg.c | 257 ++++++++++++++++++++++++++--------------- 2 files changed, 168 insertions(+), 93 deletions(-) diff --git a/sys-utils/dmesg.1.adoc b/sys-utils/dmesg.1.adoc index 156ee0896d..4314131495 100644 --- a/sys-utils/dmesg.1.adoc +++ b/sys-utils/dmesg.1.adoc @@ -136,9 +136,11 @@ Display record until the specified time. Supported is the subsecond granularity. Do not print kernel's timestamps. *--time-format* _format_:: -Print timestamps using the given _format_, which can be *ctime*, *reltime*, *delta* or *iso*. The first three formats are aliases of the time-format-specific options. The *iso* format is a *dmesg* implementation of the ISO-8601 timestamp format. The purpose of this format is to make the comparing of timestamps between two systems, and any other parsing, easy. The definition of the *iso* timestamp is: YYYY-MM-DDHH:MM:SS,<-+>. ++Print timestamps using the given _format_, which can be *ctime*, *reltime*, *delta*, *iso* or *raw*. The first three formats are aliases of the time-format-specific options. The *raw* format uses the default timestamp format showing seconds since boot. The *iso* format is a *dmesg* implementation of the ISO-8601 timestamp format. The purpose of this format is to make the comparing of timestamps between two systems, and any other parsing, easy. The definition of the *iso* timestamp is: YYYY-MM-DDHH:MM:SS,<-+>. + The *iso* format has the same issue as *ctime*: the time may be inaccurate when a system is suspended and resumed. ++ +*--time-format* may be used multiple times with different values for _format_ to output each specified format. *-u*, *--userspace*:: Print userspace messages. diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index 77728b419d..d95318c53f 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -178,7 +178,8 @@ enum { DMESG_TIMEFTM_TIME_DELTA, /* [time ] */ DMESG_TIMEFTM_ISO8601 /* 2013-06-13T22:11:00,123456+0100 */ }; -#define is_timefmt(c, f) ((c)->time_fmt == (DMESG_TIMEFTM_ ##f)) +#define TOTAL_DMESG_TIMESTAMP_FORMATS_SUPPORTED 8 +#define DEFAULT_TIMESTAMP_FORMAT DMESG_TIMEFTM_TIME struct dmesg_control { /* bit arrays -- see include/bitops.h */ @@ -216,7 +217,8 @@ struct dmesg_control { char *filename; char *mmap_buff; size_t pagesize; - unsigned int time_fmt; /* time format */ + size_t ntime_fmts; + unsigned int time_fmts[2 * TOTAL_DMESG_TIMESTAMP_FORMATS_SUPPORTED]; /* time format */ struct ul_jsonwrt jfmt; /* -J formatting */ @@ -335,7 +337,7 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -T, --ctime show human-readable timestamp (may be inaccurate!)\n"), out); fputs(_(" -t, --notime don't show any timestamp with messages\n"), out); fputs(_(" --time-format show timestamp using the given format:\n" - " [delta|reltime|ctime|notime|iso]\n" + " [delta|reltime|ctime|notime|iso|raw]\n" "Suspending/resume will make ctime and iso timestamps inaccurate.\n"), out); fputs(_(" --since