Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-detect whether Benchmark should produce colorized output #126

Merged
merged 2 commits into from
Sep 15, 2016
Merged

Auto-detect whether Benchmark should produce colorized output #126

merged 2 commits into from
Sep 15, 2016

Conversation

nickhutchinson
Copy link
Contributor

Attached is a patch that makes Benchmark only produce colorized output
if stdout is a TTY. (This can be overridden with --color-print=true.)

The logic for deciding whether to use colorized output was lifted from
GTest: https://code.google.com/p/googletest/source/browse/trunk/src/gtest.cc#2872.

I haven't got a Windows system to test my changes on, but I see from #7
that there's more work to be done here anyway, so hopefully that's okay!

#endif

namespace benchmark {
namespace posix {
Copy link
Contributor

Choose a reason for hiding this comment

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

I know the top level namespace posix is reserved for the implementation in C++. I would prefer not using a nested one either. Could you come up with another name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My model here was Google Test's internal/gtest-port.h. I'll have a ponder about an alternate namespace name. Would you be open to ditching the nested namespace altogether?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not opposed towards dropping the namespace.

@EricWF
Copy link
Contributor

EricWF commented Jun 1, 2015

Seems like a reasonable patch. Is there a reason to perfer using C strings in the function interfaces as opposed to std::string? If there isn't then I would prefer to use std::string.

@@ -858,7 +862,7 @@ void PrintUsageAndExit() {
" [--benchmark_min_time=<min_time>]\n"
" [--benchmark_repetitions=<num_repetitions>]\n"
" [--benchmark_format=<tabular|json|csv>]\n"
" [--color_print={true|false}]\n"
" [--color_print={auto|true|false}]\n"
Copy link
Member

Choose a reason for hiding this comment

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

the usage says 'true' and 'false' while the help says 'yes' and 'no'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.83%) to 66.61% when pulling 49112c6 on nickhutchinson:auto-color-output into f6c2ea7 on google:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.35%) to 67.09% when pulling 523fdf1 on nickhutchinson:auto-color-output into f6c2ea7 on google:master.

ParseInt32Flag(argv[i], "v", &FLAGS_v)) {
for (int j = i; j != *argc; ++j) argv[j] = argv[j + 1];

--(*argc);
--i;
} else if (IsFlag(argv[i], "help")) {
} else if (IsFlag(argv[i], "help") || HasBenchmarkFlagPrefix(argv[i])) {
Copy link
Member

Choose a reason for hiding this comment

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

!HasBenchmarkFlagPrefix?

also, redundant given how we parse the strings above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not quite -- without this check, you could pass --benchmark_format or similar with no associated value, and no error will be reported.

Copy link
Member

Choose a reason for hiding this comment

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

$ test/benchmark_test --benchmark_format=foo
benchmark [--benchmark_list_tests={true|false}]
[--benchmark_filter=]
[--benchmark_min_time=<min_time>]
[--benchmark_repetitions=<num_repetitions>]
[--benchmark_format=<tabular|json|csv>]
[--color_print={true|false}]
[--v=]

this is working as intended. the checks for specific args come next.

@dmah42
Copy link
Member

dmah42 commented Sep 30, 2015

@nickhutchinson are you still working on this?

@EricWF
Copy link
Contributor

EricWF commented Aug 29, 2016

@nickhutchinson Are you interested in continuing with this?

@nickhutchinson
Copy link
Contributor Author

Sorry for the delay, I’ll try fix this up this week.

On 29 Aug 2016, at 06:21, Eric notifications@github.com wrote:

@nickhutchinson https://github.com/nickhutchinson Are you interested in continuing with this?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #126 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AAuH7KjeTf9hqmHHi02JX-Sc_iWk-dL4ks5qkmx2gaJpZM4EzKUw.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 86.948% when pulling 6d69db7 on nickhutchinson:auto-color-output into 72be952 on google:master.

@nickhutchinson
Copy link
Contributor Author

Finally got around to rebasing this on master -- thanks for your patience. I believe I've addressed the review comments, in that the new patch:

  • uses std::string instead of const char*
  • removes error-checking of CLI arguments -- as pointed out, this isn't relevant to colorised output.

I've tested on Win10/MSVC2015 and OS X 10.11/Clang 3.8.

@AppVeyorBot
Copy link

Build benchmark 445 failed (commit a7ed56c34b by @nickhutchinson)

return (env != nullptr && env[0] != '\0') ? env : nullptr;
#else
return getenv(name);
#endif // BENCHMARK_OS_WINDOWS
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is incorrect.

@EricWF
Copy link
Contributor

EricWF commented Sep 5, 2016

Thanks again for this patch!

PS. I really liked your command line argument checking. If you want to submit that as a separate patch it would be very welcome.

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>.)
@nickhutchinson
Copy link
Contributor Author

Addressed code review comments.

  • rebase on master
  • make IsTruthyFlagValue() return true if given the empty string.
  • remove posix.h header, folded the functions into IsColorTerminal() (which no longer accepts any arguments)
  • remove GetEnv() shim, update callers to use stdlib's getenv() directly.
  • add FIXME to --color_print comment.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) to 86.817% when pulling 1be8b73 on nickhutchinson:auto-color-output into c6f3f0e on google:master.

@AppVeyorBot
Copy link

@EricWF EricWF merged commit 917b86e into google:master Sep 15, 2016
@nickhutchinson nickhutchinson deleted the auto-color-output branch September 24, 2016 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants