diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index cc5ecd0d..63e4542a 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -308,6 +308,9 @@ BENCHMARK_EXPORT std::string GetBenchmarkFilter(); // `benchmark::Initialize()` will override the flag's value. BENCHMARK_EXPORT void SetBenchmarkFilter(std::string value); +// Returns the current value of --v (command line value for verbosity). +BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity(); + // Creates a default display reporter. Used by the library when no display // reporter is provided, but also made available for external use in case a // custom reporter should respect the `--benchmark_format` flag as a fallback diff --git a/src/benchmark.cc b/src/benchmark.cc index d2306881..6e4415ac 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -539,6 +539,8 @@ void SetBenchmarkFilter(std::string value) { FLAGS_benchmark_filter = std::move(value); } +int32_t GetBenchmarkVerbosity() { return FLAGS_v; } + void RegisterMemoryManager(MemoryManager* manager) { internal::memory_manager = manager; } diff --git a/test/BUILD b/test/BUILD index 9d116175..f1ba0f9f 100644 --- a/test/BUILD +++ b/test/BUILD @@ -23,6 +23,7 @@ PER_SRC_TEST_ARGS = ({ "user_counters_tabular_test.cc": ["--benchmark_counters_tabular=true"], "repetitions_test.cc": [" --benchmark_repetitions=3"], "spec_arg_test.cc" : ["--benchmark_filter=BM_NotChosen"], + "spec_arg_verbosity_test.cc" : ["--v=42"], }) load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d5eb25d1..d528ee94 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,6 +62,9 @@ add_test(NAME benchmark COMMAND benchmark_test --benchmark_min_time=0.01) compile_benchmark_test(spec_arg_test) add_test(NAME spec_arg COMMAND spec_arg_test --benchmark_filter=BM_NotChosen) +compile_benchmark_test(spec_arg_verbosity_test) +add_test(NAME spec_arg_verbosity COMMAND spec_arg_verbosity_test --v=42) + compile_benchmark_test(benchmark_setup_teardown_test) add_test(NAME benchmark_setup_teardown COMMAND benchmark_setup_teardown_test) diff --git a/test/spec_arg_verbosity_test.cc b/test/spec_arg_verbosity_test.cc new file mode 100644 index 00000000..8f8eb6d3 --- /dev/null +++ b/test/spec_arg_verbosity_test.cc @@ -0,0 +1,43 @@ +#include + +#include + +#include "benchmark/benchmark.h" + +// Tests that the user specified verbosity level can be get. +static void BM_Verbosity(benchmark::State& state) { + for (auto _ : state) { + } +} +BENCHMARK(BM_Verbosity); + +int main(int argc, char** argv) { + const int32_t flagv = 42; + + // Verify that argv specify --v=42. + bool found = false; + for (int i = 0; i < argc; ++i) { + if (strcmp("--v=42", argv[i]) == 0) { + found = true; + break; + } + } + if (!found) { + std::cerr << "This test requires '--v=42' to be passed as a command-line " + << "argument.\n"; + return 1; + } + + benchmark::Initialize(&argc, argv); + + // Check that the current flag value is reported accurately via the + // GetBenchmarkVerbosity() function. + if (flagv != benchmark::GetBenchmarkVerbosity()) { + std::cerr + << "Seeing different value for flags. GetBenchmarkVerbosity() returns [" + << benchmark::GetBenchmarkVerbosity() << "] expected flag=[" << flagv + << "]\n"; + return 1; + } + return 0; +}