log: respect the NO_COLOR environment variable

http://no-color.org/
This commit is contained in:
Daniel Eklöf 2024-07-18 08:31:46 +02:00
parent 739dc30323
commit 1a323c6d21
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 11 additions and 3 deletions

View file

@ -16,6 +16,8 @@
### Added
* environment variable substitution in config files ([#96][96]).
* Log output now respects the [`NO_COLOR`](http://no-color.org/)
environment variable.
[96]: https://codeberg.org/dnkl/yambar/issues/96

12
log.c
View file

@ -39,9 +39,15 @@ log_init(enum log_colorize _colorize, bool _do_syslog, enum log_facility syslog_
[LOG_FACILITY_DAEMON] = LOG_DAEMON,
};
colorize = _colorize == LOG_COLORIZE_NEVER ? false
: _colorize == LOG_COLORIZE_ALWAYS ? true
: 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_NEVER
? false
: _colorize == LOG_COLORIZE_ALWAYS
? true
: !no_color && isatty(STDERR_FILENO);
do_syslog = _do_syslog;
log_level = _log_level;