build: add some coloring to --version output

Make it easier to discern enabled and disabled build options.
This commit is contained in:
Lennart Poettering
2023-01-23 16:23:45 +01:00
committed by Zbigniew Jędrzejewski-Szmek
parent 53fd537472
commit 4453ebe4db
2 changed files with 46 additions and 1 deletions

View 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;
}

View 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_;