log: respect the NO_COLOR environment variable

http://no-color.org/

Closes #1771
This commit is contained in:
Daniel Eklöf 2024-07-18 08:41:44 +02:00
parent 4f25e1ba9f
commit 36e4435bbf
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 10 additions and 1 deletions

8
log.c
View file

@ -40,7 +40,13 @@ log_init(enum log_colorize _colorize, bool _do_syslog,
[LOG_FACILITY_DAEMON] = LOG_DAEMON,
};
colorize = _colorize == LOG_COLORIZE_ALWAYS || (_colorize == LOG_COLORIZE_AUTO && isatty(STDERR_FILENO));
/* Don't use colors if NO_COLOR is defined and not empty */
const char *no_color_str = getenv("NO_COLOR");
const bool no_color = no_color_str != NULL && no_color_str[0] != '\0';
colorize = _colorize == LOG_COLORIZE_ALWAYS
|| (_colorize == LOG_COLORIZE_AUTO
&& !no_color && isatty(STDERR_FILENO));
do_syslog = _do_syslog;
log_level = _log_level;