Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
logging
timer.h
Go to the documentation of this file.
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present 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 <
util/log.h
>
10
#include <
util/macros.h
>
11
#include <util/time.h>
12
#include <
util/types.h
>
13
14
#include <chrono>
15
#include <optional>
16
#include <string>
17
18
19
namespace
BCLog
{
20
22
template
<
typename
TimeType>
23
class
Timer
24
{
25
public
:
28
Timer
(
29
std::string
prefix
,
30
std::string end_msg,
31
BCLog::LogFlags
log_category =
BCLog::LogFlags::ALL
,
32
bool
msg_on_completion =
true
)
33
:
m_prefix
(
std
::move(
prefix
)),
34
m_title
(
std
::move(end_msg)),
35
m_log_category
(log_category),
36
m_message_on_completion
(msg_on_completion)
37
{
38
this->
Log
(
strprintf
(
"%s started"
,
m_title
));
39
m_start_t
= std::chrono::steady_clock::now();
40
}
41
42
~Timer
()
43
{
44
if
(
m_message_on_completion
) {
45
this->
Log
(
strprintf
(
"%s completed"
,
m_title
));
46
}
else
{
47
this->
Log
(
"completed"
);
48
}
49
}
50
51
void
Log
(
const
std::string& msg)
52
{
53
const
std::string full_msg = this->
LogMsg
(msg);
54
55
if
(
m_log_category
==
BCLog::LogFlags::ALL
) {
56
LogInfo
(
"%s\n"
, full_msg);
57
}
else
{
58
LogDebug
(
m_log_category
,
"%s\n"
, full_msg);
59
}
60
}
61
62
std::string
LogMsg
(
const
std::string& msg)
63
{
64
const
auto
end_time{std::chrono::steady_clock::now()};
65
if
(!
m_start_t
) {
66
return
strprintf
(
"%s: %s"
,
m_prefix
, msg);
67
}
68
const
auto
duration{end_time - *
m_start_t
};
69
70
if
constexpr
(std::is_same_v<TimeType, std::chrono::microseconds>) {
71
return
strprintf
(
"%s: %s (%iμs)"
,
m_prefix
, msg,
Ticks<std::chrono::microseconds>
(duration));
72
}
else
if
constexpr
(std::is_same_v<TimeType, std::chrono::milliseconds>) {
73
return
strprintf
(
"%s: %s (%.2fms)"
,
m_prefix
, msg,
Ticks<MillisecondsDouble>
(duration));
74
}
else
if
constexpr
(std::is_same_v<TimeType, std::chrono::seconds>) {
75
return
strprintf
(
"%s: %s (%.2fs)"
,
m_prefix
, msg,
Ticks<SecondsDouble>
(duration));
76
}
else
{
77
static_assert
(
ALWAYS_FALSE<TimeType>
,
"Error: unexpected time type"
);
78
}
79
}
80
81
private
:
82
std::optional<std::chrono::steady_clock::time_point>
m_start_t
{};
83
85
const
std::string
m_prefix
;
86
88
const
std::string
m_title
;
89
92
const
BCLog::LogFlags
m_log_category
;
93
95
const
bool
m_message_on_completion
;
96
};
97
98
}
// namespace BCLog
99
100
101
#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \
102
BCLog::Timer<std::chrono::microseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)
103
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
104
BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)
105
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \
106
BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category,
/* msg_on_completion=*/
false)
107
#define LOG_TIME_SECONDS(end_msg) \
108
BCLog::Timer<std::chrono::seconds> UNIQUE_NAME(logging_timer)(__func__, end_msg)
109
110
111
#endif
// BITCOIN_LOGGING_TIMER_H
BCLog::Timer::Timer
Timer(std::string prefix, std::string end_msg, BCLog::LogFlags log_category=BCLog::LogFlags::ALL, bool msg_on_completion=true)
Definition
timer.h:28
BCLog::Timer::m_message_on_completion
const bool m_message_on_completion
Whether to output the message again on completion.
Definition
timer.h:95
BCLog::Timer::LogMsg
std::string LogMsg(const std::string &msg)
Definition
timer.h:62
BCLog::Timer::m_prefix
const std::string m_prefix
Log prefix; usually the name of the function this was created in.
Definition
timer.h:85
BCLog::Timer::m_title
const std::string m_title
A descriptive message of what is being timed.
Definition
timer.h:88
BCLog::Timer::~Timer
~Timer()
Definition
timer.h:42
BCLog::Timer::m_start_t
std::optional< std::chrono::steady_clock::time_point > m_start_t
Definition
timer.h:82
BCLog::Timer::Log
void Log(const std::string &msg)
Definition
timer.h:51
BCLog::Timer::m_log_category
const BCLog::LogFlags m_log_category
Definition
timer.h:92
log.h
LogInfo
#define LogInfo(...)
Definition
log.h:95
LogDebug
#define LogDebug(category,...)
Definition
log.h:115
macros.h
BCLog
Definition
logging.cpp:321
BCLog::LogFlags
LogFlags
Definition
categories.h:14
BCLog::ALL
@ ALL
Definition
categories.h:49
std
Definition
common.h:29
prefix
const char * prefix
Definition
rest.cpp:1141
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition
tinyformat.h:1172
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:73
types.h
ALWAYS_FALSE
constexpr bool ALWAYS_FALSE
Definition
types.h:10
Generated on
for Bitcoin Core by
1.17.0