util/log, logging: Provide ShouldDebugLog and ShouldTraceLog instead of a generic ShouldLog

This commit is contained in:
Anthony Towns
2026-02-11 11:27:55 +10:00
parent abea304dd6
commit 34332dba2f
3 changed files with 22 additions and 14 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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__)