Skip to content

Commit 160706a

Browse files
ryanofskyvasild
authored andcommitted
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).
1 parent 357f195 commit 160706a

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/logging.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
168168
}
169169

170170
static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
171-
{"0", BCLog::NONE},
172-
{"", BCLog::NONE},
173171
{"net", BCLog::NET},
174172
{"tor", BCLog::TOR},
175173
{"mempool", BCLog::MEMPOOL},
@@ -201,31 +199,30 @@ static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_
201199
{"txreconciliation", BCLog::TXRECONCILIATION},
202200
{"scan", BCLog::SCAN},
203201
{"txpackages", BCLog::TXPACKAGES},
204-
{"1", BCLog::ALL},
205-
{"all", BCLog::ALL},
206202
};
207203

208204
static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
209205
// Swap keys and values from LOG_CATEGORIES_BY_STR.
210206
[](const auto& in) {
211207
std::unordered_map<BCLog::LogFlags, std::string> out;
212208
for (const auto& [k, v] : in) {
213-
switch (v) {
214-
case BCLog::NONE: out.emplace(BCLog::NONE, ""); break;
215-
case BCLog::ALL: out.emplace(BCLog::ALL, "all"); break;
216-
default: out.emplace(v, k);
217-
}
209+
const bool inserted{out.emplace(v, k).second};
210+
assert(inserted);
218211
}
219212
return out;
220213
}(LOG_CATEGORIES_BY_STR)
221214
};
222215

223216
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)
224217
{
225-
if (str.empty()) {
218+
if (str.empty() || str == "1" || str == "all") {
226219
flag = BCLog::ALL;
227220
return true;
228221
}
222+
if (str == "0") {
223+
flag = BCLog::NONE;
224+
return true;
225+
}
229226
auto it = LOG_CATEGORIES_BY_STR.find(str);
230227
if (it != LOG_CATEGORIES_BY_STR.end()) {
231228
flag = it->second;
@@ -253,6 +250,9 @@ std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
253250

254251
std::string LogCategoryToStr(BCLog::LogFlags category)
255252
{
253+
if (category == BCLog::ALL) {
254+
return "all";
255+
}
256256
auto it = LOG_CATEGORIES_BY_FLAG.find(category);
257257
assert(it != LOG_CATEGORIES_BY_FLAG.end());
258258
return it->second;
@@ -278,10 +278,9 @@ static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str)
278278
std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
279279
{
280280
std::vector<LogCategory> ret;
281+
ret.reserve(LOG_CATEGORIES_BY_STR.size());
281282
for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) {
282-
if (flag != BCLog::NONE && flag != BCLog::ALL) {
283-
ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
284-
}
283+
ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
285284
}
286285
return ret;
287286
}

0 commit comments

Comments
 (0)