From c89bbfe6012275afe500c0c862329f37c4021d73 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 18 Jul 2024 08:39:53 +0200 Subject: log: respect the NO_COLOR environment variable http://no-color.org/ --- CHANGELOG.md | 2 ++ log.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 210d363..f2290cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ * Nanosvg updated to 93ce879dc4c04a3ef1758428ec80083c38610b1f * JPEG XL support ([#14][14]) +* Log output now respects the [`NO_COLOR`](http://no-color.org/) + environment variable. [14]: https://codeberg.org/dnkl/wbg/pulls/14 diff --git a/log.c b/log.c index c5d9093..950e641 100644 --- a/log.c +++ b/log.c @@ -30,7 +30,13 @@ log_init(enum log_colorize _colorize, bool _do_syslog, [LOG_CLASS_DEBUG] = LOG_DEBUG, }; - 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; if (do_syslog) { -- cgit v1.2.3