From 904c0d07bb3ac2f4e0d10332ccb3cb57c01ec9e9 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 24 Mar 2026 23:14:56 +1000 Subject: [PATCH 1/9] util/stdmutex: Drop StdLockGuard --- src/util/stdmutex.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/util/stdmutex.h b/src/util/stdmutex.h index 2cc05013a59..ab89a987685 100644 --- a/src/util/stdmutex.h +++ b/src/util/stdmutex.h @@ -40,6 +40,4 @@ public: // Provide STDLOCK(..) wrapper around StdMutex::Guard that checks the lock is not already held #define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)} -using StdLockGuard = StdMutex::Guard; // TODO: remove, provided for backwards compat only - #endif // BITCOIN_UTIL_STDMUTEX_H From 72e92d67df7ffdfb0dc8f91801341385b60e0ce9 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Mon, 8 Dec 2025 22:19:07 +1000 Subject: [PATCH 2/9] logging: Protect ShrinkDebugFile by m_cs We should not be logging while shrinking the debug file, so make sure that's true by using our mutex. --- src/logging.cpp | 11 ++++++++++- src/logging.h | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 3373063c05c..8f52312819c 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -514,6 +514,8 @@ void BCLog::Logger::LogPrint_(util::log::Entry entry) void BCLog::Logger::ShrinkDebugFile() { + STDLOCK(m_cs); + // Amount of debug.log to save at end when shrinking (must fit in memory) constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; @@ -535,7 +537,14 @@ void BCLog::Logger::ShrinkDebugFile() // Restart the file with some of the end std::vector vch(RECENT_DEBUG_HISTORY_SIZE, 0); if (fseek(file, -((long)vch.size()), SEEK_END)) { - LogWarning("Failed to shrink debug log file: fseek(...) failed"); + // LogWarning, except with m_cs held + LogPrint_({ + .category = BCLog::ALL, + .level = Level::Warning, + .should_ratelimit = true, + .source_loc = SourceLocation{__func__}, + .message = "Failed to shrink debug log file: fseek(...) failed", + }); fclose(file); return; } diff --git a/src/logging.h b/src/logging.h index 67f5d6ef007..4d40694278b 100644 --- a/src/logging.h +++ b/src/logging.h @@ -227,7 +227,7 @@ namespace BCLog { */ void DisableLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs); - void ShrinkDebugFile(); + void ShrinkDebugFile() EXCLUSIVE_LOCKS_REQUIRED(!m_cs); std::unordered_map CategoryLevels() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs) { From f69d1ae56dbdf062ea7a39d6788378c77b621013 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Mon, 8 Dec 2025 22:29:39 +1000 Subject: [PATCH 3/9] util/log: Provide util::log::NO_RATE_LIMIT to avoid rate limits --- doc/developer-notes.md | 14 +++++++---- src/logging.cpp | 3 +-- src/logging.h | 1 - src/test/logging_tests.cpp | 2 +- src/util/log.h | 49 +++++++++++++++++++++++++------------- src/validation.cpp | 4 ++-- 6 files changed, 47 insertions(+), 26 deletions(-) diff --git a/doc/developer-notes.md b/doc/developer-notes.md index 18691811aa4..4738148d914 100644 --- a/doc/developer-notes.md +++ b/doc/developer-notes.md @@ -736,13 +736,12 @@ logging messages. They should be used as follows: most of the time, and it should be used for log messages that are useful for debugging and can reasonably be enabled on a production system (that has sufficient free storage space). They will be logged - if the program is started with `-debug=category` or `-debug=1`. + if the program is started with `-debug=category` or `-debug=1`, or + the category is enabled through the `logging` RPC. - `LogInfo(fmt, params...)` should only be used rarely, e.g. for startup messages or for infrequent and important events such as a new block tip - being found or a new outbound connection being made. These log messages - are unconditional, so care must be taken that they can't be used by an - attacker to fill up storage. + being found or a new outbound connection being made. - `LogError(fmt, params...)` should be used in place of `LogInfo` for severe problems that require the node (or a subsystem) to shut down @@ -764,6 +763,13 @@ Note that the format strings and parameters of `LogDebug` and `LogTrace` are only evaluated if the logging category is enabled, so you must be careful to avoid side-effects in those expressions. +While `LogInfo`, `LogWarning` and `LogError` messages should be rare, +in case there are circumstances where they are not, those messages +are automatically rate-limited to prevent potential disk-filling +attacks. For the cases where this protection is undesirable, +rate-limiting can be avoided with the `util::log::NO_RATE_LIMIT` tag, eg +`LogInfo(util::log::NO_RATE_LIMIT, "UpdateTip: new best=%s ...",...)`. + ## General C++ For general C++ guidelines, you may refer to the [C++ Core diff --git a/src/logging.cpp b/src/logging.cpp index 8f52312819c..0edc970df67 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -572,8 +572,7 @@ void BCLog::LogRateLimiter::Reset() } for (const auto& [source_loc, stats] : source_locations) { if (stats.m_dropped_bytes == 0) continue; - LogPrintLevel_( - LogFlags::ALL, Level::Warning, /*should_ratelimit=*/false, + LogWarning(util::log::NO_RATE_LIMIT, "Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.", source_loc.file_name(), source_loc.line(), source_loc.function_name_short(), stats.m_dropped_bytes, Ticks(m_reset_window)); diff --git a/src/logging.h b/src/logging.h index 4d40694278b..9ff1c7ea4d2 100644 --- a/src/logging.h +++ b/src/logging.h @@ -276,7 +276,6 @@ namespace BCLog { bool DefaultShrinkDebugFile() const; }; - } // namespace BCLog BCLog::Logger& LogInstance(); diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp index 37a43e9740c..550ef85cc4a 100644 --- a/src/test/logging_tests.cpp +++ b/src/test/logging_tests.cpp @@ -382,7 +382,7 @@ void LogFromLocation(Location location, const std::string& message) { LogDebug(BCLog::LogFlags::HTTP, "%s\n", message); return; case Location::INFO_NOLIMIT: - LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/false, "%s\n", message); + LogInfo(util::log::NO_RATE_LIMIT, "%s\n", message); return; } // no default case, so the compiler can warn about missing cases assert(false); diff --git a/src/util/log.h b/src/util/log.h index 9394161f61d..64df6373380 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -43,6 +43,12 @@ namespace util::log { /** Opaque to util::log; interpreted by consumers (e.g., BCLog::LogFlags). */ using Category = uint64_t; +//! Structure and constant for tagging not to rate limit. +struct NoRateLimitTag { + explicit NoRateLimitTag() = default; +}; +inline constexpr NoRateLimitTag NO_RATE_LIMIT{}; + enum class Level { Trace = 0, // High-volume or detailed logging for development/debugging Debug, // Reasonably noisy logging, but still usable in production @@ -68,15 +74,9 @@ bool ShouldLog(Category category, Level level); /** Send message to be logged. Applications using the logging library need to provide this. */ void Log(Entry entry); -} // namespace util::log - -namespace BCLog { -//! Alias for compatibility. Prefer util::log::Level over BCLog::Level in new code. -using Level = util::log::Level; -} // namespace BCLog template -inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags flag, BCLog::Level level, bool should_ratelimit, util::ConstevalFormatString fmt, const Args&... args) +inline void LogPrintFormatInternal_(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, bool should_ratelimit, util::ConstevalFormatString fmt, const Args&... args) { std::string log_msg; try { @@ -92,17 +92,35 @@ inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags .message = std::move(log_msg)}); } +template +inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, util::ConstevalFormatString fmt, const Args&... args) +{ + return LogPrintFormatInternal_(std::move(source_loc), flag, level, /*should_ratelimit=*/true, fmt, args...); +} + +template +inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, util::log::NoRateLimitTag, util::ConstevalFormatString fmt, const Args&... args) +{ + return LogPrintFormatInternal_(std::move(source_loc), flag, level, /*should_ratelimit=*/false, fmt, args...); +} +} // namespace util::log + +namespace BCLog { +//! Alias for compatibility. Prefer util::log::Level over BCLog::Level in new code. +using Level = util::log::Level; +} // namespace BCLog + // Allow __func__ to be used in any context without warnings: // NOLINTNEXTLINE(bugprone-lambda-function-name) -#define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) +#define LogPrintLevel_(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__) // Log unconditionally. Uses basic rate limiting to mitigate disk filling attacks. // Be conservative when using functions that unconditionally log to debug.log! // It should not be the case that an inbound peer can fill up a user's storage // with debug.log entries. -#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) -#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) -#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) +#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Info, __VA_ARGS__) +#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__) +#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Error, __VA_ARGS__) // Use a macro instead of a function for conditional logging to prevent // evaluating arguments when logging for the category is not enabled. @@ -113,14 +131,13 @@ inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags #define detail_LogIfCategoryAndLevelEnabled(category, level, ...) \ do { \ if (util::log::ShouldLog((category), (level))) { \ - bool rate_limit{level >= BCLog::Level::Info}; \ - Assume(!rate_limit); /*Only called with the levels below*/ \ - LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ + Assume((level) < util::log::Level::Info); /*Only called with the levels below*/ \ + LogPrintLevel_((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \ } \ } while (0) // Log conditionally, prefixing the output with the passed category name. -#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) -#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Trace, __VA_ARGS__) +#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::Level::Debug, __VA_ARGS__) +#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::Level::Trace, __VA_ARGS__) #endif // BITCOIN_UTIL_LOG_H diff --git a/src/validation.cpp b/src/validation.cpp index f85a834f2a4..f7a4c87766e 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2864,8 +2864,8 @@ static void UpdateTipLog( AssertLockHeld(::cs_main); - // Disable rate limiting in LogPrintLevel_ so this source location may log during IBD. - LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Info, /*should_ratelimit=*/false, "%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)%s\n", + // Disable rate limiting as this may log frequently during IBD. + LogInfo(util::log::NO_RATE_LIMIT, "%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)%s\n", prefix, func_name, tip->GetBlockHash().ToString(), tip->nHeight, tip->nVersion, log(tip->nChainWork.getdouble()) / log(2.0), tip->m_chain_tx_count, From 58113e58334cf4486b3398dfafbeef0a08ad56aa Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 11 Feb 2026 08:08:07 +1000 Subject: [PATCH 4/9] util/log: Rename LogPrintLevel_ into detail_ namespace After the previous commit, LogPrintLevel_ is only used to implement other macros. --- src/util/log.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/log.h b/src/util/log.h index 64df6373380..c9b70c9d491 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -112,15 +112,15 @@ using Level = util::log::Level; // Allow __func__ to be used in any context without warnings: // NOLINTNEXTLINE(bugprone-lambda-function-name) -#define LogPrintLevel_(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__) +#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__) // Log unconditionally. Uses basic rate limiting to mitigate disk filling attacks. // Be conservative when using functions that unconditionally log to debug.log! // It should not be the case that an inbound peer can fill up a user's storage // with debug.log entries. -#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Info, __VA_ARGS__) -#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__) -#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, util::log::Level::Error, __VA_ARGS__) +#define LogInfo(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Info, __VA_ARGS__) +#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__) +#define LogError(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Error, __VA_ARGS__) // Use a macro instead of a function for conditional logging to prevent // evaluating arguments when logging for the category is not enabled. @@ -132,7 +132,7 @@ using Level = util::log::Level; do { \ if (util::log::ShouldLog((category), (level))) { \ Assume((level) < util::log::Level::Info); /*Only called with the levels below*/ \ - LogPrintLevel_((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \ + detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \ } \ } while (0) From abea304dd6d726acaeab3aab99df5c24467adc2f Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 11 Feb 2026 08:15:09 +1000 Subject: [PATCH 5/9] logging: Move GetLogCategory into Logger class --- src/logging.cpp | 2 +- src/logging.h | 6 +++--- src/test/logging_tests.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 0edc970df67..fc6dd1b8268 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -224,7 +224,7 @@ static const std::unordered_map LOG_CATEGORIES_BY_ }(LOG_CATEGORIES_BY_STR) }; -std::optional GetLogCategory(std::string_view str) +std::optional BCLog::Logger::GetLogCategory(std::string_view str) { if (str.empty() || str == "1" || str == "all") { return BCLog::ALL; diff --git a/src/logging.h b/src/logging.h index 9ff1c7ea4d2..a727dc249d1 100644 --- a/src/logging.h +++ b/src/logging.h @@ -275,6 +275,9 @@ namespace BCLog { static std::string LogLevelToStr(BCLog::Level level); bool DefaultShrinkDebugFile() const; + + //! Return log flag if str parses as a log category. + static std::optional GetLogCategory(std::string_view str); }; } // namespace BCLog @@ -286,7 +289,4 @@ static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level leve return LogInstance().WillLogCategoryLevel(category, level); } -/// Return log flag if str parses as a log category. -std::optional GetLogCategory(std::string_view str); - #endif // BITCOIN_LOGGING_H diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp index 550ef85cc4a..5595fe16de4 100644 --- a/src/test/logging_tests.cpp +++ b/src/test/logging_tests.cpp @@ -166,7 +166,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup) const auto category_names = SplitString(concatenated_category_names, ','); for (const auto& category_name : category_names) { const auto trimmed_category_name = TrimString(category_name); - const auto category{*Assert(GetLogCategory(trimmed_category_name))}; + const auto category{*Assert(BCLog::Logger::GetLogCategory(trimmed_category_name))}; expected_category_names.emplace_back(category, trimmed_category_name); } From 34332dba2f6f6892859ecb62daa9a2add76ae05e Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 11 Feb 2026 11:27:55 +1000 Subject: [PATCH 6/9] util/log, logging: Provide ShouldDebugLog and ShouldTraceLog instead of a generic ShouldLog --- src/logging.cpp | 9 +++++++-- src/util/log.h | 25 ++++++++++++++----------- src/validationinterface.cpp | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index fc6dd1b8268..4b6fd96b1ec 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -612,9 +612,14 @@ bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::stri return true; } -bool util::log::ShouldLog(Category category, Level level) +bool util::log::ShouldDebugLog(Category category) { - return LogInstance().WillLogCategoryLevel(static_cast(category), level); + return LogInstance().WillLogCategoryLevel(static_cast(category), util::log::Level::Debug); +} + +bool util::log::ShouldTraceLog(Category category) +{ + return LogInstance().WillLogCategoryLevel(static_cast(category), util::log::Level::Trace); } void util::log::Log(util::log::Entry entry) diff --git a/src/util/log.h b/src/util/log.h index c9b70c9d491..130e740cd0e 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -68,9 +68,13 @@ struct Entry { std::string message; }; -/** Return whether messages with specified category and level should be logged. Applications using - * the logging library need to provide this. */ -bool ShouldLog(Category category, Level level); +/// Return whether messages with specified category should be debug logged. +/// Applications using the logging library need to provide this. +bool ShouldDebugLog(Category category); + +/// Return whether messages with specified category should be trace logged. +/// Applications using the logging library need to provide this. +bool ShouldTraceLog(Category category); /** Send message to be logged. Applications using the logging library need to provide this. */ void Log(Entry entry); @@ -128,16 +132,15 @@ using Level = util::log::Level; // Log by prefixing the output with the passed category name and severity level. This logs conditionally if // the category is allowed. No rate limiting is applied, because users specifying -debug are assumed to be // developers or power users who are aware that -debug may cause excessive disk usage due to logging. -#define detail_LogIfCategoryAndLevelEnabled(category, level, ...) \ - do { \ - if (util::log::ShouldLog((category), (level))) { \ - Assume((level) < util::log::Level::Info); /*Only called with the levels below*/ \ - detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \ - } \ +#define detail_LogIfCategoryAndLevelEnabled(category, shouldlog, level, ...) \ + do { \ + if (shouldlog(category)) { \ + detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \ + } \ } while (0) // Log conditionally, prefixing the output with the passed category name. -#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::Level::Debug, __VA_ARGS__) -#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::Level::Trace, __VA_ARGS__) +#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__) +#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldTraceLog, util::log::Level::Trace, __VA_ARGS__) #endif // BITCOIN_UTIL_LOG_H diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp index 45f38b3740b..ec2f2047da6 100644 --- a/src/validationinterface.cpp +++ b/src/validationinterface.cpp @@ -171,7 +171,7 @@ void ValidationSignals::SyncWithValidationInterfaceQueue() } while (0) #define LOG_MSG(fmt, ...) \ - (ShouldLog(BCLog::VALIDATION, BCLog::Level::Debug) ? tfm::format((fmt), __VA_ARGS__) : std::string{}) + (util::log::ShouldDebugLog(BCLog::VALIDATION) ? tfm::format((fmt), __VA_ARGS__) : std::string{}) #define LOG_EVENT(fmt, ...) \ LogDebug(BCLog::VALIDATION, fmt, __VA_ARGS__) From 611878b46f702f1844e4c89b7fd36a9a54e09167 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 11 Feb 2026 09:42:45 +1000 Subject: [PATCH 7/9] scripted-diff: logging: Drop LogAcceptCategory -BEGIN VERIFY SCRIPT- sed -i 's/LogAcceptCategory(\(.*\), [a-zA-Z:]*::Level::Debug)/util::log::ShouldDebugLog(\1)/g' $(git grep -l LogAcceptCategory -- '*.cpp') sed -i 's/LogAcceptCategory(\(.*\), [a-zA-Z:]*::Level::Trace)/util::log::ShouldTraceLog(\1)/g' $(git grep -l LogAcceptCategory -- '*.cpp') sed -i '/Return true if log accepts specified category/,/^$/d' src/logging.h -END VERIFY SCRIPT- --- src/blockencodings.cpp | 2 +- src/dbwrapper.cpp | 4 ++-- src/ipc/capnp/protocol.cpp | 4 ++-- src/logging.h | 6 ------ src/net_processing.cpp | 2 +- src/wallet/coinselection.cpp | 2 +- src/wallet/sqlite.cpp | 2 +- 7 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp index 18799cd8083..064e0853748 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -219,7 +219,7 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector< return READ_STATUS_FAILED; // Possible Short ID collision } - if (LogAcceptCategory(BCLog::CMPCTBLOCK, BCLog::Level::Debug)) { + if (util::log::ShouldDebugLog(BCLog::CMPCTBLOCK)) { const uint256 hash{block.GetHash()}; uint32_t tx_missing_size{0}; for (const auto& tx : vtx_missing) tx_missing_size += tx->ComputeTotalSize(); diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 3212f6d3701..fde8e8e44f8 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -59,7 +59,7 @@ public: // This code is adapted from posix_logger.h, which is why it is using vsprintf. // Please do not do this in normal code void Logv(const char * format, va_list ap) override { - if (!LogAcceptCategory(BCLog::LEVELDB, util::log::Level::Debug)) { + if (!util::log::ShouldDebugLog(BCLog::LEVELDB)) { return; } char buffer[500]; @@ -278,7 +278,7 @@ CDBWrapper::~CDBWrapper() void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) { - const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, util::log::Level::Debug); + const bool log_memory = util::log::ShouldDebugLog(BCLog::LEVELDB); double mem_before = 0; if (log_memory) { mem_before = DynamicMemoryUsage() / double(1_MiB); diff --git a/src/ipc/capnp/protocol.cpp b/src/ipc/capnp/protocol.cpp index 2645e46f70f..e68ce872fba 100644 --- a/src/ipc/capnp/protocol.cpp +++ b/src/ipc/capnp/protocol.cpp @@ -33,8 +33,8 @@ namespace { mp::Log GetRequestedIPCLogLevel() { - if (LogAcceptCategory(BCLog::IPC, BCLog::Level::Trace)) return mp::Log::Trace; - if (LogAcceptCategory(BCLog::IPC, BCLog::Level::Debug)) return mp::Log::Debug; + if (util::log::ShouldTraceLog(BCLog::IPC)) return mp::Log::Trace; + if (util::log::ShouldDebugLog(BCLog::IPC)) return mp::Log::Debug; // Info, Warning, and Error are logged unconditionally return mp::Log::Info; diff --git a/src/logging.h b/src/logging.h index a727dc249d1..4bdcd0f241d 100644 --- a/src/logging.h +++ b/src/logging.h @@ -283,10 +283,4 @@ namespace BCLog { BCLog::Logger& LogInstance(); -/** Return true if log accepts specified category, at the specified level. */ -static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level) -{ - return LogInstance().WillLogCategoryLevel(category, level); -} - #endif // BITCOIN_LOGGING_H diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 6d35db931a5..94861130262 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2607,7 +2607,7 @@ void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlo resp.txn[i] = block.vtx[req.indexes[i]]; } - if (LogAcceptCategory(BCLog::CMPCTBLOCK, BCLog::Level::Debug)) { + if (util::log::ShouldDebugLog(BCLog::CMPCTBLOCK)) { uint32_t tx_requested_size{0}; for (const auto& tx : resp.txn) tx_requested_size += tx->ComputeTotalSize(); LogDebug(BCLog::CMPCTBLOCK, "Peer %d sent us a GETBLOCKTXN for block %s, sending a BLOCKTXN with %u txns. (%u bytes)\n", pfrom.GetId(), block.GetHash().ToString(), resp.txn.size(), tx_requested_size); diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index d6ea6851e64..1d9db426f6b 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -732,7 +732,7 @@ util::Result KnapsackSolver(std::vector& groups, c result.AddInput(*lowest_larger); } - if (LogAcceptCategory(BCLog::SELECTCOINS, BCLog::Level::Debug)) { + if (util::log::ShouldDebugLog(BCLog::SELECTCOINS)) { std::string log_message{"Coin selection best subset: "}; for (unsigned int i = 0; i < applicable_groups.size(); i++) { if (vfBest[i]) { diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index 3d6583bb037..03adc7ec95e 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -267,7 +267,7 @@ void SQLiteDatabase::Open(int additional_flags) throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(ret))); } // Trace SQL statements if tracing is enabled with -debug=walletdb -loglevel=walletdb:trace - if (LogAcceptCategory(BCLog::WALLETDB, BCLog::Level::Trace)) { + if (util::log::ShouldTraceLog(BCLog::WALLETDB)) { ret = sqlite3_trace_v2(m_db, SQLITE_TRACE_STMT, TraceSqlCallback, this); if (ret != SQLITE_OK) { LogWarning("Failed to enable SQL tracing for %s", Filename()); From 57d7495fe5cbdaee0584c239f18b4efc2ab8cd80 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 11 Feb 2026 12:47:58 +1000 Subject: [PATCH 8/9] IWYU fixes Add missing includes of logging.h in preparation for the next commit, switching to util/log.h. Also removes some unnecessary util/check.h includes that CI complains about. --- src/bitcoind.cpp | 1 + src/signet.cpp | 1 - src/test/fuzz/pcp.cpp | 1 + src/test/node_init_tests.cpp | 1 + src/zmq/zmqutil.cpp | 1 - 5 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index ac731587683..debc7845618 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include diff --git a/src/signet.cpp b/src/signet.cpp index d0f8aa28515..c37ea52fa8c 100644 --- a/src/signet.cpp +++ b/src/signet.cpp @@ -13,7 +13,6 @@ #include