Skip to content

Commit a9c2958

Browse files
committed
init: raise on invalid loglevel config option
1 parent b0c3995 commit a9c2958

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
952952
// ********************************************************* Step 3: parameter-to-internal-flags
953953
auto result = init::SetLoggingCategories(args);
954954
if (!result) return InitError(util::ErrorString(result));
955-
init::SetLoggingLevel(args);
955+
result = init::SetLoggingLevel(args);
956+
if (!result) return InitError(util::ErrorString(result));
956957

957958
nConnectTimeout = args.GetIntArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
958959
if (nConnectTimeout <= 0) {

src/init/common.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,25 @@ void SetLoggingOptions(const ArgsManager& args)
5959
fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
6060
}
6161

62-
void SetLoggingLevel(const ArgsManager& args)
62+
util::Result<void> SetLoggingLevel(const ArgsManager& args)
6363
{
6464
if (args.IsArgSet("-loglevel")) {
6565
for (const std::string& level_str : args.GetArgs("-loglevel")) {
6666
if (level_str.find_first_of(':', 3) == std::string::npos) {
6767
// user passed a global log level, i.e. -loglevel=<level>
6868
if (!LogInstance().SetLogLevel(level_str)) {
69-
InitWarning(strprintf(_("Unsupported global logging level -loglevel=%s. Valid values: %s."), level_str, LogInstance().LogLevelsString()));
69+
return util::Error{strprintf(_("Unsupported global logging level -loglevel=%s. Valid values: %s."), level_str, LogInstance().LogLevelsString())};
7070
}
7171
} else {
7272
// user passed a category-specific log level, i.e. -loglevel=<category>:<level>
7373
const auto& toks = SplitString(level_str, ':');
7474
if (!(toks.size() == 2 && LogInstance().SetCategoryLogLevel(toks[0], toks[1]))) {
75-
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()));
75+
return util::Error{strprintf(_("Unsupported category-specific logging level -loglevel=%s. Expected -loglevel=<category>:<loglevel>. Valid categories: %s. Valid loglevels: %s."), level_str, LogInstance().LogCategoriesString(), LogInstance().LogLevelsString())};
7676
}
7777
}
7878
}
7979
}
80+
return {};
8081
}
8182

8283
util::Result<void> SetLoggingCategories(const ArgsManager& args)

src/init/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace init {
1616
void AddLoggingArgs(ArgsManager& args);
1717
void SetLoggingOptions(const ArgsManager& args);
1818
[[nodiscard]] util::Result<void> SetLoggingCategories(const ArgsManager& args);
19-
void SetLoggingLevel(const ArgsManager& args);
19+
[[nodiscard]] util::Result<void> SetLoggingLevel(const ArgsManager& args);
2020
bool StartLogging(const ArgsManager& args);
2121
void LogPackageVersion();
2222
} // namespace init

src/test/logging_tests.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
200200
const char* argv_test[] = {"bitcoind", "-loglevel=debug"};
201201
std::string err;
202202
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
203-
init::SetLoggingLevel(args);
203+
204+
auto result = init::SetLoggingLevel(args);
205+
BOOST_REQUIRE(result);
204206
BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::Level::Debug);
205207
}
206208

@@ -212,7 +214,9 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
212214
const char* argv_test[] = {"bitcoind", "-loglevel=net:trace"};
213215
std::string err;
214216
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
215-
init::SetLoggingLevel(args);
217+
218+
auto result = init::SetLoggingLevel(args);
219+
BOOST_REQUIRE(result);
216220
BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::DEFAULT_LOG_LEVEL);
217221

218222
const auto& category_levels{LogInstance().CategoryLevels()};
@@ -229,7 +233,9 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
229233
const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
230234
std::string err;
231235
BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));
232-
init::SetLoggingLevel(args);
236+
237+
auto result = init::SetLoggingLevel(args);
238+
BOOST_REQUIRE(result);
233239
BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::Level::Debug);
234240

235241
const auto& category_levels{LogInstance().CategoryLevels()};

0 commit comments

Comments
 (0)