Bitcoin ABC  0.26.3
P2P Digital Currency
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 
17 namespace BCLog {
18 
20 template <typename TimeType> class Timer {
21 public:
24  Timer(std::string prefix, std::string end_msg,
25  BCLog::LogFlags log_category = BCLog::LogFlags::ALL,
26  bool msg_on_completion = true)
27  : m_prefix(std::move(prefix)), m_title(std::move(end_msg)),
28  m_log_category(log_category),
29  m_message_on_completion(msg_on_completion) {
30  this->Log(strprintf("%s started", m_title));
31  m_start_t = GetTime<std::chrono::microseconds>();
32  }
33 
34  ~Timer() {
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 
45  if (m_log_category == BCLog::LogFlags::ALL) {
46  LogPrintf("%s\n", full_msg);
47  } else {
48  LogPrint(m_log_category, "%s\n", full_msg);
49  }
50  }
51 
52  std::string LogMsg(const std::string &msg) {
53  const auto end_time = GetTime<std::chrono::microseconds>() - m_start_t;
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 
75 private:
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> PASTE2( \
96  logging_timer, __COUNTER__)(__func__, end_msg, log_category)
97 #define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
98  BCLog::Timer<std::chrono::milliseconds> PASTE2( \
99  logging_timer, __COUNTER__)(__func__, end_msg, log_category)
100 #define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \
101  BCLog::Timer<std::chrono::milliseconds> PASTE2( \
102  logging_timer, __COUNTER__)(__func__, end_msg, log_category, \
103  /* msg_on_completion=*/false)
104 #define LOG_TIME_SECONDS(end_msg) \
105  BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, \
106  __COUNTER__)(__func__, end_msg)
107 
108 #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
~Timer()
Definition: timer.h:34
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:208
#define LogPrintf(...)
Definition: logging.h:204
Definition: timer.h:17
LogFlags
Definition: logging.h:37
const char * prefix
Definition: rest.cpp:818
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201