[systemd-devel] [PATCH 1/3] Adapt interface in log-show.c (output_journal)

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Mon Jul 16 22:35:06 PDT 2012


In preparation for adding more output switches, convert a series of
flags arguments into one flag variable.
---
This is a three patch series, patch 3/3 contains the real change, and 1/2 + 2/2
are preparation. Without the cleanup, 3/3 would add a fourth bool argument
to a function, which is just not nice.

The goal is to be able to view full width output in journalctl when running under
a pager, and to leave things unchanged otherwise.

 src/journal/journalctl.c |  3 ++-
 src/shared/logs-show.c   | 56 ++++++++++++++++++++++++++++++------------------
 src/shared/logs-show.h   |  8 ++++++-
 3 files changed, 44 insertions(+), 23 deletions(-)

diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 65b3bd5..a0e3b39 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -419,7 +419,8 @@ int main(int argc, char *argv[]) {
 
                         line ++;
 
-                        r = output_journal(j, arg_output, line, 0, arg_show_all);
+                        r = output_journal(j, arg_output, line, 0,
+                                           arg_show_all ? OUTPUT_SHOW_ALL : 0);
                         if (r < 0)
                                 goto finish;
 
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 426a9d6..af9bcbd 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -77,7 +77,8 @@ static bool shall_print(bool show_all, char *p, size_t l) {
         return true;
 }
 
-static int output_short(sd_journal *j, unsigned line, unsigned n_columns, bool show_all, bool monotonic_mode) {
+static int output_short(sd_journal *j, unsigned line, unsigned n_columns,
+                        OutputFlags flags) {
         int r;
         const void *data;
         size_t length;
@@ -141,7 +142,7 @@ static int output_short(sd_journal *j, unsigned line, unsigned n_columns, bool s
                 goto finish;
         }
 
-        if (monotonic_mode) {
+        if (flags & OUTPUT_MONOTONIC_MODE) {
                 uint64_t t;
                 sd_id128_t boot_id;
 
@@ -193,29 +194,33 @@ static int output_short(sd_journal *j, unsigned line, unsigned n_columns, bool s
                 n += strlen(buf);
         }
 
-        if (hostname && shall_print(show_all, hostname, hostname_len)) {
+        if (hostname && shall_print(flags & OUTPUT_SHOW_ALL,
+                                    hostname, hostname_len)) {
                 printf(" %.*s", (int) hostname_len, hostname);
                 n += hostname_len + 1;
         }
 
-        if (identifier && shall_print(show_all, identifier, identifier_len)) {
+        if (identifier && shall_print(flags & OUTPUT_SHOW_ALL,
+                                      identifier, identifier_len)) {
                 printf(" %.*s", (int) identifier_len, identifier);
                 n += identifier_len + 1;
-        } else if (comm && shall_print(show_all, comm, comm_len)) {
+        } else if (comm && shall_print(flags & OUTPUT_SHOW_ALL,
+                                       comm, comm_len)) {
                 printf(" %.*s", (int) comm_len, comm);
                 n += comm_len + 1;
         } else
                 putchar(' ');
 
-        if (pid && shall_print(show_all, pid, pid_len)) {
+        if (pid && shall_print(flags & OUTPUT_SHOW_ALL, pid, pid_len)) {
                 printf("[%.*s]", (int) pid_len, pid);
                 n += pid_len + 2;
-        } else if (fake_pid && shall_print(show_all, fake_pid, fake_pid_len)) {
+        } else if (fake_pid && shall_print(flags & OUTPUT_SHOW_ALL,
+                                           fake_pid, fake_pid_len)) {
                 printf("[%.*s]", (int) fake_pid_len, fake_pid);
                 n += fake_pid_len + 2;
         }
 
-        if (show_all)
+        if (flags & OUTPUT_SHOW_ALL)
                 printf(": %.*s\n", (int) message_len, message);
         else if (!utf8_is_printable_n(message, message_len)) {
                 char bytes[FORMAT_BYTES_MAX];
@@ -251,15 +256,18 @@ finish:
         return r;
 }
 
-static int output_short_realtime(sd_journal *j, unsigned line, unsigned n_columns, bool show_all) {
-        return output_short(j, line, n_columns, show_all, false);
+static int output_short_realtime(sd_journal *j, unsigned line,
+                                 unsigned n_columns, OutputFlags flags) {
+        return output_short(j, line, n_columns, flags & ~OUTPUT_MONOTONIC_MODE);
 }
 
-static int output_short_monotonic(sd_journal *j, unsigned line, unsigned n_columns, bool show_all) {
-        return output_short(j, line, n_columns, show_all, true);
+static int output_short_monotonic(sd_journal *j, unsigned line,
+                                  unsigned n_columns, OutputFlags flags) {
+        return output_short(j, line, n_columns, flags | OUTPUT_MONOTONIC_MODE);
 }
 
-static int output_verbose(sd_journal *j, unsigned line, unsigned n_columns, bool show_all) {
+static int output_verbose(sd_journal *j, unsigned line,
+                          unsigned n_columns, OutputFlags flags) {
         const void *data;
         size_t length;
         char *cursor;
@@ -288,7 +296,7 @@ static int output_verbose(sd_journal *j, unsigned line, unsigned n_columns, bool
         free(cursor);
 
         SD_JOURNAL_FOREACH_DATA(j, data, length) {
-                if (!show_all && (length > PRINT_THRESHOLD ||
+                if (!(flags & OUTPUT_SHOW_ALL) && (length > PRINT_THRESHOLD ||
                                   !utf8_is_printable_n(data, length))) {
                         const char *c;
                         char bytes[FORMAT_BYTES_MAX];
@@ -310,7 +318,8 @@ static int output_verbose(sd_journal *j, unsigned line, unsigned n_columns, bool
         return 0;
 }
 
-static int output_export(sd_journal *j, unsigned line, unsigned n_columns, bool show_all) {
+static int output_export(sd_journal *j, unsigned line,
+                         unsigned n_columns, OutputFlags flags) {
         sd_id128_t boot_id;
         char sid[33];
         int r;
@@ -421,7 +430,8 @@ static void json_escape(const char* p, size_t l) {
         }
 }
 
-static int output_json(sd_journal *j, unsigned line, unsigned n_columns, bool show_all) {
+static int output_json(sd_journal *j, unsigned line,
+                       unsigned n_columns, OutputFlags flags) {
         uint64_t realtime, monotonic;
         char *cursor;
         const void *data;
@@ -494,7 +504,8 @@ static int output_json(sd_journal *j, unsigned line, unsigned n_columns, bool sh
         return 0;
 }
 
-static int output_cat(sd_journal *j, unsigned line, unsigned n_columns, bool show_all) {
+static int output_cat(sd_journal *j, unsigned line,
+                      unsigned n_columns, OutputFlags flags) {
         const void *data;
         size_t l;
         int r;
@@ -515,7 +526,8 @@ static int output_cat(sd_journal *j, unsigned line, unsigned n_columns, bool sho
         return 0;
 }
 
-static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, unsigned n_columns, bool show_all) = {
+static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line,
+                                             unsigned n_columns, OutputFlags flags) = {
         [OUTPUT_SHORT] = output_short_realtime,
         [OUTPUT_SHORT_MONOTONIC] = output_short_monotonic,
         [OUTPUT_VERBOSE] = output_verbose,
@@ -524,14 +536,15 @@ static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, unsign
         [OUTPUT_CAT] = output_cat
 };
 
-int output_journal(sd_journal *j, OutputMode mode, unsigned line, unsigned n_columns, bool show_all) {
+int output_journal(sd_journal *j, OutputMode mode, unsigned line,
+                   unsigned n_columns, OutputFlags flags) {
         assert(mode >= 0);
         assert(mode < _OUTPUT_MODE_MAX);
 
         if (n_columns <= 0)
                 n_columns = columns();
 
-        return output_funcs[mode](j, line, n_columns, show_all);
+        return output_funcs[mode](j, line, n_columns, flags);
 }
 
 int show_journal_by_unit(
@@ -620,7 +633,8 @@ int show_journal_by_unit(
 
                         line ++;
 
-                        r = output_journal(j, mode, line, n_columns, show_all);
+                        r = output_journal(j, mode, line, n_columns,
+                                           show_all ? OUTPUT_SHOW_ALL : 0);
                         if (r < 0)
                                 goto finish;
                 }
diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h
index f8a9d40..beade06 100644
--- a/src/shared/logs-show.h
+++ b/src/shared/logs-show.h
@@ -39,7 +39,13 @@ typedef enum OutputMode {
         _OUTPUT_MODE_INVALID = -1
 } OutputMode;
 
-int output_journal(sd_journal *j, OutputMode mode, unsigned line, unsigned n_columns, bool show_all);
+typedef enum OutputFlags {
+        OUTPUT_SHOW_ALL = 1 << 0,
+        OUTPUT_MONOTONIC_MODE = 1 << 1,
+} OutputFlags;
+
+int output_journal(sd_journal *j, OutputMode mode, unsigned line,
+                   unsigned n_columns, OutputFlags flags);
 
 int show_journal_by_unit(
                 const char *unit,
-- 
1.7.11.1.282.g511ba53



More information about the systemd-devel mailing list