[systemd-commits] 3 commits - TODO src/journal src/shared src/systemctl

Lennart Poettering lennart at kemper.freedesktop.org
Thu Jul 26 07:56:57 PDT 2012


 TODO                      |    4 ----
 src/journal/journalctl.c  |   22 ++++++++++++++++++++--
 src/journal/journald.c    |    2 +-
 src/shared/logs-show.c    |   38 ++++++++++++++++++++++++++++++--------
 src/shared/logs-show.h    |    9 +++++----
 src/systemctl/systemctl.c |    9 ++++++---
 6 files changed, 62 insertions(+), 22 deletions(-)

New commits:
commit c51742d02981196f992aef5f3d72799f22ee89e0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 26 16:51:58 2012 +0200

    journald: log driver messages at LOG_INFO

diff --git a/TODO b/TODO
index 618349a..3d075f1 100644
--- a/TODO
+++ b/TODO
@@ -43,16 +43,12 @@ Features:
 
 * logind: ignore inactive login screens when checking whether power key should be handled
 
-* introduce log_oom() or a similar call?
-
 * rename "userspace" to "core-os"
 
 * append ".service" to unit names without any suffix (https://bugs.freedesktop.org/show_bug.cgi?id=39386)
 
 * journalctl: add --priority switch
 
-* journalctl highlight lines based on priorities
-
 * systemctl: "Journal has been rotated since unit was started." message is misleading
 
 * syscall filter: add knowledge about compat syscalls
diff --git a/src/journal/journald.c b/src/journal/journald.c
index 5602e36..8cf8c78 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -721,7 +721,7 @@ static void driver_message(Server *s, sd_id128_t message_id, const char *format,
         assert(s);
         assert(format);
 
-        IOVEC_SET_STRING(iovec[n++], "PRIORITY=5");
+        IOVEC_SET_STRING(iovec[n++], "PRIORITY=6");
         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
 
         memcpy(buffer, "MESSAGE=", 8);

commit 498261871dfa2a930ec84b13a176e3bdc43aa212
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 26 16:50:35 2012 +0200

    journalctl: hightlight log lines by priority
    
    warn/notice = bright white
    < error = red

diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index c924afb..abcfabe 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -198,6 +198,21 @@ static int parse_argv(int argc, char *argv[]) {
         return 1;
 }
 
+static bool on_tty(void) {
+        static int t = -1;
+
+        /* Note that this is invoked relatively early, before we start
+         * the pager. That means the value we return reflects whether
+         * we originally were started on a tty, not if we currently
+         * are. But this is intended, since we want colour and so on
+         * when run in our own pager. */
+
+        if (_unlikely_(t < 0))
+                t = isatty(STDOUT_FILENO) > 0;
+
+        return t;
+}
+
 static int generate_new_id128(void) {
         sd_id128_t id;
         int r;
@@ -397,6 +412,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
+        on_tty();
         have_pager = !arg_no_pager && !arg_follow && pager_open();
 
         if (arg_output == OUTPUT_JSON) {
@@ -407,8 +423,10 @@ int main(int argc, char *argv[]) {
         for (;;) {
                 for (;;) {
                         sd_id128_t boot_id;
-                        int flags = (arg_show_all*OUTPUT_SHOW_ALL |
-                                     have_pager*OUTPUT_FULL_WIDTH);
+                        int flags =
+                                arg_show_all * OUTPUT_SHOW_ALL |
+                                have_pager * OUTPUT_FULL_WIDTH |
+                                on_tty() * OUTPUT_COLOR;
 
                         if (need_seek) {
                                 r = sd_journal_next(j);
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 375ca54..c72ebc1 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -82,13 +82,21 @@ static int output_short(sd_journal *j, unsigned line, unsigned n_columns,
         const void *data;
         size_t length;
         size_t n = 0;
-        char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = 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;
+        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;
+        int p = LOG_INFO;
+        const char *color_on = "", *color_off = "";
 
         assert(j);
 
         SD_JOURNAL_FOREACH_DATA(j, data, length) {
 
+                r = parse_field(data, length, "PRIORITY=", &priority, &priority_len);
+                if (r < 0)
+                        goto finish;
+                else if (r > 0)
+                        continue;
+
                 r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
                 if (r < 0)
                         goto finish;
@@ -141,6 +149,9 @@ static int output_short(sd_journal *j, unsigned line, unsigned n_columns,
                 goto finish;
         }
 
+        if (priority_len == 1 && *priority >= '0' && *priority <= '7')
+                p = *priority - '0';
+
         if (flags & OUTPUT_MONOTONIC_MODE) {
                 uint64_t t;
                 sd_id128_t boot_id;
@@ -219,23 +230,33 @@ static int output_short(sd_journal *j, unsigned line, unsigned n_columns,
                 n += fake_pid_len + 2;
         }
 
+        if (flags & OUTPUT_COLOR) {
+                if (p <= LOG_ERR) {
+                        color_on = ANSI_HIGHLIGHT_RED_ON;
+                        color_off = ANSI_HIGHLIGHT_OFF;
+                } else if (p <= LOG_NOTICE) {
+                        color_on = ANSI_HIGHLIGHT_ON;
+                        color_off = ANSI_HIGHLIGHT_OFF;
+                }
+        }
+
         if (flags & OUTPUT_SHOW_ALL)
-                printf(": %.*s\n", (int) message_len, message);
+                printf(": %s%.*s%s\n", color_on, (int) message_len, message, color_off);
         else if (!utf8_is_printable_n(message, message_len)) {
                 char bytes[FORMAT_BYTES_MAX];
                 printf(": [%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
         } else if ((flags & OUTPUT_FULL_WIDTH) ||
                    (message_len + n < n_columns))
-                printf(": %.*s\n", (int) message_len, message);
+                printf(": %s%.*s%s\n", color_on, (int) message_len, message, color_off);
         else if (n < n_columns && n_columns - n - 2 >= 3) {
                 char *e;
 
                 e = ellipsize_mem(message, message_len, n_columns - n - 2, 90);
 
                 if (!e)
-                        printf(": %.*s\n", (int) message_len, message);
+                        printf(": %s%.*s%s\n", color_on, (int) message_len, message, color_off);
                 else
-                        printf(": %s\n", e);
+                        printf(": %s%s%s\n", color_on, e, color_off);
 
                 free(e);
         } else
diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h
index 4e67432..58ff9e5 100644
--- a/src/shared/logs-show.h
+++ b/src/shared/logs-show.h
@@ -39,11 +39,12 @@ typedef enum OutputMode {
 } OutputMode;
 
 typedef enum OutputFlags {
-        OUTPUT_SHOW_ALL = 1 << 0,
+        OUTPUT_SHOW_ALL       = 1 << 0,
         OUTPUT_MONOTONIC_MODE = 1 << 1,
-        OUTPUT_FOLLOW = 1 << 2,
-        OUTPUT_WARN_CUTOFF = 1 << 3,
-        OUTPUT_FULL_WIDTH = 1 << 4,
+        OUTPUT_FOLLOW         = 1 << 2,
+        OUTPUT_WARN_CUTOFF    = 1 << 3,
+        OUTPUT_FULL_WIDTH     = 1 << 4,
+        OUTPUT_COLOR          = 1 << 5
 } OutputFlags;
 
 int output_journal(sd_journal *j, OutputMode mode, unsigned line,
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index ef8ab2d..e74f186 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -2584,9 +2584,12 @@ static void print_status_info(UnitStatusInfo *i) {
         }
 
         if (i->id && arg_transport != TRANSPORT_SSH) {
-                int flags = (arg_lines*OUTPUT_SHOW_ALL |
-                             arg_follow*OUTPUT_FOLLOW |
-                             !arg_quiet*OUTPUT_WARN_CUTOFF);
+                int flags =
+                        arg_lines * OUTPUT_SHOW_ALL |
+                        arg_follow * OUTPUT_FOLLOW |
+                        !arg_quiet * OUTPUT_WARN_CUTOFF |
+                        on_tty() * OUTPUT_COLOR;
+
                 printf("\n");
                 show_journal_by_unit(i->id, arg_output, 0,
                                      i->inactive_exit_timestamp_monotonic,

commit 46b0d922256b62bc8291951d9868c243ced80c9a
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 26 16:05:26 2012 +0200

    logs-show: fix OOM path

diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index edb5a9c..375ca54 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -50,11 +50,12 @@ static int parse_field(const void *data, size_t length, const char *field, char
 
         nl = length - fl;
         buf = malloc(nl+1);
-        memcpy(buf, (const char*) data + fl, nl);
-        ((char*)buf)[nl] = 0;
         if (!buf)
                 return log_oom();
 
+        memcpy(buf, (const char*) data + fl, nl);
+        ((char*)buf)[nl] = 0;
+
         free(*target);
         *target = buf;
         *target_size = nl;



More information about the systemd-commits mailing list