diff options
author | Ryan Ofsky <ryan@ofsky.org> | 2024-04-03 10:52:36 +0200 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2024-08-04 06:42:59 +0200 |
commit | 160706aa387245ed96b1f13e5362fe1837e8fc4b (patch) | |
tree | d57b392f84f37555b9ea84d8d7adf6f5bcc26127 /src | |
parent | 357f19539129fae31bbc87275fa0935fc33cade4 (diff) |
logging, refactor: make category special cases explicit
Make special cases explicit in GetLogCategory() and LogCategoryToStr()
functions. Simplify the LOG_CATEGORIES_BY_STR and LOG_CATEGORIES_BY_FLAG
mappings and LogCategoriesList() function.
This makes the maps `LOG_CATEGORIES_BY_STR` and `LOG_CATEGORIES_BY_FLAG`
consistent (one is exactly the opposite of the other).
Diffstat (limited to 'src')
-rw-r--r-- | src/logging.cpp | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/logging.cpp b/src/logging.cpp index 9c87cfd2b7..6aeab4dfb4 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -168,8 +168,6 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const } static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{ - {"0", BCLog::NONE}, - {"", BCLog::NONE}, {"net", BCLog::NET}, {"tor", BCLog::TOR}, {"mempool", BCLog::MEMPOOL}, @@ -201,8 +199,6 @@ static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_ {"txreconciliation", BCLog::TXRECONCILIATION}, {"scan", BCLog::SCAN}, {"txpackages", BCLog::TXPACKAGES}, - {"1", BCLog::ALL}, - {"all", BCLog::ALL}, }; static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{ @@ -210,11 +206,8 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_ [](const auto& in) { std::unordered_map<BCLog::LogFlags, std::string> out; for (const auto& [k, v] : in) { - switch (v) { - case BCLog::NONE: out.emplace(BCLog::NONE, ""); break; - case BCLog::ALL: out.emplace(BCLog::ALL, "all"); break; - default: out.emplace(v, k); - } + const bool inserted{out.emplace(v, k).second}; + assert(inserted); } return out; }(LOG_CATEGORIES_BY_STR) @@ -222,10 +215,14 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_ bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str) { - if (str.empty()) { + if (str.empty() || str == "1" || str == "all") { flag = BCLog::ALL; return true; } + if (str == "0") { + flag = BCLog::NONE; + return true; + } auto it = LOG_CATEGORIES_BY_STR.find(str); if (it != LOG_CATEGORIES_BY_STR.end()) { flag = it->second; @@ -253,6 +250,9 @@ std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) std::string LogCategoryToStr(BCLog::LogFlags category) { + if (category == BCLog::ALL) { + return "all"; + } auto it = LOG_CATEGORIES_BY_FLAG.find(category); assert(it != LOG_CATEGORIES_BY_FLAG.end()); return it->second; @@ -278,10 +278,9 @@ static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str) std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const { std::vector<LogCategory> ret; + ret.reserve(LOG_CATEGORIES_BY_STR.size()); for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) { - if (flag != BCLog::NONE && flag != BCLog::ALL) { - ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)}); - } + ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)}); } return ret; } |