diff options
author | klementtan <klementtan@gmail.com> | 2022-08-18 12:51:37 +0200 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2022-08-20 11:30:50 +0200 |
commit | c2797cfc602c5cdd899a7c11b37bb5711cebff38 (patch) | |
tree | ee944b7efbebbf23f6b27283321e152c63f9552c | |
parent | f6c0cc03509255ffa4dfd6e2822fce840dd0b181 (diff) | |
download | bitcoin-c2797cfc602c5cdd899a7c11b37bb5711cebff38.tar.xz |
Add BCLog::Logger::SetLogLevel()/SetCategoryLogLevel() for string inputs
and remove unnecessary param constness in LogPrintStr()
Co-authored-by: jonatack <jon@atack.com>
-rw-r--r-- | src/logging.cpp | 44 | ||||
-rw-r--r-- | src/logging.h | 6 |
2 files changed, 46 insertions, 4 deletions
diff --git a/src/logging.cpp b/src/logging.cpp index 3a25e7118d..3816297820 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -5,15 +5,17 @@ #include <fs.h> #include <logging.h> -#include <util/threadnames.h> #include <util/string.h> +#include <util/threadnames.h> #include <util/time.h> #include <algorithm> #include <array> #include <mutex> +#include <optional> const char * const DEFAULT_DEBUGLOGFILE = "debug.log"; +constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info}; BCLog::Logger& LogInstance() { @@ -269,6 +271,23 @@ std::string LogCategoryToStr(BCLog::LogFlags category) assert(false); } +static std::optional<BCLog::Level> GetLogLevel(const std::string& level_str) +{ + if (level_str == "debug") { + return BCLog::Level::Debug; + } else if (level_str == "info") { + return BCLog::Level::Info; + } else if (level_str == "warning") { + return BCLog::Level::Warning; + } else if (level_str == "error") { + return BCLog::Level::Error; + } else if (level_str == "none") { + return BCLog::Level::None; + } else { + return std::nullopt; + } +} + std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const { // Sort log categories by alphabetical order. @@ -334,7 +353,7 @@ namespace BCLog { } } // namespace BCLog -void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags category, const BCLog::Level level) +void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level) { StdLockGuard scoped_lock(m_cs); std::string str_prefixed = LogEscapeMessage(str); @@ -443,3 +462,24 @@ void BCLog::Logger::ShrinkDebugFile() else if (file != nullptr) fclose(file); } + +bool BCLog::Logger::SetLogLevel(const std::string& level_str) +{ + const auto level = GetLogLevel(level_str); + if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false; + m_log_level = level.value(); + return true; +} + +bool BCLog::Logger::SetCategoryLogLevel(const std::string& category_str, const std::string& level_str) +{ + BCLog::LogFlags flag; + if (!GetLogCategory(flag, category_str)) return false; + + const auto level = GetLogLevel(level_str); + if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false; + + StdLockGuard scoped_lock(m_cs); + m_category_log_levels[flag] = level.value(); + return true; +} diff --git a/src/logging.h b/src/logging.h index fd806ce142..f49a2db326 100644 --- a/src/logging.h +++ b/src/logging.h @@ -7,8 +7,8 @@ #define BITCOIN_LOGGING_H #include <fs.h> -#include <tinyformat.h> #include <threadsafety.h> +#include <tinyformat.h> #include <util/string.h> #include <atomic> @@ -121,7 +121,7 @@ namespace BCLog { std::atomic<bool> m_reopen_file{false}; /** Send a string to the log output */ - void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags category, const BCLog::Level level); + void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level); /** Returns whether logs will be written to any output */ bool Enabled() const @@ -162,9 +162,11 @@ namespace BCLog { StdLockGuard scoped_lock(m_cs); m_category_log_levels = levels; } + bool SetCategoryLogLevel(const std::string& category_str, const std::string& level_str); Level LogLevel() const { return m_log_level.load(); } void SetLogLevel(Level level) { m_log_level = level; } + bool SetLogLevel(const std::string& level); uint32_t GetCategoryMask() const { return m_categories.load(); } |