How to force black and white output in google control

I use google test in Xcode and for some reason produces color output. Since Xcode does not seem to support color output, I see unwanted characters. I was wondering if it is possible to force a black-and-white result in a google test. I prefer answers that use their APIs, but I am open to other alternatives.

+5
source share
1 answer

Google Benchmark color output is listed in their Readme: https://github.com/google/benchmark#output-formats

Output formats

The library supports several output formats. Use --benchmark_format=<console|json|csv> to set the format. The console is the default format.

The console format is intended for human reading. From the default format generates color information. The context is displayed on stderr and table data on stdout.

It is also possible to save a non-color copy of the output to a file using --benchmark_out=file --benchmark_out_format=console (default is json)

The library supports writing test output to a file specified by --benchmark_out=<filename> . The output format can be specified using --benchmark_out_format={json|console|csv} . The --benchmark_out task does not suppress console output.

And in the implementation, they have a flag to disable console coloring:

https://github.com/google/benchmark/blob/a271c36af93c7a3b19dfeb2aefa9ca77a58e52e4/src/benchmark.cc#L87

  DEFINE_string(benchmark_color, "auto", "Whether to use colors in the output. Valid values: " "'true'/'yes'/1, 'false'/'no'/0, and 'auto'. 'auto' means to use " "colors if the output is being sent to a terminal and the TERM " "environment variable is set to a terminal type that supports " "colors."); 

So, you can use --benchmark_color=false , as suggested by Artemy (using the shell shell script), or try to pass the console output to the "useless cat template",

  ./program | cat 

It should set stdout to fail isatty() check in auto color mode by default (this trick works to disable grep color).

Or change the env variable with export TERM=ansi to indicate that you have a green and black CRT monitor: https://github.com/google/benchmark/blob/09b93ccc6a9aed84c269b6f5b8130c878e518ebb/src/colorprint.cc#L167

  // ... This list of // supported TERM values is copied from Google Test: // <https://github.com/google/googletest/blob/master/googletest/src/gtest.cc#L2925>. const char* const SUPPORTED_TERM_VALUES[] = { "xterm", "xterm-color", "xterm-256color", "screen", "screen-256color", "tmux", "tmux-256color", "rxvt-unicode", "rxvt-unicode-256color", "linux", "cygwin", }; 

But with |cat and export TERM=ansi gbench continues to generate color. There should be an error (!!!) next to GetOutputOptions , which makes IsColorTerminal () the noop logic in the case of "auto", so the "auto" mode is not really auto, it is always enabled:

 ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) { int output_opts = ConsoleReporter::OO_Defaults; if ((FLAGS_benchmark_color == "auto" && IsColorTerminal()) || IsTruthyFlagValue(FLAGS_benchmark_color)) { output_opts |= ConsoleReporter::OO_Color; } else { output_opts &= ~ConsoleReporter::OO_Color; } 

IsTruthyFlagValue considers the default β€œauto” value to be true and always turns on color output, even if the terminal does not support it!

 bool IsTruthyFlagValue(const std::string& value) { if (value.empty()) return true; char ch = value[0]; return isalnum(ch) && !(ch == '0' || ch == 'f' || ch == 'F' || ch == 'n' || ch == 'N'); } 

Here is a patch to enable automatic mode (it will work with |cat and TERM=ansi ./program and, possibly, with some IDEs that correctly install TERM), could not fulfill the pull request:

 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -555,7 +555,8 @@ bool IsZero(double n) { ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) { int output_opts = ConsoleReporter::OO_Defaults; if ((FLAGS_benchmark_color == "auto" && IsColorTerminal()) || - IsTruthyFlagValue(FLAGS_benchmark_color)) { + (FLAGS_benchmark_color != "auto" && + IsTruthyFlagValue(FLAGS_benchmark_color))) { output_opts |= ConsoleReporter::OO_Color; } else { output_opts &= ~ConsoleReporter::OO_Color; 
+2
source

Source: https://habr.com/ru/post/1271642/


All Articles