diff options
-rw-r--r-- | src/httpserver.cpp | 4 | ||||
-rw-r--r-- | src/httpserver.h | 2 | ||||
-rw-r--r-- | src/init.cpp | 6 | ||||
-rw-r--r-- | src/interfaces/node.cpp | 2 | ||||
-rw-r--r-- | src/logging.cpp | 23 | ||||
-rw-r--r-- | src/logging.h | 16 | ||||
-rw-r--r-- | src/rpc/misc.cpp | 30 |
7 files changed, 55 insertions, 28 deletions
diff --git a/src/httpserver.cpp b/src/httpserver.cpp index b8b338dfbc..bd08b04c0f 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -364,8 +364,8 @@ bool InitHTTPServer() // Update libevent's log handling. Returns false if our version of // libevent doesn't support debug logging, in which case we should // clear the BCLog::LIBEVENT flag. - if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) { - logCategories &= ~BCLog::LIBEVENT; + if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) { + g_logger->DisableCategory(BCLog::LIBEVENT); } #ifdef WIN32 diff --git a/src/httpserver.h b/src/httpserver.h index f17be65962..8132c887b5 100644 --- a/src/httpserver.h +++ b/src/httpserver.h @@ -32,7 +32,7 @@ void InterruptHTTPServer(); /** Stop HTTP server */ void StopHTTPServer(); -/** Change logging level for libevent. Removes BCLog::LIBEVENT from logCategories if +/** Change logging level for libevent. Removes BCLog::LIBEVENT from log categories if * libevent doesn't support debug logging.*/ bool UpdateHTTPServerLogging(bool enable); diff --git a/src/init.cpp b/src/init.cpp index c14597d517..ccaa09a85e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -968,7 +968,7 @@ bool AppInitParameterInteraction() InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat)); continue; } - logCategories |= flag; + g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag)); } } } @@ -980,7 +980,7 @@ bool AppInitParameterInteraction() InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat)); continue; } - logCategories &= ~flag; + g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag)); } // Check for -debugnet @@ -1232,7 +1232,7 @@ bool AppInitMain() CreatePidFile(GetPidFile(), getpid()); #endif if (g_logger->fPrintToDebugLog) { - if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) { + if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) { // 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/interfaces/node.cpp b/src/interfaces/node.cpp index 55786c807f..53d2359caf 100644 --- a/src/interfaces/node.cpp +++ b/src/interfaces/node.cpp @@ -60,7 +60,7 @@ class NodeImpl : public Node void initLogging() override { InitLogging(); } void initParameterInteraction() override { InitParameterInteraction(); } std::string getWarnings(const std::string& type) override { return GetWarnings(type); } - uint32_t getLogCategories() override { return ::logCategories; } + uint32_t getLogCategories() override { return g_logger->GetCategoryMask(); } bool baseInitialize() override { return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() && diff --git a/src/logging.cpp b/src/logging.cpp index ed225a6a67..7604c0fd94 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -26,9 +26,6 @@ BCLog::Logger* const g_logger = new BCLog::Logger(); bool fLogIPs = DEFAULT_LOGIPS; -/** Log categories bitfield. */ -std::atomic<uint32_t> logCategories(0); - static int FileWriteStr(const std::string &str, FILE *fp) { return fwrite(str.data(), 1, str.size(), fp); @@ -62,6 +59,26 @@ bool BCLog::Logger::OpenDebugLog() return true; } +void BCLog::Logger::EnableCategory(BCLog::LogFlags flag) +{ + logCategories |= flag; +} + +void BCLog::Logger::DisableCategory(BCLog::LogFlags flag) +{ + logCategories &= ~flag; +} + +bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const +{ + return (logCategories.load(std::memory_order_relaxed) & category) != 0; +} + +bool BCLog::Logger::DefaultShrinkDebugFile() const +{ + return logCategories == BCLog::NONE; +} + struct CLogCategoryDesc { uint32_t flag; diff --git a/src/logging.h b/src/logging.h index c27a71168b..7a1c25233a 100644 --- a/src/logging.h +++ b/src/logging.h @@ -23,8 +23,6 @@ extern const char * const DEFAULT_DEBUGLOGFILE; extern bool fLogIPs; -extern std::atomic<uint32_t> logCategories; - struct CLogCategoryActive { std::string category; @@ -72,6 +70,9 @@ namespace BCLog { */ std::atomic_bool fStartedNewLine{true}; + /** Log categories bitfield. */ + std::atomic<uint32_t> logCategories{0}; + std::string LogTimestampStr(const std::string& str); public: @@ -92,6 +93,13 @@ namespace BCLog { fs::path GetDebugLogPath() const; bool OpenDebugLog(); void ShrinkDebugFile(); + + uint32_t GetCategoryMask() const { return logCategories.load(); } + void EnableCategory(LogFlags flag); + void DisableCategory(LogFlags flag); + bool WillLogCategory(LogFlags category) const; + + bool DefaultShrinkDebugFile() const; }; } // namespace BCLog @@ -99,9 +107,9 @@ namespace BCLog { extern BCLog::Logger* const g_logger; /** Return true if log accepts specified category */ -static inline bool LogAcceptCategory(uint32_t category) +static inline bool LogAcceptCategory(BCLog::LogFlags category) { - return (logCategories.load(std::memory_order_relaxed) & category) != 0; + return g_logger->WillLogCategory(category); } /** Returns a string with the log categories. */ diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 6754407dbd..26bf21356e 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -346,9 +346,8 @@ UniValue getmemoryinfo(const JSONRPCRequest& request) } } -uint32_t getCategoryMask(UniValue cats) { +void EnableOrDisableLogCategories(UniValue cats, bool enable) { cats = cats.get_array(); - uint32_t mask = 0; for (unsigned int i = 0; i < cats.size(); ++i) { uint32_t flag = 0; std::string cat = cats[i].get_str(); @@ -356,11 +355,14 @@ uint32_t getCategoryMask(UniValue cats) { throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat); } if (flag == BCLog::NONE) { - return 0; + return; + } + if (enable) { + g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag)); + } else { + g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag)); } - mask |= flag; } - return mask; } UniValue logging(const JSONRPCRequest& request) @@ -399,25 +401,25 @@ UniValue logging(const JSONRPCRequest& request) ); } - uint32_t originalLogCategories = logCategories; + uint32_t original_log_categories = g_logger->GetCategoryMask(); if (request.params[0].isArray()) { - logCategories |= getCategoryMask(request.params[0]); + EnableOrDisableLogCategories(request.params[0], true); } - if (request.params[1].isArray()) { - logCategories &= ~getCategoryMask(request.params[1]); + EnableOrDisableLogCategories(request.params[1], false); } + uint32_t updated_log_categories = g_logger->GetCategoryMask(); + uint32_t changed_log_categories = original_log_categories ^ updated_log_categories; // Update libevent logging if BCLog::LIBEVENT has changed. // If the library version doesn't allow it, UpdateHTTPServerLogging() returns false, // in which case we should clear the BCLog::LIBEVENT flag. // Throw an error if the user has explicitly asked to change only the libevent // flag and it failed. - uint32_t changedLogCategories = originalLogCategories ^ logCategories; - if (changedLogCategories & BCLog::LIBEVENT) { - if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) { - logCategories &= ~BCLog::LIBEVENT; - if (changedLogCategories == BCLog::LIBEVENT) { + if (changed_log_categories & BCLog::LIBEVENT) { + if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) { + g_logger->DisableCategory(BCLog::LIBEVENT); + if (changed_log_categories == BCLog::LIBEVENT) { throw JSONRPCError(RPC_INVALID_PARAMETER, "libevent logging cannot be updated when using libevent before v2.1.1."); } } |