From 160706aa387245ed96b1f13e5362fe1837e8fc4b Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 3 Apr 2024 10:52:36 +0200 Subject: 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). --- src/logging.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src') 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> 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> LOG_CATEGORIES_ {"txreconciliation", BCLog::TXRECONCILIATION}, {"scan", BCLog::SCAN}, {"txpackages", BCLog::TXPACKAGES}, - {"1", BCLog::ALL}, - {"all", BCLog::ALL}, }; static const std::unordered_map LOG_CATEGORIES_BY_FLAG{ @@ -210,11 +206,8 @@ static const std::unordered_map LOG_CATEGORIES_BY_ [](const auto& in) { std::unordered_map 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 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 GetLogLevel(std::string_view level_str) std::vector BCLog::Logger::LogCategoriesList() const { std::vector 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; } -- cgit v1.2.3