diff options
author | Nicholas Hutchinson <nshutchinson@gmail.com> | 2016-09-15 22:10:35 +0100 |
---|---|---|
committer | Eric <eric@efcs.ca> | 2016-09-15 15:10:35 -0600 |
commit | 917b86e615f659d9fb9819d1fa765cd459fc6861 (patch) | |
tree | 37ef00f4eb462f5e10ece365f7dc1dae365540e1 /src/colorprint.cc | |
parent | b826143ac20a105f7caba6d1d5afe4c5204864cf (diff) | |
download | google-benchmark-917b86e615f659d9fb9819d1fa765cd459fc6861.tar.gz |
Auto-detect whether Benchmark should produce colorized output (#126)
* Auto-detect whether to produce colorized output
Rename --color_print to --benchmark_color for consistency with the other
flags (and Google Test). Old flag name is kept around for compatibility.
The --benchmark_color/--color_print flag takes a third option, "auto",
which is the new default. In this mode, we attempt to auto-detect
whether to produce colorized output. (The logic for deciding whether to
use colorized output was lifted from GTest:
<https://github.com/google/googletest/blob/master/googletest/src/gtest.cc#L2925>.)
* Update CONTRIBUTORS, AUTHORS
Diffstat (limited to 'src/colorprint.cc')
-rw-r--r-- | src/colorprint.cc | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/src/colorprint.cc b/src/colorprint.cc index b7d316f..f24e6f8 100644 --- a/src/colorprint.cc +++ b/src/colorprint.cc @@ -16,16 +16,20 @@ #include <cstdarg> #include <cstdio> -#include <cstdarg> -#include <string> +#include <cstdlib> +#include <cstring> #include <memory> +#include <string> #include "check.h" #include "internal_macros.h" #ifdef BENCHMARK_OS_WINDOWS +#include <io.h> #include <Windows.h> -#endif +#else +#include <unistd.h> +#endif // BENCHMARK_OS_WINDOWS namespace benchmark { namespace { @@ -151,4 +155,34 @@ void ColorPrintf(std::ostream& out, LogColor color, const char* fmt, va_list arg } +bool IsColorTerminal() { +#if BENCHMARK_OS_WINDOWS + // On Windows the TERM variable is usually not set, but the + // console there does support colors. + return 0 != _isatty(_fileno(stdout)); +#else + // On non-Windows platforms, we rely on the TERM variable. 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", + }; + + const char* const term = getenv("TERM"); + + bool term_supports_color = false; + for (const char* candidate : SUPPORTED_TERM_VALUES) { + if (term && 0 == strcmp(term, candidate)) { + term_supports_color = true; + break; + } + } + + return 0 != isatty(fileno(stdout)) && term_supports_color; +#endif // BENCHMARK_OS_WINDOWS +} + } // end namespace benchmark |