From: Zbigniew Jędrzejewski-Szmek Date: Fri, 26 Apr 2019 10:27:33 +0000 (+0200) Subject: basic/log: use colors to highlight messages like journalctl X-Git-Tag: v243-rc1~467^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=37b8d2f699ac6c80d653e94bad638122a6a1b3ef;p=thirdparty%2Fsystemd.git basic/log: use colors to highlight messages like journalctl --- diff --git a/src/basic/log.c b/src/basic/log.c index ea252c41302..a81c350ab4d 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -336,7 +336,7 @@ static int write_to_console( char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2]; struct iovec iovec[6] = {}; - bool highlight; + const char *on = NULL, *off = NULL; size_t n = 0; if (console_fd < 0) @@ -347,18 +347,19 @@ static int write_to_console( iovec[n++] = IOVEC_MAKE_STRING(prefix); } - highlight = LOG_PRI(level) <= LOG_ERR && show_color; + if (show_color) + get_log_colors(LOG_PRI(level), &on, &off, NULL); if (show_location) { (void) snprintf(location, sizeof location, "(%s:%i) ", file, line); iovec[n++] = IOVEC_MAKE_STRING(location); } - if (highlight) - iovec[n++] = IOVEC_MAKE_STRING(ANSI_HIGHLIGHT_RED); + if (on) + iovec[n++] = IOVEC_MAKE_STRING(on); iovec[n++] = IOVEC_MAKE_STRING(buffer); - if (highlight) - iovec[n++] = IOVEC_MAKE_STRING(ANSI_NORMAL); + if (off) + iovec[n++] = IOVEC_MAKE_STRING(off); iovec[n++] = IOVEC_MAKE_STRING("\n"); if (writev(console_fd, iovec, n) < 0) { diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index b692c52e590..4676ce2a806 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -1310,3 +1310,33 @@ int vt_release(int fd, bool restore) { return 0; } + +void get_log_colors(int priority, const char **on, const char **off, const char **highlight) { + /* Note that this will initialize output variables only when there's something to output. + * The caller must pre-initalize to "" or NULL as appropriate. */ + + if (priority <= LOG_ERR) { + if (on) + *on = ANSI_HIGHLIGHT_RED; + if (off) + *off = ANSI_NORMAL; + if (highlight) + *highlight = ANSI_HIGHLIGHT; + + } else if (priority <= LOG_NOTICE) { + if (on) + *on = ANSI_HIGHLIGHT; + if (off) + *off = ANSI_NORMAL; + if (highlight) + *highlight = ANSI_HIGHLIGHT_RED; + + } else if (priority >= LOG_DEBUG) { + if (on) + *on = ANSI_GREY; + if (off) + *off = ANSI_NORMAL; + if (highlight) + *highlight = ANSI_HIGHLIGHT_RED; + } +} diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index c885e0a2d1a..f3e785e7ba3 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "macro.h" @@ -158,3 +159,5 @@ int vt_default_utf8(void); int vt_reset_keyboard(int fd); int vt_restore(int fd); int vt_release(int fd, bool restore_vt); + +void get_log_colors(int priority, const char **on, const char **off, const char **highlight); diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 5fb736f6330..bdde6c11e38 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -163,21 +163,8 @@ static bool print_multiline( bool ellipsized = false; int line = 0; - if (flags & OUTPUT_COLOR) { - if (priority <= LOG_ERR) { - color_on = ANSI_HIGHLIGHT_RED; - color_off = ANSI_NORMAL; - highlight_on = ANSI_HIGHLIGHT; - } else if (priority <= LOG_NOTICE) { - color_on = ANSI_HIGHLIGHT; - color_off = ANSI_NORMAL; - highlight_on = ANSI_HIGHLIGHT_RED; - } else if (priority >= LOG_DEBUG) { - color_on = ANSI_GREY; - color_off = ANSI_NORMAL; - highlight_on = ANSI_HIGHLIGHT_RED; - } - } + if (flags & OUTPUT_COLOR) + get_log_colors(priority, &color_on, &color_off, &highlight_on); /* A special case: make sure that we print a newline when the message is empty. */