From f530202353a4f8bb444966559aa15681ab3cebc6 Mon Sep 17 00:00:00 2001 From: Martin Ankerl Date: Tue, 7 Sep 2021 18:16:09 +0200 Subject: [PATCH] Make unexpected time type in BCLog::LogMsg() a compile-time error --- src/logging/timer.h | 10 +++++----- src/test/logging_tests.cpp | 5 ----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/logging/timer.h b/src/logging/timer.h index 6d584084cad..79627b1fe31 100644 --- a/src/logging/timer.h +++ b/src/logging/timer.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -58,14 +59,14 @@ public: return strprintf("%s: %s", m_prefix, msg); } - if (std::is_same::value) { + if constexpr (std::is_same::value) { return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count()); - } else if (std::is_same::value) { + } else if constexpr (std::is_same::value) { return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001); - } else if (std::is_same::value) { + } else if constexpr (std::is_same::value) { return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001); } else { - return "Error: unexpected time type"; + static_assert(ALWAYS_FALSE, "Error: unexpected time type"); } } @@ -81,7 +82,6 @@ private: //! Forwarded on to LogPrint if specified - has the effect of only //! outputting the timing log when a particular debug= category is specified. const BCLog::LogFlags m_log_category{}; - }; } // namespace BCLog diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp index 0e384f72e2a..84ddbc50c66 100644 --- a/src/test/logging_tests.cpp +++ b/src/test/logging_tests.cpp @@ -28,11 +28,6 @@ BOOST_AUTO_TEST_CASE(logging_timer) auto sec_timer = BCLog::Timer("tests", "end_msg"); SetMockTime(2); BOOST_CHECK_EQUAL(sec_timer.LogMsg("test secs"), "tests: test secs (1.00s)"); - - SetMockTime(1); - auto minute_timer = BCLog::Timer("tests", "end_msg"); - SetMockTime(2); - BOOST_CHECK_EQUAL(minute_timer.LogMsg("test minutes"), "Error: unexpected time type"); } BOOST_AUTO_TEST_SUITE_END()