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 <ryan@ofsky.org>
This commit is contained in:
stickies-v 2025-12-16 15:37:21 +00:00
parent 8799eb7440
commit f5233f7e98
2 changed files with 33 additions and 20 deletions

View File

@ -11,6 +11,7 @@
#include <tinyformat.h>
#include <util/check.h>
#include <util/fs.h>
#include <util/log.h> // IWYU pragma: export
#include <util/string.h>
#include <util/time.h>
@ -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
{

32
src/util/log.h Normal file
View File

@ -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 <cstdint>
#include <source_location>
#include <string_view>
/// 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