Skip to content

Commit fbd7642

Browse files
committed
logging: add -loglevelalways=1 option
This option tells the logging system to always include a "[cat:level]" prefix, so [net] becomes [net:debug], LogInfo/LogPrint statements will have an [all:info] prefix, and LogWarning and LogError logs will become [all:warning] and [all:error]. This may be easier for automated parsing of logs, particularly if additional prefixes such as thread or source location are enabled.
1 parent 782bb6a commit fbd7642

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

src/init/common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void AddLoggingArgs(ArgsManager& argsman)
4040
#endif
4141
argsman.AddArg("-logsourcelocations", strprintf("Prepend debug output with name of the originating source location (source file, line number and function name) (default: %u)", DEFAULT_LOGSOURCELOCATIONS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4242
argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
43+
argsman.AddArg("-loglevelalways", strprintf("Always prepend a category and level (default: %u)", DEFAULT_LOGLEVELALWAYS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4344
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -daemon. To disable logging to file, set -nodebuglogfile)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4445
argsman.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4546
}
@@ -55,6 +56,7 @@ void SetLoggingOptions(const ArgsManager& args)
5556
LogInstance().m_log_threadnames = args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
5657
#endif
5758
LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
59+
LogInstance().m_always_print_category_level = args.GetBoolArg("-loglevelalways", DEFAULT_LOGLEVELALWAYS);
5860

5961
fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
6062
}

src/logging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level l
392392
{
393393
if (category == LogFlags::NONE) category = LogFlags::ALL;
394394

395-
const bool has_category{category != LogFlags::ALL};
395+
const bool has_category{m_always_print_category_level || category != LogFlags::ALL};
396396

397397
// If there is no category, Info is implied
398398
if (!has_category && level == Level::Info) return {};
@@ -402,7 +402,7 @@ std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level l
402402
s += LogCategoryToStr(category);
403403
}
404404

405-
if (!has_category || level != Level::Debug) {
405+
if (m_always_print_category_level || !has_category || level != Level::Debug) {
406406
// If there is a category, Debug is implied, so don't add the level
407407

408408
// Only add separator if we have a category

src/logging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const bool DEFAULT_LOGIPS = false;
2525
static const bool DEFAULT_LOGTIMESTAMPS = true;
2626
static const bool DEFAULT_LOGTHREADNAMES = false;
2727
static const bool DEFAULT_LOGSOURCELOCATIONS = false;
28+
static constexpr bool DEFAULT_LOGLEVELALWAYS = false;
2829
extern const char * const DEFAULT_DEBUGLOGFILE;
2930

3031
extern bool fLogIPs;
@@ -119,6 +120,7 @@ namespace BCLog {
119120
bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
120121
bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
121122
bool m_log_sourcelocations = DEFAULT_LOGSOURCELOCATIONS;
123+
bool m_always_print_category_level = DEFAULT_LOGLEVELALWAYS;
122124

123125
fs::path m_file_path;
124126
std::atomic<bool> m_reopen_file{false};

0 commit comments

Comments
 (0)