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

View file

@ -65,11 +65,14 @@
* Support for SGR 21 (double underline).
* Support for `XTPUSHCOLORS`, `XTPOPCOLORS` and `XTREPORTCOLORS`,
i.e. color palette stack ([#856][856]).
* Log output now respects the [`NO_COLOR`](http://no-color.org/)
environment variable ([#1771][1771]).
[1707]: https://codeberg.org/dnkl/foot/issues/1707
[1738]: https://codeberg.org/dnkl/foot/issues/1738
[828]: https://codeberg.org/dnkl/foot/issues/828
[856]: https://codeberg.org/dnkl/foot/issues/856
[1771]: https://codeberg.org/dnkl/foot/issues/1771
### Changed

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;