Skip to content

Commit 498b323

Browse files
committed
log, timer: improve BCLog::LogMsg()
- make timer code more homogeneous - replace division with multiplication - log if the time type is unexpected
1 parent 8d2f847 commit 498b323

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/logging/timer.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,13 @@ class Timer
6060

6161
if (std::is_same<TimeType, std::chrono::microseconds>::value) {
6262
return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
63-
}
64-
65-
std::string units;
66-
float divisor = 1;
67-
68-
if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
69-
units = "ms";
70-
divisor = 1000.;
63+
} else if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
64+
return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001);
7165
} else if (std::is_same<TimeType, std::chrono::seconds>::value) {
72-
units = "s";
73-
divisor = 1000. * 1000.;
66+
return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001);
67+
} else {
68+
return "Error: unexpected time type";
7469
}
75-
76-
const float time_ms = end_time.count() / divisor;
77-
return strprintf("%s: %s (%.2f%s)", m_prefix, msg, time_ms, units);
7870
}
7971

8072
private:

src/test/logging_tests.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ BOOST_FIXTURE_TEST_SUITE(logging_tests, BasicTestingSetup)
1515
BOOST_AUTO_TEST_CASE(logging_timer)
1616
{
1717
SetMockTime(1);
18-
auto sec_timer = BCLog::Timer<std::chrono::seconds>("tests", "end_msg");
18+
auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg");
1919
SetMockTime(2);
20-
BOOST_CHECK_EQUAL(sec_timer.LogMsg("test secs"), "tests: test secs (1.00s)");
20+
BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000μs)");
2121

2222
SetMockTime(1);
2323
auto ms_timer = BCLog::Timer<std::chrono::milliseconds>("tests", "end_msg");
2424
SetMockTime(2);
2525
BOOST_CHECK_EQUAL(ms_timer.LogMsg("test ms"), "tests: test ms (1000.00ms)");
2626

2727
SetMockTime(1);
28-
auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg");
28+
auto sec_timer = BCLog::Timer<std::chrono::seconds>("tests", "end_msg");
2929
SetMockTime(2);
30-
BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000μs)");
30+
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");
3136
}
3237

3338
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)