Skip to content

Add GZ_CONSOLE_COLOR env variable to control terminal color output#792

Open
BhuvanB404 wants to merge 1 commit intogazebosim:gz-common5from
BhuvanB404:ANSI-color-output
Open

Add GZ_CONSOLE_COLOR env variable to control terminal color output#792
BhuvanB404 wants to merge 1 commit intogazebosim:gz-common5from
BhuvanB404:ANSI-color-output

Conversation

@BhuvanB404
Copy link
Copy Markdown

@BhuvanB404 BhuvanB404 commented Mar 6, 2026

🎉 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

  • Signed all commits for DCO
  • Added a screen capture or video to the PR description that demonstrates the feature
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • Updated Bazel files (if adding new files). Created an issue otherwise.
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers
  • Was GenAI used to generate this PR? If so, make sure to add "Generated-by" to your commits. (See this policy for more info.)

@BhuvanB404 BhuvanB404 requested a review from marcoag as a code owner March 6, 2026 07:49
Copilot AI review requested due to automatic review settings March 6, 2026 07:49
@github-actions github-actions bot added the 🪵 jetty Gazebo Jetty label Mar 6, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_COLOR during gz::common::Console::Init and set spdlog console color mode to always / never.
  • Emit a warning when GZ_CONSOLE_COLOR is set to an unrecognized value.
  • Document GZ_CONSOLE_COLOR in 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.

src/Console.cc Outdated
Comment on lines +94 to +105
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.");
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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.");

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
Only the values `yes` and `no` are recognized; other values are ignored.

Copilot uses AI. Check for mistakes.
BhuvanB404 added a commit to BhuvanB404/gz-utils that referenced this pull request Mar 6, 2026
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>
Copy link
Copy Markdown
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@github-project-automation github-project-automation bot moved this from Inbox to In review in Core development Mar 6, 2026
…utput

Signed-off-by: BhuvanB <bhuvanb1408@gmail.com>
@BhuvanB404 BhuvanB404 changed the base branch from gz-common7 to gz-common5 March 23, 2026 03:04
@BhuvanB404
Copy link
Copy Markdown
Author

@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 PERFORMANCE_plugin_specialization test.

I am not sure its related to my changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🪵 jetty Gazebo Jetty

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

Provide a mechanism to disable color output

3 participants