]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: add with-unit mode 8245/head
authorLuca Boccassi <bluca@debian.org>
Tue, 22 May 2018 11:22:00 +0000 (12:22 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 25 May 2018 12:45:34 +0000 (14:45 +0200)
When dealing with a large number of template instances, for example
when launching daemons per VRF, it is hard for operators to correlate
log lines to arguments.
Add a new with-unit mode which, if available, prefixes unit and user
unit names when displaying its log messages instead of the syslog
identifier. It will also use the full timestamp with timezones, like
the short-full mode.

man/journalctl.xml
shell-completion/bash/journalctl
shell-completion/zsh/_sd_outputmodes
src/journal/journalctl.c
src/shared/logs-show.c
src/shared/output-mode.c
src/shared/output-mode.h

index 12d6a80c573375b8f6afec685b12a89437538912..b43e266873d6bc9331d6fd7f0cc7a11c12e79cf5 100644 (file)
                 not even a timestamp.</para>
               </listitem>
             </varlistentry>
+
+            <varlistentry>
+              <term>
+                <option>with-unit</option>
+              </term>
+              <listitem>
+                <para>similar to short-full, but prefixes the unit and
+                user unit names instead of the traditional syslog
+                identifier. Useful when using templated instances, as it
+                will include the arguments in the unit names.</para>
+              </listitem>
+            </varlistentry>
           </variablelist>
         </listitem>
       </varlistentry>
index d2b9a0470603520e791d275e1727d29fdca5d196..8fcb42ad7b16cc0f88f1a6046eb1d80d4600bf81 100644 (file)
@@ -66,7 +66,7 @@ _journalctl() {
                                 compopt -o filenames
                         ;;
                         --output|-o)
-                                comps='short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json json-pretty json-sse cat'
+                                comps='short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json json-pretty json-sse cat with-unit'
                         ;;
                         --field|-F)
                                 comps=$(journalctl --fields | sort 2>/dev/null)
index 3b8850b3f0a285d8fe35eab78e25a919e980b2b7..70ff7233afc2ac9bf54a07fb766697d1ae0489e1 100644 (file)
@@ -2,5 +2,5 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
 local -a _output_opts
-_output_opts=(short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json json-pretty json-sse cat)
+_output_opts=(short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json json-pretty json-sse cat with-unit)
 _describe -t output 'output mode' _output_opts || compadd "$@"
index 92cf2eb1228f95671659205a734beb5a953bd428..26222ea28d4ca5213b5687bba8e37004f3e3ff7c 100644 (file)
@@ -334,7 +334,7 @@ static void help(void) {
                "  -o --output=STRING         Change journal output mode (short, short-precise,\n"
                "                               short-iso, short-iso-precise, short-full,\n"
                "                               short-monotonic, short-unix, verbose, export,\n"
-               "                               json, json-pretty, json-sse, cat)\n"
+               "                               json, json-pretty, json-sse, cat, with-unit)\n"
                "     --output-fields=LIST    Select fields to print in verbose/export/json modes\n"
                "     --utc                   Express time in Coordinated Universal Time (UTC)\n"
                "  -x --catalog               Add message explanations where available\n"
index 26a1513a052bfce88399ef5526a5a1cddc1090b7..47becbf37c31713582f9640d4605324acc64bbec 100644 (file)
@@ -306,7 +306,7 @@ static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, Ou
                 return -EINVAL;
         }
 
-        if (mode == OUTPUT_SHORT_FULL) {
+        if (IN_SET(mode, OUTPUT_SHORT_FULL, OUTPUT_WITH_UNIT)) {
                 const char *k;
 
                 if (flags & OUTPUT_UTC)
@@ -391,8 +391,8 @@ static int output_short(
         const void *data;
         size_t length;
         size_t n = 0;
-        _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL;
-        size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, realtime_len = 0, monotonic_len = 0, priority_len = 0;
+        _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL, *unit = NULL, *user_unit = NULL;
+        size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, realtime_len = 0, monotonic_len = 0, priority_len = 0, unit_len = 0, user_unit_len = 0;
         int p = LOG_INFO;
         bool ellipsized = false;
         const ParseFieldVec fields[] = {
@@ -405,6 +405,8 @@ static int output_short(
                 PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier, &identifier_len),
                 PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len),
                 PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len),
+                PARSE_FIELD_VEC_ENTRY("_SYSTEMD_UNIT=", &unit, &unit_len),
+                PARSE_FIELD_VEC_ENTRY("_SYSTEMD_USER_UNIT=", &user_unit, &user_unit_len),
         };
         size_t highlight_shifted[] = {highlight ? highlight[0] : 0, highlight ? highlight[1] : 0};
 
@@ -462,7 +464,19 @@ static int output_short(
                 n += hostname_len + 1;
         }
 
-        if (identifier && shall_print(identifier, identifier_len, flags)) {
+        if (mode == OUTPUT_WITH_UNIT && ((unit && shall_print(unit, unit_len, flags)) || (user_unit && shall_print(user_unit, user_unit_len, flags)))) {
+                if (unit) {
+                        fprintf(f, " %.*s", (int) unit_len, unit);
+                        n += unit_len + 1;
+                }
+                if (user_unit) {
+                        if (unit)
+                                fprintf(f, "/%.*s", (int) user_unit_len, user_unit);
+                        else
+                                fprintf(f, " %.*s", (int) user_unit_len, user_unit);
+                        n += unit_len + 1;
+                }
+        } else if (identifier && shall_print(identifier, identifier_len, flags)) {
                 fprintf(f, " %.*s", (int) identifier_len, identifier);
                 n += identifier_len + 1;
         } else if (comm && shall_print(comm, comm_len, flags)) {
@@ -1053,7 +1067,8 @@ static int (*output_funcs[_OUTPUT_MODE_MAX])(
         [OUTPUT_JSON] = output_json,
         [OUTPUT_JSON_PRETTY] = output_json,
         [OUTPUT_JSON_SSE] = output_json,
-        [OUTPUT_CAT] = output_cat
+        [OUTPUT_CAT] = output_cat,
+        [OUTPUT_WITH_UNIT] = output_short,
 };
 
 int output_journal(
index 89be1d68a1a17d4bfe9c13ea15549f7ac7076f4a..1bb161794a73f5df783958a9252ef93ec1fa9159 100644 (file)
@@ -21,7 +21,8 @@ static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
         [OUTPUT_JSON] = "json",
         [OUTPUT_JSON_PRETTY] = "json-pretty",
         [OUTPUT_JSON_SSE] = "json-sse",
-        [OUTPUT_CAT] = "cat"
+        [OUTPUT_CAT] = "cat",
+        [OUTPUT_WITH_UNIT] = "with-unit",
 };
 
 DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);
index 3aa7b295e355676f9d75d44b8191bdd5922b036f..a44d5286756a5336ea8a2fc61a5e30f5707f8eaa 100644 (file)
@@ -23,6 +23,7 @@ typedef enum OutputMode {
         OUTPUT_JSON_PRETTY,
         OUTPUT_JSON_SSE,
         OUTPUT_CAT,
+        OUTPUT_WITH_UNIT,
         _OUTPUT_MODE_MAX,
         _OUTPUT_MODE_INVALID = -1
 } OutputMode;