]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
build: add some coloring to --version output
authorLennart Poettering <lennart@poettering.net>
Mon, 23 Jan 2023 15:23:45 +0000 (16:23 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 23 Jan 2023 20:38:51 +0000 (21:38 +0100)
Make it easier to discern enabled and disabled build options.

src/basic/build.c
src/basic/log.h

index 0b7a44f1f6f83bbb8985fa0878638297192585bd..aaf7d2ceeb80038495fa40515309ab9fb157f5a9 100644 (file)
@@ -2,8 +2,12 @@
 
 #include <stdio.h>
 
+#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;
 }
index fcb8fcfb69aeac17d1ea209f4d123d44319ed76a..f73d4c41545c07c6cad1345ee562672043f9c098 100644 (file)
@@ -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_;