Skip to content

Commit 3f4c6b8

Browse files
committed
log, timer: add timing macro in usec LOG_TIME_MICROS_WITH_CATEGORY
and update BCLog::LogMsg() to omit irrelevant decimals for microseconds and skip unneeded code and math.
1 parent b7a1744 commit 3f4c6b8

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/logging/timer.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ class Timer
5858
return strprintf("%s: %s", m_prefix, msg);
5959
}
6060

61-
std::string units = "";
61+
if (std::is_same<TimeType, std::chrono::microseconds>::value) {
62+
return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
63+
}
64+
65+
std::string units;
6266
float divisor = 1;
6367

64-
if (std::is_same<TimeType, std::chrono::microseconds>::value) {
65-
units = "μs";
66-
} else if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
68+
if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
6769
units = "ms";
6870
divisor = 1000.;
6971
} else if (std::is_same<TimeType, std::chrono::seconds>::value) {
@@ -93,6 +95,8 @@ class Timer
9395
} // namespace BCLog
9496

9597

98+
#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \
99+
BCLog::Timer<std::chrono::microseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
96100
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
97101
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
98102
#define LOG_TIME_SECONDS(end_msg) \

src/test/logging_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(logging_timer)
2727
SetMockTime(1);
2828
auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg");
2929
SetMockTime(2);
30-
BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000.00μs)");
30+
BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000μs)");
3131
}
3232

3333
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)