chiark / gitweb /
journalctl: add ”short-iso” output format with verbose ISO8601 timestamps
[elogind.git] / src / shared / logs-show.c
index 91b2bec1599d0852322af14dfc00572c477e7289..bd7363aa82a2888f6efa183f4423ac3fbe454fb4 100644 (file)
@@ -273,9 +273,14 @@ static int output_short(
                 }
 
                 t = (time_t) (x / USEC_PER_SEC);
-                if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
+                if (mode == OUTPUT_SHORT_ISO)
+                        r = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", localtime_r(&t, &tm));
+                else
+                        r = strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm));
+
+                if (r <= 0) {
                         log_error("Failed to format time.");
-                        return r;
+                        return -EINVAL;
                 }
 
                 fputs(buf, f);
@@ -357,6 +362,8 @@ static int output_verbose(
         JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
                 const char *c;
                 int fieldlen;
+                const char *on = "", *off = "";
+
                 c = memchr(data, '=', length);
                 if (!c) {
                         log_error("Invalid field.");
@@ -364,16 +371,25 @@ static int output_verbose(
                 }
                 fieldlen = c - (const char*) data;
 
-                if ((flags & OUTPUT_SHOW_ALL) || (length < PRINT_THRESHOLD && utf8_is_printable(data, length))) {
-                        fprintf(f, "    %.*s=", fieldlen, (const char*)data);
+                if (flags & OUTPUT_COLOR && startswith(data, "MESSAGE=")) {
+                        on = ANSI_HIGHLIGHT_ON;
+                        off = ANSI_HIGHLIGHT_OFF;
+                }
+
+                if (flags & OUTPUT_SHOW_ALL ||
+                    (((length < PRINT_THRESHOLD) || flags & OUTPUT_FULL_WIDTH) && utf8_is_printable(data, length))) {
+                        fprintf(f, "    %s%.*s=", on, fieldlen, (const char*)data);
                         print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, c + 1, length - fieldlen - 1);
+                        fputs(off, f);
                 } else {
                         char bytes[FORMAT_BYTES_MAX];
 
-                        fprintf(f, "    %.*s=[%s blob data]\n",
+                        fprintf(f, "    %s%.*s=[%s blob data]%s\n",
+                                on,
                                 (int) (c - (const char*) data),
                                 (const char*) data,
-                                format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1));
+                                format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1),
+                                off);
                 }
         }
 
@@ -787,6 +803,7 @@ static int (*output_funcs[_OUTPUT_MODE_MAX])(
 
         [OUTPUT_SHORT] = output_short,
         [OUTPUT_SHORT_MONOTONIC] = output_short,
+        [OUTPUT_SHORT_ISO] = output_short,
         [OUTPUT_VERBOSE] = output_verbose,
         [OUTPUT_EXPORT] = output_export,
         [OUTPUT_JSON] = output_json,
@@ -1048,7 +1065,12 @@ int show_journal_by_unit(
         if (r < 0)
                 return r;
 
-        log_debug("Journal filter: %s", journal_make_match_string(j));
+        if (_unlikely_(log_get_max_level() >= LOG_PRI(LOG_DEBUG))) {
+                _cleanup_free_ char *filter;
+
+                filter = journal_make_match_string(j);
+                log_debug("Journal filter: %s", filter);
+        }
 
         r = show_journal(f, j, mode, n_columns, not_before, how_many, flags);
         if (r < 0)
@@ -1060,6 +1082,7 @@ 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_VERBOSE] = "verbose",
         [OUTPUT_EXPORT] = "export",
         [OUTPUT_JSON] = "json",