diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -1387,7 +1387,7 @@ strprintf(_("Unsupported logging category %s=%s."), "-debug", cat)); } - logCategories |= flag; + g_logger->EnableCategory(static_cast(flag)); } } } @@ -1400,7 +1400,7 @@ InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat)); } - logCategories &= ~flag; + g_logger->DisableCategory(static_cast(flag)); } } @@ -1740,7 +1740,8 @@ #ifndef WIN32 CreatePidFile(GetPidFile(), getpid()); #endif - if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories != BCLog::NONE)) { + bool default_shrinkdebugfile = g_logger->DefaultShrinkDebugFile(); + if (gArgs.GetBoolArg("-shrinkdebugfile", default_shrinkdebugfile)) { // Do this first since it both loads a bunch of debug.log into memory, // and because this needs to happen before any other debug.log printing. g_logger->ShrinkDebugFile(); diff --git a/src/logging.h b/src/logging.h --- a/src/logging.h +++ b/src/logging.h @@ -21,8 +21,6 @@ extern bool fLogIPs; -extern std::atomic logCategories; - namespace BCLog { enum LogFlags : uint32_t { @@ -65,6 +63,12 @@ */ std::atomic_bool fStartedNewLine{true}; + /** + * Log categories bitfield. Leveldb/libevent need special handling if their + * flags are changed at runtime. + */ + std::atomic logCategories{0}; + std::string LogTimestampStr(const std::string &str); public: @@ -83,6 +87,15 @@ void OpenDebugLog(); void ShrinkDebugFile(); + + void EnableCategory(LogFlags category); + void DisableCategory(LogFlags category); + + /** Return true if log accepts specified category */ + bool WillLogCategory(LogFlags category) const; + + /** Default for whether ShrinkDebugFile should be run */ + bool DefaultShrinkDebugFile() const; }; } // namespace BCLog @@ -90,8 +103,8 @@ extern std::unique_ptr g_logger; /** Return true if log accepts specified category */ -static inline bool LogAcceptCategory(uint32_t category) { - return (logCategories.load(std::memory_order_relaxed) & category) != 0; +static inline bool LogAcceptCategory(BCLog::LogFlags category) { + return g_logger && g_logger->WillLogCategory(category); } /** Returns a string with the supported log categories */ @@ -102,7 +115,7 @@ #define LogPrint(category, ...) \ do { \ - if (g_logger && LogAcceptCategory((category))) { \ + if (g_logger && g_logger->WillLogCategory((category))) { \ g_logger->LogPrintStr(tfm::format(__VA_ARGS__)); \ } \ } while (0) diff --git a/src/logging.cpp b/src/logging.cpp --- a/src/logging.cpp +++ b/src/logging.cpp @@ -11,12 +11,6 @@ bool fLogIPs = DEFAULT_LOGIPS; -/** - * Log categories bitfield. Leveldb/libevent need special handling if their - * flags are changed at runtime. - */ -std::atomic logCategories(0); - std::unique_ptr g_logger; static int FileWriteStr(const std::string &str, FILE *fp) { @@ -188,3 +182,19 @@ } else if (file != nullptr) fclose(file); } + +void BCLog::Logger::EnableCategory(LogFlags category) { + logCategories |= category; +} + +void BCLog::Logger::DisableCategory(LogFlags category) { + logCategories &= ~category; +} + +bool BCLog::Logger::WillLogCategory(LogFlags category) const { + return (logCategories.load(std::memory_order_relaxed) & category) != 0; +} + +bool BCLog::Logger::DefaultShrinkDebugFile() const { + return logCategories != BCLog::NONE; +}