refactor: Return std::optional from GetLogCategory
What changed, and why it matters
This is a clean internal code refactor: a helper function that looks up a log category by name now returns an optional value instead of writing to an output parameter. No behavior changes, no security implications.
No security action needed. This is a routine refactor; review as normal code quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit changes GetLogCategory from an out-parameter style (bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)) to returning std::optional
Changed components
src/logging.cppsrc/logging.hsrc/test/logging_tests.cppInspect captured patch +21 / −21
diff --git a/src/logging.cpp b/src/logging.cpp
index bfd48c96..3a9d84f5 100644
--- a/src/logging.cpp
+++ b/src/logging.cpp
@@ -127,10 +127,11 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
bool BCLog::Logger::EnableCategory(std::string_view str)
{
- BCLog::LogFlags flag;
- if (!GetLogCategory(flag, str)) return false;
- EnableCategory(flag);
- return true;
+ if (const auto flag{GetLogCategory(str)}) {
+ EnableCategory(*flag);
+ return true;
+ }
+ return false;
}
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
@@ -140,10 +141,11 @@ void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
bool BCLog::Logger::DisableCategory(std::string_view str)
{
- BCLog::LogFlags flag;
- if (!GetLogCategory(flag, str)) return false;
- DisableCategory(flag);
- return true;
+ if (const auto flag{GetLogCategory(str)}) {
+ DisableCategory(*flag);
+ return true;
+ }
+ return false;
}
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
@@ -217,18 +219,16 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_
}(LOG_CATEGORIES_BY_STR)
};
-bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)
+std::optional<BCLog::LogFlags> GetLogCategory(std::string_view str)
{
if (str.empty() || str == "1" || str == "all") {
- flag = BCLog::ALL;
- return true;
+ return BCLog::ALL;
}
auto it = LOG_CATEGORIES_BY_STR.find(str);
if (it != LOG_CATEGORIES_BY_STR.end()) {
- flag = it->second;
- return true;
+ return it->second;
}
- return false;
+ return std::nullopt;
}
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
@@ -592,14 +592,14 @@ bool BCLog::Logger::SetLogLevel(std::string_view level_str)
bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
{
- BCLog::LogFlags flag;
- if (!GetLogCategory(flag, category_str)) return false;
+ const auto flag{GetLogCategory(category_str)};
+ if (!flag) return false;
const auto level = GetLogLevel(level_str);
if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
STDLOCK(m_cs);
- m_category_log_levels[flag] = level.value();
+ m_category_log_levels[*flag] = level.value();
return true;
}
diff --git a/src/logging.h b/src/logging.h
index 005c67fd..cccac1df 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -20,6 +20,7 @@
#include <functional>
#include <list>
#include <memory>
+#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
@@ -296,7 +297,7 @@ static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level leve
return LogInstance().WillLogCategoryLevel(category, level);
}
-/** Return true if str parses as a log category and set the flag */
-bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str);
+/// Return log flag if str parses as a log category.
+std::optional<BCLog::LogFlags> GetLogCategory(std::string_view str);
#endif // BITCOIN_LOGGING_H
diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp
index f232bb41..dfefd917 100644
--- a/src/test/logging_tests.cpp
+++ b/src/test/logging_tests.cpp
@@ -165,9 +165,8 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
std::vector<std::pair<BCLog::LogFlags, std::string>> expected_category_names;
const auto category_names = SplitString(concatenated_category_names, ',');
for (const auto& category_name : category_names) {
- BCLog::LogFlags category;
const auto trimmed_category_name = TrimString(category_name);
- BOOST_REQUIRE(GetLogCategory(category, trimmed_category_name));
+ const auto category{*Assert(GetLogCategory(trimmed_category_name))};
expected_category_names.emplace_back(category, trimmed_category_name);
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.