Skip to content

Commit c2797cf

Browse files
klementtanjonatack
andcommitted
Add BCLog::Logger::SetLogLevel()/SetCategoryLogLevel() for string inputs
and remove unnecessary param constness in LogPrintStr() Co-authored-by: jonatack <[email protected]>
1 parent f6c0cc0 commit c2797cf

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/logging.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
#include <fs.h>
77
#include <logging.h>
8-
#include <util/threadnames.h>
98
#include <util/string.h>
9+
#include <util/threadnames.h>
1010
#include <util/time.h>
1111

1212
#include <algorithm>
1313
#include <array>
1414
#include <mutex>
15+
#include <optional>
1516

1617
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
18+
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info};
1719

1820
BCLog::Logger& LogInstance()
1921
{
@@ -269,6 +271,23 @@ std::string LogCategoryToStr(BCLog::LogFlags category)
269271
assert(false);
270272
}
271273

274+
static std::optional<BCLog::Level> GetLogLevel(const std::string& level_str)
275+
{
276+
if (level_str == "debug") {
277+
return BCLog::Level::Debug;
278+
} else if (level_str == "info") {
279+
return BCLog::Level::Info;
280+
} else if (level_str == "warning") {
281+
return BCLog::Level::Warning;
282+
} else if (level_str == "error") {
283+
return BCLog::Level::Error;
284+
} else if (level_str == "none") {
285+
return BCLog::Level::None;
286+
} else {
287+
return std::nullopt;
288+
}
289+
}
290+
272291
std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
273292
{
274293
// Sort log categories by alphabetical order.
@@ -334,7 +353,7 @@ namespace BCLog {
334353
}
335354
} // namespace BCLog
336355

337-
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags category, const BCLog::Level level)
356+
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
338357
{
339358
StdLockGuard scoped_lock(m_cs);
340359
std::string str_prefixed = LogEscapeMessage(str);
@@ -443,3 +462,24 @@ void BCLog::Logger::ShrinkDebugFile()
443462
else if (file != nullptr)
444463
fclose(file);
445464
}
465+
466+
bool BCLog::Logger::SetLogLevel(const std::string& level_str)
467+
{
468+
const auto level = GetLogLevel(level_str);
469+
if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
470+
m_log_level = level.value();
471+
return true;
472+
}
473+
474+
bool BCLog::Logger::SetCategoryLogLevel(const std::string& category_str, const std::string& level_str)
475+
{
476+
BCLog::LogFlags flag;
477+
if (!GetLogCategory(flag, category_str)) return false;
478+
479+
const auto level = GetLogLevel(level_str);
480+
if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
481+
482+
StdLockGuard scoped_lock(m_cs);
483+
m_category_log_levels[flag] = level.value();
484+
return true;
485+
}

src/logging.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#define BITCOIN_LOGGING_H
88

99
#include <fs.h>
10-
#include <tinyformat.h>
1110
#include <threadsafety.h>
11+
#include <tinyformat.h>
1212
#include <util/string.h>
1313

1414
#include <atomic>
@@ -121,7 +121,7 @@ namespace BCLog {
121121
std::atomic<bool> m_reopen_file{false};
122122

123123
/** Send a string to the log output */
124-
void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags category, const BCLog::Level level);
124+
void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level);
125125

126126
/** Returns whether logs will be written to any output */
127127
bool Enabled() const
@@ -162,9 +162,11 @@ namespace BCLog {
162162
StdLockGuard scoped_lock(m_cs);
163163
m_category_log_levels = levels;
164164
}
165+
bool SetCategoryLogLevel(const std::string& category_str, const std::string& level_str);
165166

166167
Level LogLevel() const { return m_log_level.load(); }
167168
void SetLogLevel(Level level) { m_log_level = level; }
169+
bool SetLogLevel(const std::string& level);
168170

169171
uint32_t GetCategoryMask() const { return m_categories.load(); }
170172

0 commit comments

Comments
 (0)