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

Expose default help printer function #1425

Merged
merged 2 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ namespace benchmark {
class BenchmarkReporter;

BENCHMARK_EXPORT void Initialize(int* argc, char** argv,
void (*HelperPrinterf)() = NULL);
void (*HelperPrinterf)(FILE*,
const char*) = NULL);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a breaking change to the API. If needed, I think we can overload Initialize to accept either an argumentless function or a two-argument version. Please say so if this is necessary.

BENCHMARK_EXPORT void Shutdown();

// Report to stdout all arguments in 'argv' as unrecognized except the first.
Expand Down
53 changes: 28 additions & 25 deletions src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,31 +564,32 @@ void AddCustomContext(const std::string& key, const std::string& value) {

namespace internal {

void (*HelperPrintf)();
void (*HelperPrintf)(FILE* stream, const char* text);

void DefaultHelperPrintf(FILE* stream, const char* text) {
fprintf(stream, "%s", text);
}

void PrintUsageAndExit() {
if (HelperPrintf) {
HelperPrintf();
} else {
fprintf(stdout,
"benchmark"
" [--benchmark_list_tests={true|false}]\n"
" [--benchmark_filter=<regex>]\n"
" [--benchmark_min_time=<min_time>]\n"
" [--benchmark_min_warmup_time=<min_warmup_time>]\n"
" [--benchmark_repetitions=<num_repetitions>]\n"
" [--benchmark_enable_random_interleaving={true|false}]\n"
" [--benchmark_report_aggregates_only={true|false}]\n"
" [--benchmark_display_aggregates_only={true|false}]\n"
" [--benchmark_format=<console|json|csv>]\n"
" [--benchmark_out=<filename>]\n"
" [--benchmark_out_format=<json|console|csv>]\n"
" [--benchmark_color={auto|true|false}]\n"
" [--benchmark_counters_tabular={true|false}]\n"
" [--benchmark_context=<key>=<value>,...]\n"
" [--benchmark_time_unit={ns|us|ms|s}]\n"
" [--v=<verbosity>]\n");
}
HelperPrintf(
stdout,
"benchmark"
" [--benchmark_list_tests={true|false}]\n"
" [--benchmark_filter=<regex>]\n"
" [--benchmark_min_time=<min_time>]\n"
" [--benchmark_min_warmup_time=<min_warmup_time>]\n"
" [--benchmark_repetitions=<num_repetitions>]\n"
" [--benchmark_enable_random_interleaving={true|false}]\n"
" [--benchmark_report_aggregates_only={true|false}]\n"
" [--benchmark_display_aggregates_only={true|false}]\n"
" [--benchmark_format=<console|json|csv>]\n"
" [--benchmark_out=<filename>]\n"
" [--benchmark_out_format=<json|console|csv>]\n"
" [--benchmark_color={auto|true|false}]\n"
" [--benchmark_counters_tabular={true|false}]\n"
" [--benchmark_context=<key>=<value>,...]\n"
" [--benchmark_time_unit={ns|us|ms|s}]\n"
" [--v=<verbosity>]\n");
exit(0);
}

Expand Down Expand Up @@ -670,10 +671,12 @@ int InitializeStreams() {

} // end namespace internal

void Initialize(int* argc, char** argv, void (*HelperPrintf)()) {
void Initialize(int* argc, char** argv,
void (*HelperPrintf)(FILE*, const char*)) {
internal::ParseCommandLineFlags(argc, argv);
internal::LogLevel() = FLAGS_v;
internal::HelperPrintf = HelperPrintf;
internal::HelperPrintf =
HelperPrintf ? HelperPrintf : internal::DefaultHelperPrintf;
}

void Shutdown() { delete internal::global_context; }
Expand Down