diff options
author | Jim Posen <jimpo@coinbase.com> | 2018-04-11 14:06:35 -0700 |
---|---|---|
committer | Jim Posen <jimpo@coinbase.com> | 2018-04-29 14:37:17 -0700 |
commit | 1eac317f25b905e97e311130ab19c3b0d257fc04 (patch) | |
tree | cf844c12f7b18d452492200dab948d6ba9d40028 /src/logging.cpp | |
parent | 3316a9ebb66171937efddb213daed64fe51c4082 (diff) |
util: Refactor GetLogCategory.
Changing parameter types from pointers to references and uint32_t to
BCLog::LogFlags simplies calling code.
Diffstat (limited to 'src/logging.cpp')
-rw-r--r-- | src/logging.cpp | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/src/logging.cpp b/src/logging.cpp index 7604c0fd94..dc1ed0afb6 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -5,7 +5,6 @@ #include <logging.h> #include <util.h> -#include <utilstrencodings.h> const char * const DEFAULT_DEBUGLOGFILE = "debug.log"; @@ -64,11 +63,27 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag) logCategories |= flag; } +bool BCLog::Logger::EnableCategory(const std::string& str) +{ + BCLog::LogFlags flag; + if (!GetLogCategory(flag, str)) return false; + EnableCategory(flag); + return true; +} + void BCLog::Logger::DisableCategory(BCLog::LogFlags flag) { logCategories &= ~flag; } +bool BCLog::Logger::DisableCategory(const std::string& str) +{ + BCLog::LogFlags flag; + if (!GetLogCategory(flag, str)) return false; + DisableCategory(flag); + return true; +} + bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const { return (logCategories.load(std::memory_order_relaxed) & category) != 0; @@ -81,7 +96,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const struct CLogCategoryDesc { - uint32_t flag; + BCLog::LogFlags flag; std::string category; }; @@ -114,19 +129,17 @@ const CLogCategoryDesc LogCategories[] = {BCLog::ALL, "all"}, }; -bool GetLogCategory(uint32_t *f, const std::string *str) +bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str) { - if (f && str) { - if (*str == "") { - *f = BCLog::ALL; + if (str == "") { + flag = BCLog::ALL; + return true; + } + for (const CLogCategoryDesc& category_desc : LogCategories) { + if (category_desc.category == str) { + flag = category_desc.flag; return true; } - for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) { - if (LogCategories[i].category == *str) { - *f = LogCategories[i].flag; - return true; - } - } } return false; } @@ -135,11 +148,11 @@ std::string ListLogCategories() { std::string ret; int outcount = 0; - for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) { + for (const CLogCategoryDesc& category_desc : LogCategories) { // Omit the special cases. - if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) { + if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) { if (outcount != 0) ret += ", "; - ret += LogCategories[i].category; + ret += category_desc.category; outcount++; } } @@ -149,12 +162,12 @@ std::string ListLogCategories() std::vector<CLogCategoryActive> ListActiveLogCategories() { std::vector<CLogCategoryActive> ret; - for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) { + for (const CLogCategoryDesc& category_desc : LogCategories) { // Omit the special cases. - if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) { + if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) { CLogCategoryActive catActive; - catActive.category = LogCategories[i].category; - catActive.active = LogAcceptCategory(LogCategories[i].flag); + catActive.category = category_desc.category; + catActive.active = LogAcceptCategory(category_desc.flag); ret.push_back(catActive); } } |