|
5 | 5 |
|
6 | 6 | #include <fs.h>
|
7 | 7 | #include <logging.h>
|
8 |
| -#include <util/threadnames.h> |
9 | 8 | #include <util/string.h>
|
| 9 | +#include <util/threadnames.h> |
10 | 10 | #include <util/time.h>
|
11 | 11 |
|
12 | 12 | #include <algorithm>
|
13 | 13 | #include <array>
|
14 | 14 | #include <mutex>
|
| 15 | +#include <optional> |
15 | 16 |
|
16 | 17 | const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
|
| 18 | +constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info}; |
17 | 19 |
|
18 | 20 | BCLog::Logger& LogInstance()
|
19 | 21 | {
|
@@ -269,6 +271,23 @@ std::string LogCategoryToStr(BCLog::LogFlags category)
|
269 | 271 | assert(false);
|
270 | 272 | }
|
271 | 273 |
|
| 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 | + |
272 | 291 | std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
|
273 | 292 | {
|
274 | 293 | // Sort log categories by alphabetical order.
|
@@ -334,7 +353,7 @@ namespace BCLog {
|
334 | 353 | }
|
335 | 354 | } // namespace BCLog
|
336 | 355 |
|
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) |
338 | 357 | {
|
339 | 358 | StdLockGuard scoped_lock(m_cs);
|
340 | 359 | std::string str_prefixed = LogEscapeMessage(str);
|
@@ -443,3 +462,24 @@ void BCLog::Logger::ShrinkDebugFile()
|
443 | 462 | else if (file != nullptr)
|
444 | 463 | fclose(file);
|
445 | 464 | }
|
| 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 | +} |
0 commit comments