Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2018 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_LOGGING_TIMER_H
7#define BITCOIN_LOGGING_TIMER_H
8
9#include <logging.h>
10#include <util/macros.h>
11#include <util/time.h>
12#include <util/types.h>
13
14#include <chrono>
15#include <string>
16
17namespace BCLog {
18
20template <typename TimeType> class Timer {
21public:
33
36 this->Log(strprintf("%s completed", m_title));
37 } else {
38 this->Log("completed");
39 }
40 }
41
42 void Log(const std::string &msg) {
43 const std::string full_msg = this->LogMsg(msg);
44
46 LogPrintf("%s\n", full_msg);
47 } else {
49 }
50 }
51
52 std::string LogMsg(const std::string &msg) {
54 if (m_start_t.count() <= 0) {
55 return strprintf("%s: %s", m_prefix, msg);
56 }
57
58 if constexpr (std::is_same<TimeType,
59 std::chrono::microseconds>::value) {
60 return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
61 } else if constexpr (std::is_same<TimeType,
62 std::chrono::milliseconds>::value) {
63 return strprintf("%s: %s (%.2fms)", m_prefix, msg,
64 end_time.count() * 0.001);
65 } else if constexpr (std::is_same<TimeType,
66 std::chrono::seconds>::value) {
67 return strprintf("%s: %s (%.2fs)", m_prefix, msg,
68 end_time.count() * 0.000001);
69 } else {
70 static_assert(ALWAYS_FALSE<TimeType>,
71 "Error: unexpected time type");
72 }
73 }
74
75private:
76 std::chrono::microseconds m_start_t{};
77
79 const std::string m_prefix;
80
82 const std::string m_title;
83
87
90};
91
92} // namespace BCLog
93
94#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \
95 BCLog::Timer<std::chrono::microseconds> UNIQUE_LOG_NAME(logging_timer)( \
96 __func__, end_msg, log_category)
97#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
98 BCLog::Timer<std::chrono::milliseconds> UNIQUE_LOG_NAME(logging_timer)( \
99 __func__, end_msg, log_category)
100#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \
101 BCLog::Timer<std::chrono::milliseconds> UNIQUE_LOG_NAME(logging_timer)( \
102 __func__, end_msg, log_category, /* msg_on_completion=*/false)
103#define LOG_TIME_SECONDS(end_msg) \
104 BCLog::Timer<std::chrono::seconds> UNIQUE_LOG_NAME(logging_timer)( \
105 __func__, end_msg)
106
107#endif // BITCOIN_LOGGING_TIMER_H
RAII-style object that outputs timing information to logs.
Definition timer.h:20
Timer(std::string prefix, std::string end_msg, BCLog::LogFlags log_category=BCLog::LogFlags::ALL, bool msg_on_completion=true)
If log_category is left as the default, end_msg will log unconditionally (instead of being filtered b...
Definition timer.h:24
const bool m_message_on_completion
Whether to output the message again on completion.
Definition timer.h:89
std::string LogMsg(const std::string &msg)
Definition timer.h:52
const std::string m_prefix
Log prefix; usually the name of the function this was created in.
Definition timer.h:79
const std::string m_title
A descriptive message of what is being timed.
Definition timer.h:82
std::chrono::microseconds m_start_t
Definition timer.h:76
void Log(const std::string &msg)
Definition timer.h:42
const BCLog::LogFlags m_log_category
Forwarded on to LogPrint if specified - has the effect of only outputing the timing log when a partic...
Definition timer.h:86
#define LogPrint(category,...)
Definition logging.h:238
#define LogPrintf(...)
Definition logging.h:227
LogFlags
Definition logging.h:38
@ ALL
Definition logging.h:71
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition rcu.h:259
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * prefix
Definition rest.cpp:817
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...