From f5233f7e9827f8dd23414c7fcf49298420f9fc42 Mon Sep 17 00:00:00 2001 From: stickies-v Date: Tue, 16 Dec 2025 15:37:21 +0000 Subject: [PATCH] move-only: Move SourceLocation to util/log.h Introduce util/log.h as a lightweight header for code that only needs to emit log messages. Move SourceLocation into this header so log-emitting code no longer needs to include logging.h and depend on the full log management API. This is a move-only change and the first change of several changes that separate log generation from log handling. It also applies clang-format suggestions to the moved code. Review with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space Co-authored-by: Ryan Ofsky --- src/logging.h | 21 +-------------------- src/util/log.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 src/util/log.h diff --git a/src/logging.h b/src/logging.h index 2ab21071a06..2b4b657dc9e 100644 --- a/src/logging.h +++ b/src/logging.h @@ -11,6 +11,7 @@ #include #include #include +#include // IWYU pragma: export #include #include @@ -37,26 +38,6 @@ extern const char * const DEFAULT_DEBUGLOGFILE; extern bool fLogIPs; -/// Like std::source_location, but allowing to override the function name. -class SourceLocation -{ -public: - /// The func argument must be constructed from the C++11 __func__ macro. - /// Ref: https://en.cppreference.com/w/cpp/language/function.html#func - /// Non-static string literals are not supported. - SourceLocation(const char* func, - std::source_location loc = std::source_location::current()) - : m_func{func}, m_loc{loc} {} - - std::string_view file_name() const { return m_loc.file_name(); } - std::uint_least32_t line() const { return m_loc.line(); } - std::string_view function_name_short() const { return m_func; } - -private: - std::string_view m_func; - std::source_location m_loc; -}; - struct SourceLocationEqual { bool operator()(const SourceLocation& lhs, const SourceLocation& rhs) const noexcept { diff --git a/src/util/log.h b/src/util/log.h new file mode 100644 index 00000000000..7ad548631a7 --- /dev/null +++ b/src/util/log.h @@ -0,0 +1,32 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_UTIL_LOG_H +#define BITCOIN_UTIL_LOG_H + +#include +#include +#include + +/// Like std::source_location, but allowing to override the function name. +class SourceLocation +{ +public: + /// The func argument must be constructed from the C++11 __func__ macro. + /// Ref: https://en.cppreference.com/w/cpp/language/function.html#func + /// Non-static string literals are not supported. + SourceLocation(const char* func, + std::source_location loc = std::source_location::current()) + : m_func{func}, m_loc{loc} {} + + std::string_view file_name() const { return m_loc.file_name(); } + std::uint_least32_t line() const { return m_loc.line(); } + std::string_view function_name_short() const { return m_func; } + +private: + std::string_view m_func; + std::source_location m_loc; +}; + +#endif // BITCOIN_UTIL_LOG_H