Add GZ_CONSOLE_COLOR env variable to control terminal color output#792
Add GZ_CONSOLE_COLOR env variable to control terminal color output#792BhuvanB404 wants to merge 2 commits into
Conversation
6d78a70 to
2a6538a
Compare
There was a problem hiding this comment.
Pull request overview
Adds a new environment variable (GZ_CONSOLE_COLOR) to control whether console log output includes ANSI color codes, addressing the need to disable unreadable escape sequences in CI consoles (issue #611).
Changes:
- Read
GZ_CONSOLE_COLORduringgz::common::Console::Initand set spdlog console color mode toalways/never. - Emit a warning when
GZ_CONSOLE_COLORis set to an unrecognized value. - Document
GZ_CONSOLE_COLORin the repository README.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Console.cc | Parses GZ_CONSOLE_COLOR and configures console sink color mode accordingly. |
| README.md | Documents how to use GZ_CONSOLE_COLOR to enable/disable color output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (value == "yes") | ||
| { | ||
| Console::Root().SetConsoleColorMode(spdlog::color_mode::always); | ||
| } | ||
| else if (value == "no") | ||
| { | ||
| Console::Root().SetConsoleColorMode(spdlog::color_mode::never); | ||
| } | ||
| else | ||
| { | ||
| Console::Root().RawLogger().log(spdlog::level::warn, | ||
| "Valid values are: yes, no."); |
There was a problem hiding this comment.
GZ_CONSOLE_COLOR only recognizes yes/no. The linked issue / common patterns in this repo suggest accepting additional boolean-style values (e.g., 0/1, true/false) and ideally auto (leave default behavior) to avoid surprising users who follow gtest-style env vars.
| if (value == "yes") | |
| { | |
| Console::Root().SetConsoleColorMode(spdlog::color_mode::always); | |
| } | |
| else if (value == "no") | |
| { | |
| Console::Root().SetConsoleColorMode(spdlog::color_mode::never); | |
| } | |
| else | |
| { | |
| Console::Root().RawLogger().log(spdlog::level::warn, | |
| "Valid values are: yes, no."); | |
| if (value == "yes" || value == "true" || value == "1" || value == "on") | |
| { | |
| Console::Root().SetConsoleColorMode(spdlog::color_mode::always); | |
| } | |
| else if (value == "no" || value == "false" || value == "0" || value == "off") | |
| { | |
| Console::Root().SetConsoleColorMode(spdlog::color_mode::never); | |
| } | |
| else if (value == "auto") | |
| { | |
| // Leave console color mode at its default behavior. | |
| } | |
| else | |
| { | |
| Console::Root().RawLogger().log(spdlog::level::warn, | |
| "GZ_CONSOLE_COLOR: valid values are: yes, no, true, false, 1, 0, on, off, auto."); |
There was a problem hiding this comment.
Is there a need to handle all cases? making it work for standard yes no should be fine I believe
|
|
||
| * `yes`: always emit ANSI color codes. | ||
| * `no`: never emit ANSI color codes. | ||
|
|
There was a problem hiding this comment.
README documents only yes/no, but the implementation is intended to address the broader “disable color output” use case (and the issue suggests gtest-like behavior). If you add support for 0/1 and/or auto, please document those here; otherwise consider explicitly stating that only yes/no are supported and that other values will be ignored with a warning.
| Only the values `yes` and `no` are recognized; other values are ignored. |
Exposes color_mode control on the console sink to allow callers to override spdlog's automatic color detection. Required by gazebosim/gz-common#792 Related: gazebosim/gz-common#611 Signed-off-by: BhuvanB <bhuvanb1408@gmail.com>
azeey
left a comment
There was a problem hiding this comment.
Thanks for the contribution @BhuvanB404! Sorry I didn't get a chance to respond to your comment on #611. That issue was created while gz-common had it's own implementation of console logging. Now that we are using spdlog, our Jenkins logs do not contain the color codes (see https://build.osrfoundation.org/view/gz-jetty/job/gz_common-ci-gz-common7-noble-amd64/58/consoleText search for 1;31m).
However, we still have the problem on Harmonic (gz-common5) and Fortress (ign-common4) (see https://build.osrfoundation.org/view/gz-harmonic/job/gz_common-ci-gz-common5-noble-amd64/187/consoleText and search for 1;31m). So, could you please rework your PR to target gz-common5? Thanks!
…utput Signed-off-by: BhuvanB <bhuvanb1408@gmail.com>
6d9eaf2 to
906cdd8
Compare
|
@azeey i have refactored it for gz-common 5 . the dco failure is for other contributes commits . its fine to leave it I guess? the build is failing on I am not sure its related to my changes |
azeey
left a comment
There was a problem hiding this comment.
Thanks for the contribution! The PR looks good to me. I just have a couple of minor comments. Sorry for the long delay in reviewing it.
|
|
||
| Please refer to the [examples directory](https://github.com/gazebosim/gz-common/tree/main/examples). | ||
|
|
||
| Console color output can be controlled with `GZ_CONSOLE_COLOR`: |
There was a problem hiding this comment.
The README is not the best place to put this information. You can instead place it in Console.hh so it shows up in the API docs https://gazebosim.org/api/common/5/classgz_1_1common_1_1Console.html#details
| #define CONSOLE_COLOR_ON 1 | ||
| #define CONSOLE_COLOR_OFF 0 |
There was a problem hiding this comment.
Consider using an enum instead.
🎉 New feature
Closes #611
Summary
If the env variable is yes , the ANSI color codes are on for logging. When it is no, it disables ANSI colors
-- Testing GZ_CONSOLE_COLOR = yes ---
^[[32m(2026-03-06 13:16:01.402) [info] [test_color.cc:10] This is an info message
^[[m^[[33m^[[1m(2026-03-06 13:16:01.402) [warning] [test_color.cc:11] This is a warning message
^[[m^[[31m^[[1m(2026-03-06 13:16:01.402) [error] [test_color.cc:12] This is an error message
after turning the color_mode to no the outputs do not contain the asci color codes.
--- Testing GZ_CONSOLE_COLOR = no---
(2026-03-06 13:16:01.404) [info] [test_color.cc:10] This is an info message
(2026-03-06 13:16:01.404) [warning] [test_color.cc:11] This is a warning message
(2026-03-06 13:16:01.404) [error] [test_color.cc:12] This is an error message
(2026-03-06 13:16:01.404) [debug] [test_color.cc:13] This is a debug message
Need to add a logger wrapper in gz-utils to change the color_mode through sink. that is done in this pr -> gazebosim/gz-utils#205
Test it
Checklist
codecheckpassed (See contributing)