diff options
| author | Stefan Boberg <[email protected]> | 2026-03-09 12:03:25 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-03-09 12:03:25 +0100 |
| commit | 07649104761ee910b667adb2b865c4e2fd0979c9 (patch) | |
| tree | 16445986fd35ecb79e8f2417ae4426d1f2ea3c3d /src/zencore/include | |
| parent | Eliminate spdlog dependency (#773) (diff) | |
| download | zen-07649104761ee910b667adb2b865c4e2fd0979c9.tar.xz zen-07649104761ee910b667adb2b865c4e2fd0979c9.zip | |
added auto-detection logic for console colour output (#817)
Add auto-detection of colour support to `AnsicolourStdoutSink`.
**New `colorMode` enum** (`On`, `Off`, `Auto`) added to the header, accepted by the `AnsicolorStdoutSink` constructor. Defaults to `Auto`, so all existing call sites are unaffected.
**`Auto` mode detection logic** (in `IscolourTerminal()`):
1. **TTY check** -- if stdout is not a terminal, colour is disabled.
2. **`NO_COLOR`** -- respects the no-colour.org convention. If set, colour is disabled.
3. **`COLORTERM`** -- if set (e.g. `truecolour`, `24bit`), colour is enabled.
4. **`TERM`** -- rejects `dumb`; accepts known colour-capable terminals via substring match:
`alacritty`, `ansi`, `colour`, `console`, `cygwin`, `gnome`, `konsole`, `kterm`, `linux`, `msys`, `putty`, `rxvt`, `screen`, `tmux`, `vt100`, `vt102`, `xterm`.
Substring matching covers variants like `xterm-256color` and `rxvt-unicode`.
5. **Fallback** -- Windows defaults to colour enabled (modern console supports ANSI natively); other platforms default to disabled.
When colour is disabled, ANSI escape sequences are omitted entirely from the output.
NOTE: this doesn't currently apply to all paths which do logging in zen as they may be determining their colour output mode separately from `AnsicolorStdoutSink`.
Diffstat (limited to 'src/zencore/include')
| -rw-r--r-- | src/zencore/include/zencore/logging/ansicolorsink.h | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/zencore/include/zencore/logging/ansicolorsink.h b/src/zencore/include/zencore/logging/ansicolorsink.h index 9f859e8d7..5060a8393 100644 --- a/src/zencore/include/zencore/logging/ansicolorsink.h +++ b/src/zencore/include/zencore/logging/ansicolorsink.h @@ -8,10 +8,17 @@ namespace zen::logging { +enum class ColorMode +{ + On, + Off, + Auto +}; + class AnsiColorStdoutSink : public Sink { public: - AnsiColorStdoutSink(); + explicit AnsiColorStdoutSink(ColorMode Mode = ColorMode::Auto); ~AnsiColorStdoutSink() override; void Log(const LogMessage& Msg) override; |