Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_LOGGING_TIMER_H7#define BITCOIN_LOGGING_TIMER_H9#include <util/log.h>10#include <util/macros.h>11#include <util/time.h>12#include <util/types.h>14#include <chrono>15#include <optional>16#include <string>19namespace BCLog {21//! RAII-style object that outputs timing information to logs.22template <typename TimeType>23class Timer24{25public:26//! If log_category is left as the default, end_msg will log unconditionally27//! (instead of being filtered by category).28Timer(29std::string prefix,30std::string end_msg,31BCLog::LogFlags log_category = BCLog::LogFlags::ALL,32bool msg_on_completion = true)33: m_prefix(std::move(prefix)),34m_title(std::move(end_msg)),35m_log_category(log_category),36m_message_on_completion(msg_on_completion)37{38this->Log(strprintf("%s started", m_title));39m_start_t = std::chrono::steady_clock::now();40}42~Timer()43{44if (m_message_on_completion) {45this->Log(strprintf("%s completed", m_title));46} else {47this->Log("completed");48}49}51void Log(const std::string& msg)52{53const std::string full_msg = this->LogMsg(msg);55if (m_log_category == BCLog::LogFlags::ALL) {56LogInfo("%s\n", full_msg);57} else {58LogDebug(m_log_category, "%s\n", full_msg);59}60}62std::string LogMsg(const std::string& msg)63{64const auto end_time{std::chrono::steady_clock::now()};65if (!m_start_t) {66return strprintf("%s: %s", m_prefix, msg);67}68const auto duration{end_time - *m_start_t};70if constexpr (std::is_same_v<TimeType, std::chrono::microseconds>) {71return 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>) {73return strprintf("%s: %s (%.2fms)", m_prefix, msg, Ticks<MillisecondsDouble>(duration));74} else if constexpr (std::is_same_v<TimeType, std::chrono::seconds>) {75return strprintf("%s: %s (%.2fs)", m_prefix, msg, Ticks<SecondsDouble>(duration));76} else {77static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type");78}79}81private:82std::optional<std::chrono::steady_clock::time_point> m_start_t{};84//! Log prefix; usually the name of the function this was created in.85const std::string m_prefix;87//! A descriptive message of what is being timed.88const std::string m_title;90//! Forwarded on to LogDebug if specified - has the effect of only91//! outputting the timing log when a particular debug= category is specified.92const BCLog::LogFlags m_log_category;94//! Whether to output the message again on completion.95const bool m_message_on_completion;96};98} // namespace BCLog101#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \102BCLog::Timer<std::chrono::microseconds> BITCOIN_UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)103#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \104BCLog::Timer<std::chrono::milliseconds> BITCOIN_UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)105#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \106BCLog::Timer<std::chrono::milliseconds> BITCOIN_UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category, /* msg_on_completion=*/false)107#define LOG_TIME_SECONDS(end_msg) \108BCLog::Timer<std::chrono::seconds> BITCOIN_UNIQUE_NAME(logging_timer)(__func__, end_msg)111#endif // BITCOIN_LOGGING_TIMER_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.