Bitcoin ABC  0.24.7
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 
13 #include <chrono>
14 #include <string>
15 
16 namespace BCLog {
17 
19 template <typename TimeType> class Timer {
20 public:
23  Timer(std::string prefix, std::string end_msg,
24  BCLog::LogFlags log_category = BCLog::LogFlags::ALL)
25  : m_prefix(std::move(prefix)), m_title(std::move(end_msg)),
26  m_log_category(log_category) {
27  this->Log(strprintf("%s started", m_title));
28  m_start_t = GetTime<std::chrono::microseconds>();
29  }
30 
31  ~Timer() { this->Log(strprintf("%s completed", m_title)); }
32 
33  void Log(const std::string &msg) {
34  const std::string full_msg = this->LogMsg(msg);
35 
36  if (m_log_category == BCLog::LogFlags::ALL) {
37  LogPrintf("%s\n", full_msg);
38  } else {
39  LogPrint(m_log_category, "%s\n", full_msg);
40  }
41  }
42 
43  std::string LogMsg(const std::string &msg) {
44  const auto end_time = GetTime<std::chrono::microseconds>() - m_start_t;
45  if (m_start_t.count() <= 0) {
46  return strprintf("%s: %s", m_prefix, msg);
47  }
48 
49  std::string units = "";
50  float divisor = 1;
51 
52  if (std::is_same<TimeType, std::chrono::microseconds>::value) {
53  units = "μs";
54  } else if (std::is_same<TimeType, std::chrono::milliseconds>::value) {
55  units = "ms";
56  divisor = 1000.;
57  } else if (std::is_same<TimeType, std::chrono::seconds>::value) {
58  units = "s";
59  divisor = 1000. * 1000.;
60  }
61 
62  const float time_ms = end_time.count() / divisor;
63  return strprintf("%s: %s (%.2f%s)", m_prefix, msg, time_ms, units);
64  }
65 
66 private:
67  std::chrono::microseconds m_start_t{};
68 
70  const std::string m_prefix{};
71 
73  const std::string m_title{};
74 
78 };
79 
80 } // namespace BCLog
81 
82 #define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
83  BCLog::Timer<std::chrono::milliseconds> PASTE2( \
84  logging_timer, __COUNTER__)(__func__, end_msg, log_category)
85 #define LOG_TIME_SECONDS(end_msg) \
86  BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, \
87  __COUNTER__)(__func__, end_msg)
88 
89 #endif // BITCOIN_LOGGING_TIMER_H
BCLog::LogFlags
LogFlags
Definition: logging.h:36
BCLog::Timer
RAII-style object that outputs timing information to logs.
Definition: timer.h:19
BCLog::Timer::LogMsg
std::string LogMsg(const std::string &msg)
Definition: timer.h:43
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:23
BCLog::Timer::~Timer
~Timer()
Definition: timer.h:31
prefix
const char * prefix
Definition: rest.cpp:772
BCLog::Timer::m_title
const std::string m_title
A descriptive message of what is being timed.
Definition: timer.h:73
BCLog::Timer::Log
void Log(const std::string &msg)
Definition: timer.h:33
time.h
BCLog::Timer::m_log_category
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:77
LogPrint
#define LogPrint(category,...)
Definition: logging.h:193
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
BCLog
Definition: timer.h:16
BCLog::Timer::m_prefix
const std::string m_prefix
Log prefix; usually the name of the function this was created in.
Definition: timer.h:70
logging.h
LogPrintf
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:175
BCLog::Timer::m_start_t
std::chrono::microseconds m_start_t
Definition: timer.h:67