[systemd-devel] [RFC PATCH] logs-show: add short-precise mode with us timestamps

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Tue Aug 6 20:15:25 PDT 2013


Also, always show us timestamps in verbose mode.

https://bugzilla.redhat.com/show_bug.cgi?id=991678
---
Is verbose mode fixed, or can/should it be amended to show us timestamps?
short-precise seems useful, especially when looking at boot time messages
when a lot of things happen in one second. I don't like the name though.

Zbyszek

 man/journalctl.xml       | 25 +++++++++++++++++++------
 src/journal/journalctl.c |  5 +++--
 src/shared/logs-show.c   | 25 +++++++++++++++++++------
 src/shared/output-mode.h |  3 ++-
 src/shared/time-util.c   | 22 ++++++++++++++++++++++
 src/shared/time-util.h   |  1 +
 6 files changed, 66 insertions(+), 15 deletions(-)

diff --git a/man/journalctl.xml b/man/journalctl.xml
index dcc6d5d..8680e53 100644
--- a/man/journalctl.xml
+++ b/man/journalctl.xml
@@ -253,12 +253,11 @@
 
                                         <varlistentry>
                                                 <term>
-                                                        <option>short-monotonic</option>
+                                                        <option>short-iso</option>
                                                 </term>
                                                 <listitem>
-                                                        <para>is very similar
-                                                        but shows monotonic
-                                                        timestamps instead of
+                                                        <para>is very similar,
+                                                        but shows ISO 8601
                                                         wallclock timestamps.
                                                         </para>
                                                 </listitem>
@@ -266,11 +265,25 @@
 
                                         <varlistentry>
                                                 <term>
-                                                        <option>short-iso</option>
+                                                        <option>short-precise</option>
                                                 </term>
                                                 <listitem>
                                                         <para>is very similar,
-                                                        but shows ISO 8601
+                                                        but shows timestamps
+                                                        with full microsecond
+                                                        precision.
+                                                        </para>
+                                                </listitem>
+                                        </varlistentry>
+
+                                        <varlistentry>
+                                                <term>
+                                                        <option>short-monotonic</option>
+                                                </term>
+                                                <listitem>
+                                                        <para>is very similar
+                                                        but shows monotonic
+                                                        timestamps instead of
                                                         wallclock timestamps.
                                                         </para>
                                                 </listitem>
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index feea6bf..6716669 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -134,8 +134,9 @@ static int help(void) {
                "  -n --lines[=INTEGER]     Number of journal entries to show\n"
                "     --no-tail             Show all lines, even in follow mode\n"
                "  -r --reverse             Show the newest entries first\n"
-               "  -o --output=STRING       Change journal output mode (short, short-monotonic, short-iso\n"
-               "                           verbose, export, json, json-pretty, json-sse, cat)\n"
+               "  -o --output=STRING       Change journal output mode (short, short-iso,\n"
+               "                           short-precise, short-monotonic, verbose,\n"
+               "                           export, json, json-pretty, json-sse, cat)\n"
                "  -x --catalog             Add message explanations where available\n"
                "  -l --full                Do not ellipsize fields\n"
                "  -a --all                 Show all fields, including long and unprintable\n"
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index bd7363a..8741f29 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -273,10 +273,21 @@ static int output_short(
                 }
 
                 t = (time_t) (x / USEC_PER_SEC);
-                if (mode == OUTPUT_SHORT_ISO)
+
+                switch(mode) {
+                case OUTPUT_SHORT_ISO:
                         r = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", localtime_r(&t, &tm));
-                else
+                        break;
+                case OUTPUT_SHORT_PRECISE:
+                        r = strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm));
+                        if (r > 0) {
+                                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                                         ".%06llu", x % USEC_PER_SEC);
+                        }
+                        break;
+                default:
                         r = strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm));
+                }
 
                 if (r <= 0) {
                         log_error("Failed to format time.");
@@ -334,7 +345,7 @@ static int output_verbose(
         size_t length;
         _cleanup_free_ char *cursor = NULL;
         uint64_t realtime;
-        char ts[FORMAT_TIMESTAMP_MAX];
+        char ts[FORMAT_TIMESTAMP_MAX + 7];
         int r;
 
         assert(f);
@@ -356,7 +367,7 @@ static int output_verbose(
         }
 
         fprintf(f, "%s [%s]\n",
-                format_timestamp(ts, sizeof(ts), realtime),
+                format_timestamp_us(ts, sizeof(ts), realtime),
                 cursor);
 
         JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
@@ -802,8 +813,9 @@ static int (*output_funcs[_OUTPUT_MODE_MAX])(
                 OutputFlags flags) = {
 
         [OUTPUT_SHORT] = output_short,
-        [OUTPUT_SHORT_MONOTONIC] = output_short,
         [OUTPUT_SHORT_ISO] = output_short,
+        [OUTPUT_SHORT_PRECISE] = output_short,
+        [OUTPUT_SHORT_MONOTONIC] = output_short,
         [OUTPUT_VERBOSE] = output_verbose,
         [OUTPUT_EXPORT] = output_export,
         [OUTPUT_JSON] = output_json,
@@ -1081,8 +1093,9 @@ int show_journal_by_unit(
 
 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
         [OUTPUT_SHORT] = "short",
-        [OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
         [OUTPUT_SHORT_ISO] = "short-iso",
+        [OUTPUT_SHORT_PRECISE] = "short-precise",
+        [OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
         [OUTPUT_VERBOSE] = "verbose",
         [OUTPUT_EXPORT] = "export",
         [OUTPUT_JSON] = "json",
diff --git a/src/shared/output-mode.h b/src/shared/output-mode.h
index 4012889..9da789d 100644
--- a/src/shared/output-mode.h
+++ b/src/shared/output-mode.h
@@ -23,8 +23,9 @@
 
 typedef enum OutputMode {
         OUTPUT_SHORT,
-        OUTPUT_SHORT_MONOTONIC,
         OUTPUT_SHORT_ISO,
+        OUTPUT_SHORT_PRECISE,
+        OUTPUT_SHORT_MONOTONIC,
         OUTPUT_VERBOSE,
         OUTPUT_EXPORT,
         OUTPUT_JSON,
diff --git a/src/shared/time-util.c b/src/shared/time-util.c
index 9ee711a..860be61 100644
--- a/src/shared/time-util.c
+++ b/src/shared/time-util.c
@@ -168,6 +168,28 @@ char *format_timestamp(char *buf, size_t l, usec_t t) {
         return buf;
 }
 
+char *format_timestamp_us(char *buf, size_t l, usec_t t) {
+        struct tm tm;
+        time_t sec;
+
+        assert(buf);
+        assert(l > 0);
+
+        if (t <= 0)
+                return NULL;
+
+        sec = (time_t) (t / USEC_PER_SEC);
+        localtime_r(&sec, &tm);
+
+        if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm) <= 0)
+                return NULL;
+        snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", t % USEC_PER_SEC);
+        if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
+                return NULL;
+
+        return buf;
+}
+
 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
         usec_t n, d;
 
diff --git a/src/shared/time-util.h b/src/shared/time-util.h
index f27a006..7660fe1 100644
--- a/src/shared/time-util.h
+++ b/src/shared/time-util.h
@@ -73,6 +73,7 @@ usec_t timeval_load(const struct timeval *tv) _pure_;
 struct timeval *timeval_store(struct timeval *tv, usec_t u);
 
 char *format_timestamp(char *buf, size_t l, usec_t t);
+char *format_timestamp_us(char *buf, size_t l, usec_t t);
 char *format_timestamp_relative(char *buf, size_t l, usec_t t);
 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy);
 
-- 
1.8.2.562.g931e949



More information about the systemd-devel mailing list