Skip to content

Commit f530202

Browse files
martinusjonatack
authored andcommitted
Make unexpected time type in BCLog::LogMsg() a compile-time error
1 parent bddae7e commit f530202

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

src/logging/timer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <logging.h>
1010
#include <util/macros.h>
1111
#include <util/time.h>
12+
#include <util/types.h>
1213

1314
#include <chrono>
1415
#include <string>
@@ -58,14 +59,14 @@ class Timer
5859
return strprintf("%s: %s", m_prefix, msg);
5960
}
6061

61-
if (std::is_same<TimeType, std::chrono::microseconds>::value) {
62+
if constexpr (std::is_same<TimeType, std::chrono::microseconds>::value) {
6263
return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
63-
} else if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
64+
} else if constexpr (std::is_same<TimeType, std::chrono::milliseconds>::value) {
6465
return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001);
65-
} else if (std::is_same<TimeType, std::chrono::seconds>::value) {
66+
} else if constexpr (std::is_same<TimeType, std::chrono::seconds>::value) {
6667
return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001);
6768
} else {
68-
return "Error: unexpected time type";
69+
static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type");
6970
}
7071
}
7172

@@ -81,7 +82,6 @@ class Timer
8182
//! Forwarded on to LogPrint if specified - has the effect of only
8283
//! outputting the timing log when a particular debug= category is specified.
8384
const BCLog::LogFlags m_log_category{};
84-
8585
};
8686

8787
} // namespace BCLog

src/test/logging_tests.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ BOOST_AUTO_TEST_CASE(logging_timer)
2828
auto sec_timer = BCLog::Timer<std::chrono::seconds>("tests", "end_msg");
2929
SetMockTime(2);
3030
BOOST_CHECK_EQUAL(sec_timer.LogMsg("test secs"), "tests: test secs (1.00s)");
31-
32-
SetMockTime(1);
33-
auto minute_timer = BCLog::Timer<std::chrono::minutes>("tests", "end_msg");
34-
SetMockTime(2);
35-
BOOST_CHECK_EQUAL(minute_timer.LogMsg("test minutes"), "Error: unexpected time type");
3631
}
3732

3833
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)