[systemd-devel] [PATCH] journalctl: add --iso-dates for long timestamps

Tomasz Torcz tomek at pipebreaker.pl
Wed Jul 17 08:15:05 PDT 2013


Add possibility have "short" output with long format ISO 8601 timestamps,
like "2008-05-28T14:14:46.316223-04:00".
---
 man/journalctl.xml       |  8 ++++++++
 src/journal/journalctl.c | 11 ++++++++++-
 src/shared/logs-show.c   |  8 +++++++-
 src/shared/output-mode.h |  3 ++-
 4 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/man/journalctl.xml b/man/journalctl.xml
index 3e03c45..8c9a796 100644
--- a/man/journalctl.xml
+++ b/man/journalctl.xml
@@ -170,6 +170,14 @@
                         </varlistentry>
 
                         <varlistentry>
+                                <term><option>--iso-dates</option></term>
+
+                                <listitem><para>In "short" output mode,
+                                show timestamps in verbose, ISO 8601 format.
+                                </para></listitem>
+                        </varlistentry>
+
+                        <varlistentry>
                                 <term><option>-f</option></term>
                                 <term><option>--follow</option></term>
 
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 7099706..3709319 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -65,6 +65,7 @@ static bool arg_pager_end = false;
 static bool arg_follow = false;
 static bool arg_full = false;
 static bool arg_all = false;
+static bool arg_iso_dates = false;
 static bool arg_no_pager = false;
 static int arg_lines = -1;
 static bool arg_no_tail = false;
@@ -138,6 +139,7 @@ static int help(void) {
                "  -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"
+               "     --iso-dates           Show ISO 8601 full timestamps in short output mode\n"
                "  -q --quiet               Don't show privilege warning\n"
                "     --no-pager            Do not pipe output into a pager\n"
                "  -m --merge               Show entries from all available journals\n"
@@ -194,6 +196,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_DUMP_CATALOG,
                 ARG_UPDATE_CATALOG,
                 ARG_FORCE,
+                ARG_ISO_DATES,
         };
 
         static const struct option options[] = {
@@ -239,6 +242,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "dump-catalog",   no_argument,       NULL, ARG_DUMP_CATALOG   },
                 { "update-catalog", no_argument,       NULL, ARG_UPDATE_CATALOG },
                 { "reverse",        no_argument,       NULL, 'r'                },
+                { "iso-dates",      no_argument,       NULL, ARG_ISO_DATES      },
                 { NULL,             0,                 NULL, 0                  }
         };
 
@@ -300,6 +304,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_all = true;
                         break;
 
+                case ARG_ISO_DATES:
+                        arg_iso_dates = true;
+                        break;
+
                 case 'n':
                         if (optarg) {
                                 r = safe_atoi(optarg, &arg_lines);
@@ -1605,7 +1613,8 @@ int main(int argc, char *argv[]) {
                                 arg_all * OUTPUT_SHOW_ALL |
                                 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
                                 on_tty() * OUTPUT_COLOR |
-                                arg_catalog * OUTPUT_CATALOG;
+                                arg_catalog * OUTPUT_CATALOG |
+                                arg_iso_dates * OUTPUT_ISO_DATES;
 
                         r = output_journal(stdout, j, arg_output, 0, flags);
                         need_seek = true;
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index ea47468..1753ca8 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -258,6 +258,7 @@ static int output_short(
                 uint64_t x;
                 time_t t;
                 struct tm tm;
+                const char *timestamp_format;
 
                 r = -ENOENT;
 
@@ -272,8 +273,13 @@ static int output_short(
                         return r;
                 }
 
+		if (flags & OUTPUT_ISO_DATES)
+			timestamp_format = "%Y-%m-%dT%H:%M:%S%z";
+		else
+			timestamp_format = "%b %d %H:%M:%S";
+
                 t = (time_t) (x / USEC_PER_SEC);
-                if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
+                if (strftime(buf, sizeof(buf), timestamp_format, localtime_r(&t, &tm)) <= 0) {
                         log_error("Failed to format time.");
                         return r;
                 }
diff --git a/src/shared/output-mode.h b/src/shared/output-mode.h
index 0efd430..0b860e5 100644
--- a/src/shared/output-mode.h
+++ b/src/shared/output-mode.h
@@ -40,5 +40,6 @@ typedef enum OutputFlags {
         OUTPUT_WARN_CUTOFF    = 1 << 2,
         OUTPUT_FULL_WIDTH     = 1 << 3,
         OUTPUT_COLOR          = 1 << 4,
-        OUTPUT_CATALOG        = 1 << 5
+        OUTPUT_CATALOG        = 1 << 5,
+        OUTPUT_ISO_DATES      = 1 << 6
 } OutputFlags;
-- 
1.8.3.1



More information about the systemd-devel mailing list