Bitcoin Core  22.99.0
P2P Digital Currency
timer.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 
17 
18 namespace BCLog {
19 
21 template <typename TimeType>
22 class Timer
23 {
24 public:
28  std::string prefix,
29  std::string end_msg,
30  BCLog::LogFlags log_category = BCLog::LogFlags::ALL) :
31  m_prefix(std::move(prefix)),
32  m_title(std::move(end_msg)),
33  m_log_category(log_category)
34  {
35  this->Log(strprintf("%s started", m_title));
36  m_start_t = GetTime<std::chrono::microseconds>();
37  }
38 
40  {
41  this->Log(strprintf("%s completed", m_title));
42  }
43 
44  void Log(const std::string& msg)
45  {
46  const std::string full_msg = this->LogMsg(msg);
47 
49  LogPrintf("%s\n", full_msg);
50  } else {
51  LogPrint(m_log_category, "%s\n", full_msg);
52  }
53  }
54 
55  std::string LogMsg(const std::string& msg)
56  {
57  const auto end_time = GetTime<std::chrono::microseconds>() - m_start_t;
58  if (m_start_t.count() <= 0) {
59  return strprintf("%s: %s", m_prefix, msg);
60  }
61 
62  if constexpr (std::is_same<TimeType, std::chrono::microseconds>::value) {
63  return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
64  } else if constexpr (std::is_same<TimeType, std::chrono::milliseconds>::value) {
65  return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001);
66  } else if constexpr (std::is_same<TimeType, std::chrono::seconds>::value) {
67  return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001);
68  } else {
69  static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type");
70  }
71  }
72 
73 private:
74  std::chrono::microseconds m_start_t{};
75 
77  const std::string m_prefix{};
78 
80  const std::string m_title{};
81 
85 };
86 
87 } // namespace BCLog
88 
89 
90 #define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \
91  BCLog::Timer<std::chrono::microseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
92 #define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
93  BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
94 #define LOG_TIME_SECONDS(end_msg) \
95  BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg)
96 
97 
98 #endif // BITCOIN_LOGGING_TIMER_H
BCLog::LogFlags
LogFlags
Definition: logging.h:36
types.h
BCLog::Timer
RAII-style object that outputs timing information to logs.
Definition: timer.h:22
BCLog::Timer::LogMsg
std::string LogMsg(const std::string &msg)
Definition: timer.h:55
macros.h
BCLog::Timer::Timer
Timer(std::string prefix, std::string end_msg, BCLog::LogFlags log_category=BCLog::LogFlags::ALL)
If log_category is left as the default, end_msg will log unconditionally (instead of being filtered b...
Definition: timer.h:27
BCLog::Timer::~Timer
~Timer()
Definition: timer.h:39
prefix
const char * prefix
Definition: rest.cpp:712
BCLog::Timer::m_title
const std::string m_title
A descriptive message of what is being timed.
Definition: timer.h:80
BCLog::Timer::Log
void Log(const std::string &msg)
Definition: timer.h:44
time.h
LogPrintf
#define LogPrintf(...)
Definition: logging.h:185
BCLog::Timer::m_log_category
const BCLog::LogFlags m_log_category
Forwarded on to LogPrint if specified - has the effect of only outputting the timing log when a parti...
Definition: timer.h:84
BCLog::ALL
@ ALL
Definition: logging.h:63
LogPrint
#define LogPrint(category,...)
Definition: logging.h:189
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
BCLog
Definition: timer.h:18
BCLog::Timer::m_prefix
const std::string m_prefix
Log prefix; usually the name of the function this was created in.
Definition: timer.h:77
BCLog::Timer::m_start_t
std::chrono::microseconds m_start_t
Definition: timer.h:74