]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: man and coding style changes
authorKarel Zak <kzak@redhat.com>
Tue, 12 Dec 2023 10:40:01 +0000 (11:40 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 12 Dec 2023 10:40:01 +0000 (11:40 +0100)
* add note about delta time to the man page
* make the latest changes to the code less talkative

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/dmesg.1.adoc
sys-utils/dmesg.c

index 4314131495a67438cf344750b1da86b4c94333b7..ccdeefa8f23402da5ee2f2d773c938e27dac80cf 100644 (file)
@@ -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-DD<T>HH:MM:SS,<microseconds><-+><timezone offset from UTC>.
+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-DD<T>HH:MM:SS,<microseconds><-+><timezone offset from UTC>.
 +
 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.
index d95318c53fa806f022e905f0d49b38fe66b3592d..48928b095393fef1beccc4e935ac9cda711744a3 100644 (file)
@@ -176,10 +176,12 @@ enum {
        DMESG_TIMEFTM_RELTIME,          /* [relative] */
        DMESG_TIMEFTM_TIME,             /* [time] */
        DMESG_TIMEFTM_TIME_DELTA,       /* [time <delta>] */
-       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) {