Skip to content

Commit 2a8712d

Browse files
klementtanjonatack
authored andcommitted
Unit test coverage for -loglevel configuration option
Co-authored-by: "Jon Atack <[email protected]>"
1 parent eb7bee5 commit 2a8712d

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/test/logging_tests.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <init/common.h>
56
#include <logging.h>
67
#include <logging/timer.h>
78
#include <test/util/setup_common.h>
@@ -18,6 +19,12 @@
1819

1920
BOOST_FIXTURE_TEST_SUITE(logging_tests, BasicTestingSetup)
2021

22+
static void ResetLogger()
23+
{
24+
LogInstance().SetLogLevel(BCLog::DEFAULT_LOG_LEVEL);
25+
LogInstance().SetCategoryLogLevel({});
26+
}
27+
2128
struct LogSetup : public BasicTestingSetup {
2229
fs::path prev_log_path;
2330
fs::path tmp_log_path;
@@ -194,4 +201,59 @@ BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
194201
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
195202
}
196203

204+
BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
205+
{
206+
// Set global log level
207+
{
208+
ResetLogger();
209+
ArgsManager args;
210+
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
211+
const char* argv_test[] = {"bitcoind", "-loglevel=debug"};
212+
std::string err;
213+
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
214+
init::SetLoggingLevel(args);
215+
BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::Level::Debug);
216+
}
217+
218+
// Set category-specific log level
219+
{
220+
ResetLogger();
221+
ArgsManager args;
222+
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
223+
const char* argv_test[] = {"bitcoind", "-loglevel=net:debug"};
224+
std::string err;
225+
BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
226+
init::SetLoggingLevel(args);
227+
BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::DEFAULT_LOG_LEVEL);
228+
229+
const auto& category_levels{LogInstance().CategoryLevels()};
230+
const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
231+
BOOST_REQUIRE(net_it != category_levels.end());
232+
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Debug);
233+
}
234+
235+
// Set both global log level and category-specific log level
236+
{
237+
ResetLogger();
238+
ArgsManager args;
239+
args.AddArg("-loglevel", "...", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
240+
const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:debug", "-loglevel=http:info"};
241+
std::string err;
242+
BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));
243+
init::SetLoggingLevel(args);
244+
BOOST_CHECK_EQUAL(LogInstance().LogLevel(), BCLog::Level::Debug);
245+
246+
const auto& category_levels{LogInstance().CategoryLevels()};
247+
BOOST_CHECK_EQUAL(category_levels.size(), 2);
248+
249+
const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
250+
BOOST_CHECK(net_it != category_levels.end());
251+
BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Debug);
252+
253+
const auto http_it{category_levels.find(BCLog::LogFlags::HTTP)};
254+
BOOST_CHECK(http_it != category_levels.end());
255+
BOOST_CHECK_EQUAL(http_it->second, BCLog::Level::Info);
256+
}
257+
}
258+
197259
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)