In commit “logging: move special cases to GetLogCategory()” (9fb321ad2b0176955718317b796715250eac01f4)
Would be good if commit title had a “refactor:” prefix so it is clear this is not intended to change behavior.
Also, I think this commit should remove ALL and NONE cases from the map, so special cases are handled completely within the GetLogCategory() and LogCategoryToStr() functions, and there are no more special cases in the map and LogCategoriesList(). Would suggest:
0--- a/src/logging.cpp
1+++ b/src/logging.cpp
2@@ -142,7 +142,6 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
3 }
4
5 static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
6- {"", BCLog::NONE},
7 {"net", BCLog::NET},
8 {"tor", BCLog::TOR},
9 {"mempool", BCLog::MEMPOOL},
10@@ -174,7 +173,6 @@ static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
11 {"txreconciliation", BCLog::TXRECONCILIATION},
12 {"scan", BCLog::SCAN},
13 {"txpackages", BCLog::TXPACKAGES},
14- {"all", BCLog::ALL},
15 };
16
17 static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
18@@ -191,11 +189,10 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_
19
20 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
21 {
22- if (str.empty() || str == "1") {
23+ if (str.empty() || str == "1" || str == "all") {
24 flag = BCLog::ALL;
25 return true;
26- }
27- if (str == "0") {
28+ } else if (str == "0") {
29 flag = BCLog::NONE;
30 return true;
31 }
32@@ -226,6 +223,11 @@ std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
33
34 std::string LogCategoryToStr(BCLog::LogFlags category)
35 {
36+ if (category == BCLog::NONE) {
37+ return "";
38+ } else if (category == BCLog::ALL) {
39+ return "all";
40+ }
41 auto it = LOG_CATEGORIES_BY_FLAG.find(category);
42 assert(it != LOG_CATEGORIES_BY_FLAG.end());
43 return it->second;
44@@ -252,9 +254,7 @@ std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
45 {
46 std::vector<LogCategory> ret;
47 for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) {
48- if (flag != BCLog::NONE && flag != BCLog::ALL) {
49- ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
50- }
51+ ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
52 }
53 return ret;
54 }
And would suggest a commit message like:
0logging, refactor: make category special cases explicit
1
2Make special cases explicit in GetLogCategory() and LogCategoryToStr()
3functions. Simplify the LOG_CATEGORIES_BY_STR and LOG_CATEGORIES_BY_FLAG
4mappings and LogCategoriesList() function.