Skip to content

Commit 6d78a70

Browse files
committed
Add GZ_CONSOLE_COLOR env variable to control terminal color output
If the env variable is yes , the ANSI color codes are on for logging. When it is no, it disables ANSI colors Fixes #611 Signed-off-by: BhuvanB <bhuvanb1408@gmail.com>
1 parent 926f9e9 commit 6d78a70

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ See the [installation tutorial](https://gazebosim.org/api/common/7/install.html)
6161

6262
Please refer to the [examples directory](https://github.com/gazebosim/gz-common/tree/gz-common7/examples).
6363

64+
Console color output can be controlled with `GZ_CONSOLE_COLOR`:
65+
66+
* `yes`: always emit ANSI color codes.
67+
* `no`: never emit ANSI color codes.
68+
6469
# Folder Structure
6570

6671
Refer to the following table for information about important directories and files in this repository.

src/Console.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ Console &Console::Root()
8585
bool Console::Init(const std::string &_directory, const std::string &_filename)
8686
{
8787
std::string logPath;
88+
std::string consoleColor;
89+
90+
if (env("GZ_CONSOLE_COLOR", consoleColor))
91+
{
92+
const auto value = lowercase(trimmed(consoleColor));
93+
94+
if (value == "yes")
95+
{
96+
Console::Root().SetConsoleColorMode(spdlog::color_mode::always);
97+
}
98+
else if (value == "no")
99+
{
100+
Console::Root().SetConsoleColorMode(spdlog::color_mode::never);
101+
}
102+
else
103+
{
104+
Console::Root().RawLogger().log(spdlog::level::warn,
105+
"Valid values are: yes, no.");
106+
}
107+
}
88108

89109
if (_directory.empty() ||
90110
#ifndef _WIN32

0 commit comments

Comments
 (0)