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).
This commit is contained in:
Ryan Ofsky 2024-04-03 10:52:36 +02:00 committed by Vasil Dimov
parent 357f195391
commit 160706aa38
No known key found for this signature in database
GPG key ID: 54DF06F64B55CBBF

View file

@ -168,8 +168,6 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
} }
static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{ static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
{"0", BCLog::NONE},
{"", BCLog::NONE},
{"net", BCLog::NET}, {"net", BCLog::NET},
{"tor", BCLog::TOR}, {"tor", BCLog::TOR},
{"mempool", BCLog::MEMPOOL}, {"mempool", BCLog::MEMPOOL},
@ -201,8 +199,6 @@ static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_
{"txreconciliation", BCLog::TXRECONCILIATION}, {"txreconciliation", BCLog::TXRECONCILIATION},
{"scan", BCLog::SCAN}, {"scan", BCLog::SCAN},
{"txpackages", BCLog::TXPACKAGES}, {"txpackages", BCLog::TXPACKAGES},
{"1", BCLog::ALL},
{"all", BCLog::ALL},
}; };
static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{ 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) { [](const auto& in) {
std::unordered_map<BCLog::LogFlags, std::string> out; std::unordered_map<BCLog::LogFlags, std::string> out;
for (const auto& [k, v] : in) { for (const auto& [k, v] : in) {
switch (v) { const bool inserted{out.emplace(v, k).second};
case BCLog::NONE: out.emplace(BCLog::NONE, ""); break; assert(inserted);
case BCLog::ALL: out.emplace(BCLog::ALL, "all"); break;
default: out.emplace(v, k);
}
} }
return out; return out;
}(LOG_CATEGORIES_BY_STR) }(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) bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)
{ {
if (str.empty()) { if (str.empty() || str == "1" || str == "all") {
flag = BCLog::ALL; flag = BCLog::ALL;
return true; return true;
} }
if (str == "0") {
flag = BCLog::NONE;
return true;
}
auto it = LOG_CATEGORIES_BY_STR.find(str); auto it = LOG_CATEGORIES_BY_STR.find(str);
if (it != LOG_CATEGORIES_BY_STR.end()) { if (it != LOG_CATEGORIES_BY_STR.end()) {
flag = it->second; flag = it->second;
@ -253,6 +250,9 @@ std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
std::string LogCategoryToStr(BCLog::LogFlags category) std::string LogCategoryToStr(BCLog::LogFlags category)
{ {
if (category == BCLog::ALL) {
return "all";
}
auto it = LOG_CATEGORIES_BY_FLAG.find(category); auto it = LOG_CATEGORIES_BY_FLAG.find(category);
assert(it != LOG_CATEGORIES_BY_FLAG.end()); assert(it != LOG_CATEGORIES_BY_FLAG.end());
return it->second; 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> BCLog::Logger::LogCategoriesList() const
{ {
std::vector<LogCategory> ret; std::vector<LogCategory> ret;
ret.reserve(LOG_CATEGORIES_BY_STR.size());
for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) { 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; return ret;
} }