From 423ed6e6eb0d064a54a95ba4c067f13fcef6f791 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 12 Dec 2023 11:40:01 +0100 Subject: [PATCH] dmesg: man and coding style changes * add note about delta time to the man page * make the latest changes to the code less talkative Signed-off-by: Karel Zak --- sys-utils/dmesg.1.adoc | 4 +++- sys-utils/dmesg.c | 35 +++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/sys-utils/dmesg.1.adoc b/sys-utils/dmesg.1.adoc index 4314131495..ccdeefa8f2 100644 --- a/sys-utils/dmesg.1.adoc +++ b/sys-utils/dmesg.1.adoc @@ -136,11 +136,13 @@ 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*, *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,<-+>. +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. ++ +The *delta* always follows *ctime* or *raw* if specified together. *-u*, *--userspace*:: Print userspace messages. diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index d95318c53f..48928b0953 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -176,10 +176,12 @@ enum { DMESG_TIMEFTM_RELTIME, /* [relative] */ DMESG_TIMEFTM_TIME, /* [time] */ DMESG_TIMEFTM_TIME_DELTA, /* [time ] */ - DMESG_TIMEFTM_ISO8601 /* 2013-06-13T22:11:00,123456+0100 */ + DMESG_TIMEFTM_ISO8601, /* 2013-06-13T22:11:00,123456+0100 */ + + __DMESG_TIMEFTM_COUNT }; -#define TOTAL_DMESG_TIMESTAMP_FORMATS_SUPPORTED 8 -#define DEFAULT_TIMESTAMP_FORMAT DMESG_TIMEFTM_TIME + +#define DMESG_TIMEFTM_DEFAULT DMESG_TIMEFTM_TIME struct dmesg_control { /* bit arrays -- see include/bitops.h */ @@ -218,7 +220,7 @@ struct dmesg_control { char *mmap_buff; size_t pagesize; size_t ntime_fmts; - unsigned int time_fmts[2 * TOTAL_DMESG_TIMESTAMP_FORMATS_SUPPORTED]; /* time format */ + unsigned int time_fmts[2 * __DMESG_TIMEFTM_COUNT]; /* time format */ struct ul_jsonwrt jfmt; /* -J formatting */ @@ -363,8 +365,8 @@ static void __attribute__((__noreturn__)) usage(void) static void reset_time_fmts(struct dmesg_control *ctl) { memset(ctl->time_fmts, 0, - TOTAL_DMESG_TIMESTAMP_FORMATS_SUPPORTED * sizeof(*(ctl->time_fmts))); - ctl->time_fmts[0] = DEFAULT_TIMESTAMP_FORMAT; + __DMESG_TIMEFTM_COUNT * sizeof(*(ctl->time_fmts))); + ctl->time_fmts[0] = DMESG_TIMEFTM_DEFAULT; } static int is_time_fmt_set(struct dmesg_control *ctl, unsigned int time_format) @@ -372,11 +374,12 @@ static int is_time_fmt_set(struct dmesg_control *ctl, unsigned int time_format) size_t i; if (ctl->ntime_fmts == 0) - return time_format == DEFAULT_TIMESTAMP_FORMAT; + return time_format == DMESG_TIMEFTM_DEFAULT; - for (i = 0; i < ctl->ntime_fmts; i++) + for (i = 0; i < ctl->ntime_fmts; i++) { if (ctl->time_fmts[i] == time_format) return 1; + } return 0; } @@ -385,7 +388,7 @@ static void include_time_fmt(struct dmesg_control *ctl, unsigned int time_format if (ctl->ntime_fmts > 0 && is_time_fmt_set(ctl, time_format)) return; - if (ctl->ntime_fmts < TOTAL_DMESG_TIMESTAMP_FORMATS_SUPPORTED) + if (ctl->ntime_fmts < __DMESG_TIMEFTM_COUNT) ctl->time_fmts[ctl->ntime_fmts++] = time_format; } @@ -1009,12 +1012,12 @@ static void print_record(struct dmesg_control *ctl, char buf[128]; char fpbuf[32] = "\0"; char tsbuf[64] = "\0"; - char full_tsbuf[64*TOTAL_DMESG_TIMESTAMP_FORMATS_SUPPORTED] = "\0"; + char full_tsbuf[64 * __DMESG_TIMEFTM_COUNT] = "\0"; size_t mesg_size = rec->mesg_size; int timebreak = 0; char *mesg_copy = NULL; const char *line = NULL; - double delta = record_count_delta(ctl, rec); + double delta = 0; size_t format_iter = 0; if (!accept_record(ctl, rec)) @@ -1026,6 +1029,8 @@ static void print_record(struct dmesg_control *ctl, return; } + delta = record_count_delta(ctl, rec); + if (ctl->json) { if (!ul_jsonwrt_is_ready(&ctl->jfmt)) { ul_jsonwrt_init(&ctl->jfmt, stdout, 0); @@ -1055,8 +1060,9 @@ static void print_record(struct dmesg_control *ctl, level_names[rec->level].name); /* Store the timestamp in a buffer */ - for (format_iter = 0; format_iter < (ctl->ntime_fmts > 0 ? ctl->ntime_fmts : 1); - format_iter++) { + for (format_iter = 0; + format_iter < (ctl->ntime_fmts > 0 ? ctl->ntime_fmts : 1); + format_iter++) { switch (ctl->time_fmts[format_iter]) { struct tm cur; case DMESG_TIMEFTM_NONE: @@ -1507,7 +1513,6 @@ int main(int argc, char *argv[]) .ntime_fmts = 0, .indent = 0, }; - ctl.time_fmts[0] = DEFAULT_TIMESTAMP_FORMAT; int colormode = UL_COLORMODE_UNDEF; enum { OPT_TIME_FORMAT = CHAR_MAX + 1, @@ -1572,6 +1577,8 @@ int main(int argc, char *argv[]) textdomain(PACKAGE); close_stdout_atexit(); + ctl.time_fmts[0] = DMESG_TIMEFTM_DEFAULT; + while ((c = getopt_long(argc, argv, "CcDdEeF:f:HhJK:kL::l:n:iPprSs:TtuVWwx", longopts, NULL)) != -1) { -- 2.47.3