From: Lennart Poettering Date: Mon, 23 Jan 2023 15:23:45 +0000 (+0100) Subject: build: add some coloring to --version output X-Git-Tag: v253-rc1~27 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4453ebe4db0511d25bed1040930ea6430c1bed91;p=thirdparty%2Fsystemd.git build: add some coloring to --version output Make it easier to discern enabled and disabled build options. --- diff --git a/src/basic/build.c b/src/basic/build.c index 0b7a44f1f6f..aaf7d2ceeb8 100644 --- a/src/basic/build.c +++ b/src/basic/build.c @@ -2,8 +2,12 @@ #include +#include "alloc-util.h" #include "build.h" +#include "extract-word.h" #include "macro.h" +#include "string-util.h" +#include "terminal-util.h" #include "version.h" const char* const systemd_features = @@ -231,8 +235,48 @@ const char* const systemd_features = " default-hierarchy=" DEFAULT_HIERARCHY_NAME ; +static char *systemd_features_with_color(void) { + const char *p = systemd_features; + _cleanup_free_ char *ret = NULL; + int r; + + for (;;) { + _cleanup_free_ char *word = NULL; + char *q; + + r = extract_first_word(&p, &word, NULL, 0); + if (r < 0) { + log_warning_errno(r, "Cannot split features string, ignoring: %m"); + return NULL; + } + if (r == 0) + return TAKE_PTR(ret); + + if (ret && !strextend(&ret, " ")) { + log_oom_warning(); + return NULL; + } + + if (word[0] == '+') + q = strextend(&ret, ANSI_HIGHLIGHT_GREEN, CHAR_TO_STR(word[0]), ANSI_GREEN, word+1, ANSI_NORMAL); + else if (word[0] == '-') + q = strextend(&ret, ANSI_HIGHLIGHT_RED, CHAR_TO_STR(word[0]), ANSI_RED, word+1, ANSI_NORMAL); + else + q = strextend(&ret, word); + if (!q) { + log_oom_warning(); + return NULL; + } + } +} + int version(void) { + _cleanup_free_ char *b = NULL; + + if (colors_enabled()) + b = systemd_features_with_color(); + printf("systemd " STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")\n%s\n", - systemd_features); + b ?: systemd_features); return 0; } diff --git a/src/basic/log.h b/src/basic/log.h index fcb8fcfb69a..f73d4c41545 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -298,6 +298,7 @@ int log_emergency_level(void); #define log_oom() log_oom_internal(LOG_ERR, PROJECT_FILE, __LINE__, __func__) #define log_oom_debug() log_oom_internal(LOG_DEBUG, PROJECT_FILE, __LINE__, __func__) +#define log_oom_warning() log_oom_internal(LOG_WARNING, PROJECT_FILE, __LINE__, __func__) bool log_on_console(void) _pure_;