From bd971bffb02c7b06aac9a479f7e5ed8f71de2bec Mon Sep 17 00:00:00 2001 From: laanwj <126646+laanwj@users.noreply.github.com> Date: Tue, 24 May 2022 19:52:49 +0200 Subject: [PATCH] logging: Unconditionally log levels >= WARN Messages with level `WARN` or higher should be logged even when the category is not provided with `-debug=`, to make sure important warnings are not lost. --- src/dbwrapper.cpp | 4 ++-- src/logging.h | 13 +++++++++---- src/timedata.cpp | 2 +- src/wallet/coinselection.cpp | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 50a601c684308..83b59dc56d9dc 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -19,7 +19,7 @@ class CBitcoinLevelDBLogger : public leveldb::Logger { // 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)) { + if (!LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug)) { return; } char buffer[500]; @@ -186,7 +186,7 @@ CDBWrapper::~CDBWrapper() bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) { - const bool log_memory = LogAcceptCategory(BCLog::LEVELDB); + const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug); double mem_before = 0; if (log_memory) { mem_before = DynamicMemoryUsage() / 1024.0 / 1024; diff --git a/src/logging.h b/src/logging.h index 421c3395c2c27..8b4459400bb01 100644 --- a/src/logging.h +++ b/src/logging.h @@ -166,9 +166,14 @@ namespace BCLog { BCLog::Logger& LogInstance(); -/** Return true if log accepts specified category */ -static inline bool LogAcceptCategory(BCLog::LogFlags category) +/** Return true if log accepts specified category, at the specified level. */ +static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level) { + // Log messages at Warning and Error level unconditionally, so that + // important troubleshooting information doesn't get lost. + if (level >= BCLog::Level::Warning) { + return true; + } return LogInstance().WillLogCategory(category); } @@ -203,14 +208,14 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st // evaluating arguments when logging for the category is not enabled. #define LogPrint(category, ...) \ do { \ - if (LogAcceptCategory((category))) { \ + if (LogAcceptCategory((category), BCLog::Level::Debug)) { \ LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__); \ } \ } while (0) #define LogPrintLevel(level, category, ...) \ do { \ - if (LogAcceptCategory((category))) { \ + if (LogAcceptCategory((category), (level))) { \ LogPrintLevel_(category, level, __VA_ARGS__); \ } \ } while (0) diff --git a/src/timedata.cpp b/src/timedata.cpp index 06659871e5e48..7faf22aba0d33 100644 --- a/src/timedata.cpp +++ b/src/timedata.cpp @@ -99,7 +99,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample) } } - if (LogAcceptCategory(BCLog::NET)) { + if (LogAcceptCategory(BCLog::NET, BCLog::Level::Debug)) { std::string log_message{"time data samples: "}; for (const int64_t n : vSorted) { log_message += strprintf("%+d ", n); diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index e710787a2639d..07df8d9fc8a48 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -307,7 +307,7 @@ std::optional KnapsackSolver(std::vector& groups, } } - if (LogAcceptCategory(BCLog::SELECTCOINS)) { + if (LogAcceptCategory(BCLog::SELECTCOINS, BCLog::Level::Debug)) { std::string log_message{"Coin selection best subset: "}; for (unsigned int i = 0; i < applicable_groups.size(); i++) { if (vfBest[i]) {