util/log, logging: Provide ShouldDebugLog and ShouldTraceLog instead of a generic ShouldLog
What changed, and why it matters
This commit is a straightforward internal code cleanup in Bitcoin Core's logging system. It replaces one generic helper function with two more specific ones (one for debug logs, one for trace logs) and updates the macros that use them. There is no change to user-facing behavior, no bug fix, and no security relevance visible in the code.
No security action needed. Treat as normal code maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors util::log::ShouldLog(Category, Level) into ShouldDebugLog(Category) and ShouldTraceLog(Category). It updates detail_LogIfCategoryAndLevelEnabled to take a function pointer and hardcodes the corresponding Level, removes an Assume(level < Info) assertion, and updates the one direct caller in validationinterface.cpp. The functional behavior remains identical: the same log levels and categories are checked before emitting debug/trace log entries.
Changed components
src/logging.cppsrc/util/log.hsrc/validationinterface.cppInspect captured patch +22 / −14
diff --git a/src/logging.cpp b/src/logging.cpp
index fc6dd1b8..4b6fd96b 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<BCLog::LogFlags>(category), level);
+ return LogInstance().WillLogCategoryLevel(static_cast<BCLog::LogFlags>(category), util::log::Level::Debug);
+}
+
+bool util::log::ShouldTraceLog(Category category)
+{
+ return LogInstance().WillLogCategoryLevel(static_cast<BCLog::LogFlags>(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 c9b70c9d..130e740c 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 45f38b37..ec2f2047 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__)
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.