Skip to content

Commit 8fe3457

Browse files
klementtanjonatack
authored andcommitted
Update LogAcceptCategory() and unit tests with log severity levels
Co-authored-by: "Jon Atack <[email protected]>"
1 parent c2797cf commit 8fe3457

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

src/logging.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
124124
return (m_categories.load(std::memory_order_relaxed) & category) != 0;
125125
}
126126

127+
bool BCLog::Logger::WillLogCategoryLevel(BCLog::LogFlags category, BCLog::Level level) const
128+
{
129+
// Log messages at Warning and Error level unconditionally, so that
130+
// important troubleshooting information doesn't get lost.
131+
if (level >= BCLog::Level::Warning) return true;
132+
133+
if (!WillLogCategory(category)) return false;
134+
135+
StdLockGuard scoped_lock(m_cs);
136+
const auto it{m_category_log_levels.find(category)};
137+
return level >= (it == m_category_log_levels.end() ? LogLevel() : it->second);
138+
}
139+
127140
bool BCLog::Logger::DefaultShrinkDebugFile() const
128141
{
129142
return m_categories == BCLog::NONE;

src/logging.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ namespace BCLog {
176176
bool DisableCategory(const std::string& str);
177177

178178
bool WillLogCategory(LogFlags category) const;
179+
bool WillLogCategoryLevel(LogFlags category, Level level) const;
180+
179181
/** Returns a vector of the log categories in alphabetical order. */
180182
std::vector<LogCategory> LogCategoriesList() const;
181183
/** Returns a string with the log categories in alphabetical order. */
@@ -194,12 +196,7 @@ BCLog::Logger& LogInstance();
194196
/** Return true if log accepts specified category, at the specified level. */
195197
static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
196198
{
197-
// Log messages at Warning and Error level unconditionally, so that
198-
// important troubleshooting information doesn't get lost.
199-
if (level >= BCLog::Level::Warning) {
200-
return true;
201-
}
202-
return LogInstance().WillLogCategory(category);
199+
return LogInstance().WillLogCategoryLevel(category, level);
203200
}
204201

205202
/** Return true if str parses as a log category and set the flag */

src/test/i2p_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <i2p.h>
6+
#include <logging.h>
67
#include <netaddress.h>
78
#include <test/util/logging.h>
89
#include <test/util/net.h>
@@ -19,6 +20,8 @@ BOOST_FIXTURE_TEST_SUITE(i2p_tests, BasicTestingSetup)
1920

2021
BOOST_AUTO_TEST_CASE(unlimited_recv)
2122
{
23+
const auto prev_log_level{LogInstance().LogLevel()};
24+
LogInstance().SetLogLevel(BCLog::Level::Debug);
2225
auto CreateSockOrig = CreateSock;
2326

2427
// Mock CreateSock() to create MockSock.
@@ -39,6 +42,7 @@ BOOST_AUTO_TEST_CASE(unlimited_recv)
3942
}
4043

4144
CreateSock = CreateSockOrig;
45+
LogInstance().SetLogLevel(prev_log_level);
4246
}
4347

4448
BOOST_AUTO_TEST_SUITE_END()

src/test/logging_tests.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <chrono>
1111
#include <fstream>
1212
#include <iostream>
13+
#include <unordered_map>
1314
#include <utility>
1415
#include <vector>
1516

@@ -25,21 +26,30 @@ struct LogSetup : public BasicTestingSetup {
2526
bool prev_log_timestamps;
2627
bool prev_log_threadnames;
2728
bool prev_log_sourcelocations;
29+
std::unordered_map<BCLog::LogFlags, BCLog::Level> prev_category_levels;
30+
BCLog::Level prev_log_level;
2831

2932
LogSetup() : prev_log_path{LogInstance().m_file_path},
3033
tmp_log_path{m_args.GetDataDirBase() / "tmp_debug.log"},
3134
prev_reopen_file{LogInstance().m_reopen_file},
3235
prev_print_to_file{LogInstance().m_print_to_file},
3336
prev_log_timestamps{LogInstance().m_log_timestamps},
3437
prev_log_threadnames{LogInstance().m_log_threadnames},
35-
prev_log_sourcelocations{LogInstance().m_log_sourcelocations}
38+
prev_log_sourcelocations{LogInstance().m_log_sourcelocations},
39+
prev_category_levels{LogInstance().CategoryLevels()},
40+
prev_log_level{LogInstance().LogLevel()}
3641
{
3742
LogInstance().m_file_path = tmp_log_path;
3843
LogInstance().m_reopen_file = true;
3944
LogInstance().m_print_to_file = true;
4045
LogInstance().m_log_timestamps = false;
4146
LogInstance().m_log_threadnames = false;
42-
LogInstance().m_log_sourcelocations = true;
47+
48+
// Prevent tests from failing when the line number of the logs changes.
49+
LogInstance().m_log_sourcelocations = false;
50+
51+
LogInstance().SetLogLevel(BCLog::Level::Debug);
52+
LogInstance().SetCategoryLogLevel({});
4353
}
4454

4555
~LogSetup()
@@ -51,6 +61,8 @@ struct LogSetup : public BasicTestingSetup {
5161
LogInstance().m_log_timestamps = prev_log_timestamps;
5262
LogInstance().m_log_threadnames = prev_log_threadnames;
5363
LogInstance().m_log_sourcelocations = prev_log_sourcelocations;
64+
LogInstance().SetLogLevel(prev_log_level);
65+
LogInstance().SetCategoryLogLevel(prev_category_levels);
5466
}
5567
};
5668

@@ -74,6 +86,7 @@ BOOST_AUTO_TEST_CASE(logging_timer)
7486

7587
BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
7688
{
89+
LogInstance().m_log_sourcelocations = true;
7790
LogPrintf_("fn1", "src1", 1, BCLog::LogFlags::NET, BCLog::Level::Debug, "foo1: %s", "bar1\n");
7891
LogPrintf_("fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::None, "foo2: %s", "bar2\n");
7992
LogPrintf_("fn3", "src3", 3, BCLog::LogFlags::NONE, BCLog::Level::Debug, "foo3: %s", "bar3\n");
@@ -94,9 +107,6 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
94107

95108
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
96109
{
97-
// Prevent tests from failing when the line number of the following log calls changes.
98-
LogInstance().m_log_sourcelocations = false;
99-
100110
LogPrintf("foo5: %s\n", "bar5");
101111
LogPrint(BCLog::NET, "foo6: %s\n", "bar6");
102112
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7");
@@ -123,16 +133,14 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
123133

124134
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
125135
{
126-
// Prevent tests from failing when the line number of the following log calls changes.
127-
LogInstance().m_log_sourcelocations = false;
128136
LogInstance().EnableCategory(BCLog::LogFlags::ALL);
129-
const auto concated_categery_names = LogInstance().LogCategoriesString();
137+
const auto concatenated_category_names = LogInstance().LogCategoriesString();
130138
std::vector<std::pair<BCLog::LogFlags, std::string>> expected_category_names;
131-
const auto category_names = SplitString(concated_categery_names, ',');
139+
const auto category_names = SplitString(concatenated_category_names, ',');
132140
for (const auto& category_name : category_names) {
133-
BCLog::LogFlags category = BCLog::NONE;
141+
BCLog::LogFlags category;
134142
const auto trimmed_category_name = TrimString(category_name);
135-
BOOST_TEST(GetLogCategory(category, trimmed_category_name));
143+
BOOST_REQUIRE(GetLogCategory(category, trimmed_category_name));
136144
expected_category_names.emplace_back(category, trimmed_category_name);
137145
}
138146

test/functional/test_framework/test_node.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def __init__(self, i, datadir, *, chain, rpchost, timewait, timeout_factor, bitc
118118
self.args.append("-logthreadnames")
119119
if self.version_is_at_least(219900):
120120
self.args.append("-logsourcelocations")
121+
if self.version_is_at_least(239000):
122+
self.args.append("-loglevel=debug")
121123

122124
self.cli = TestNodeCLI(bitcoin_cli, self.datadir)
123125
self.use_cli = use_cli

0 commit comments

Comments
 (0)