Skip to content

Commit

Permalink
logging, refactor: make category special cases explicit
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ryanofsky authored and vasild committed Aug 4, 2024
1 parent 357f195 commit 160706a
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -201,31 +199,30 @@ 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{
// Swap keys and values from LOG_CATEGORIES_BY_STR.
[](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)
};

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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 160706a

Please sign in to comment.