Skip to content

Commit eb7bee5

Browse files
klementtanjonatack
authored andcommitted
Create -loglevel configuration option
- add a -loglevel=<level>|<category:level> config option to allow users to set a global -loglevel and category-specific log levels. LogPrintLevel messages with a higher severity level than -loglevel will not be printed in the debug log. - for now, this config option is debug-only during the migration to severity-based logging - update unit and functional tests Co-authored-by: "Jon Atack <[email protected]>"
1 parent 98a1f9c commit eb7bee5

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
915915

916916
// ********************************************************* Step 3: parameter-to-internal-flags
917917
init::SetLoggingCategories(args);
918+
init::SetLoggingLevel(args);
918919

919920
fCheckBlockIndex = args.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
920921
fCheckpointsEnabled = args.GetBoolArg("-checkpoints", DEFAULT_CHECKPOINTS_ENABLED);

src/init/common.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <logging.h>
1212
#include <node/interface_ui.h>
1313
#include <tinyformat.h>
14+
#include <util/string.h>
1415
#include <util/system.h>
1516
#include <util/time.h>
1617
#include <util/translation.h>
@@ -28,6 +29,7 @@ void AddLoggingArgs(ArgsManager& argsman)
2829
ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
2930
argsman.AddArg("-debugexclude=<category>", "Exclude debugging information for a category. Can be used in conjunction with -debug=1 to output debug logs for all categories except the specified category. This option can be specified multiple times to exclude multiple categories.", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
3031
argsman.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
32+
argsman.AddArg("-loglevel=<level>|<category>:<level>", strprintf("Set the global or per-category severity level for logging categories enabled with the -debug configuration option or the logging RPC: %s (default=%s); warning and error levels are always logged. If <category>:<level> is supplied, the setting will override the global one and may be specified multiple times to set multiple category-specific levels. <category> can be: %s.", LogInstance().LogLevelsString(), LogInstance().LogLevelToStr(BCLog::DEFAULT_LOG_LEVEL), LogInstance().LogCategoriesString()), ArgsManager::DISALLOW_NEGATION | ArgsManager::DISALLOW_ELISION | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
3133
argsman.AddArg("-logtimestamps", strprintf("Prepend debug output with timestamp (default: %u)", DEFAULT_LOGTIMESTAMPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
3234
#ifdef HAVE_THREAD_LOCAL
3335
argsman.AddArg("-logthreadnames", strprintf("Prepend debug output with name of the originating thread (only available on platforms supporting thread_local) (default: %u)", DEFAULT_LOGTHREADNAMES), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
@@ -55,6 +57,26 @@ void SetLoggingOptions(const ArgsManager& args)
5557
fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
5658
}
5759

60+
void SetLoggingLevel(const ArgsManager& args)
61+
{
62+
if (args.IsArgSet("-loglevel")) {
63+
for (const std::string& level_str : args.GetArgs("-loglevel")) {
64+
if (level_str.find_first_of(':', 3) == std::string::npos) {
65+
// user passed a global log level, i.e. -loglevel=<level>
66+
if (!LogInstance().SetLogLevel(level_str)) {
67+
InitWarning(strprintf(_("Unsupported global logging level -loglevel=%s. Valid values: %s."), level_str, LogInstance().LogLevelsString()));
68+
}
69+
} else {
70+
// user passed a category-specific log level, i.e. -loglevel=<category>:<level>
71+
const auto& toks = SplitString(level_str, ':');
72+
if (!(toks.size() == 2 && LogInstance().SetCategoryLogLevel(toks[0], toks[1]))) {
73+
InitWarning(strprintf(_("Unsupported category-specific logging level -loglevel=%s. Expected -loglevel=<category>:<loglevel>. Valid categories: %s. Valid loglevels: %s."), level_str, LogInstance().LogCategoriesString(), LogInstance().LogLevelsString()));
74+
}
75+
}
76+
}
77+
}
78+
}
79+
5880
void SetLoggingCategories(const ArgsManager& args)
5981
{
6082
if (args.IsArgSet("-debug")) {

src/init/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace init {
1414
void AddLoggingArgs(ArgsManager& args);
1515
void SetLoggingOptions(const ArgsManager& args);
1616
void SetLoggingCategories(const ArgsManager& args);
17+
void SetLoggingLevel(const ArgsManager& args);
1718
bool StartLogging(const ArgsManager& args);
1819
void LogPackageVersion();
1920
} // namespace init

src/logging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
199199
return false;
200200
}
201201

202-
std::string LogLevelToStr(BCLog::Level level)
202+
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) const
203203
{
204204
switch (level) {
205205
case BCLog::Level::Debug:

src/logging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ namespace BCLog {
189189
//! Returns a string with all user-selectable log levels.
190190
std::string LogLevelsString() const;
191191

192+
//! Returns the string representation of a log level.
193+
std::string LogLevelToStr(BCLog::Level level) const;
194+
192195
bool DefaultShrinkDebugFile() const;
193196
};
194197

src/test/util/setup_common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
108108
"-logsourcelocations",
109109
"-logtimemicros",
110110
"-logthreadnames",
111+
"-loglevel=debug",
111112
"-debug",
112113
"-debugexclude=libevent",
113114
"-debugexclude=leveldb",

0 commit comments

Comments
 (0)