Skip to content

Commit 2210feb

Browse files
committed
Merge bitcoin#34051: log: Remove brittle and confusing LogPrintLevel
fa8a5d2 log: Remove brittle and confusing LogPrintLevel (MarcoFalke) fac24bb test: Clarify logging_SeverityLevels test (MarcoFalke) f273167 ipc: separate log statements per level (stickies-v) 94c51ae libevent: separate log statements per level (stickies-v) Pull request description: `LogPrintLevel` has many issues: * It encourages to log several levels in one source location. This is problematic, because all levels (even warnings and errors) will be rate limited equally for the same location. * Its warning and error logs are specially formatted compared to all other warning and error logs in the codebase, making them harder to spot (both in the debug log and in the code). * It is verbose to type and read. * It is confusing, because the majority of code uses the `Log$LEVEL(...)` macros. Having less ways to achieve the same makes the code more consistent and easier to review. Fix all issues by removing it ACKs for top commit: stickies-v: re-ACK fa8a5d2 ajtowns: ACK fa8a5d2 pablomartin4btc: re-ACK fa8a5d2 Tree-SHA512: 9fbb04962d9c26e566338694a7725b3c0e88ef733322d890bcc6aeddb45266c754e7c885c69bbfebd1588cc09912c6784cfc00e69882f1271a8c87d201490478
2 parents 58251bf + fa8a5d2 commit 2210feb

File tree

8 files changed

+58
-74
lines changed

8 files changed

+58
-74
lines changed

src/httpserver.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,22 +422,20 @@ static void HTTPWorkQueueRun(WorkQueue<HTTPClosure>* queue, int worker_num)
422422
/** libevent event log callback */
423423
static void libevent_log_cb(int severity, const char *msg)
424424
{
425-
BCLog::Level level;
426425
switch (severity) {
427426
case EVENT_LOG_DEBUG:
428-
level = BCLog::Level::Debug;
427+
LogDebug(BCLog::LIBEVENT, "%s", msg);
429428
break;
430429
case EVENT_LOG_MSG:
431-
level = BCLog::Level::Info;
430+
LogInfo("libevent: %s", msg);
432431
break;
433432
case EVENT_LOG_WARN:
434-
level = BCLog::Level::Warning;
433+
LogWarning("libevent: %s", msg);
435434
break;
436435
default: // EVENT_LOG_ERR and others are mapped to error
437-
level = BCLog::Level::Error;
436+
LogError("libevent: %s", msg);
438437
break;
439438
}
440-
LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg);
441439
}
442440

443441
bool InitHTTPServer(const util::SignalInterrupt& interrupt)

src/i2p.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void Session::CreateIfNotCreatedAlready()
415415
const auto session_type = m_transient ? "transient" : "persistent";
416416
const auto session_id = GetRandHash().GetHex().substr(0, 10); // full is overkill, too verbose in the logs
417417

418-
LogDebug(BCLog::I2P, "Creating %s SAM session %s with %s\n", session_type, session_id, m_control_host.ToString());
418+
LogDebug(BCLog::I2P, "Creating %s I2P SAM session %s with %s\n", session_type, session_id, m_control_host.ToString());
419419

420420
auto sock = Hello();
421421

@@ -452,7 +452,7 @@ void Session::CreateIfNotCreatedAlready()
452452
m_session_id = session_id;
453453
m_control_sock = std::move(sock);
454454

455-
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "%s SAM session %s created, my address=%s\n",
455+
LogInfo("%s I2P SAM session %s created, my address=%s",
456456
Capitalize(session_type),
457457
m_session_id,
458458
m_my_addr.ToStringAddrPort());
@@ -483,9 +483,9 @@ void Session::Disconnect()
483483
{
484484
if (m_control_sock) {
485485
if (m_session_id.empty()) {
486-
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "Destroying incomplete SAM session\n");
486+
LogInfo("Destroying incomplete I2P SAM session");
487487
} else {
488-
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "Destroying SAM session %s\n", m_session_id);
488+
LogInfo("Destroying I2P SAM session %s", m_session_id);
489489
}
490490
m_control_sock.reset();
491491
}

src/ipc/capnp/protocol.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ namespace ipc {
3131
namespace capnp {
3232
namespace {
3333

34-
BCLog::Level ConvertIPCLogLevel(mp::Log level)
35-
{
36-
switch (level) {
37-
case mp::Log::Trace: return BCLog::Level::Trace;
38-
case mp::Log::Debug: return BCLog::Level::Debug;
39-
case mp::Log::Info: return BCLog::Level::Info;
40-
case mp::Log::Warning: return BCLog::Level::Warning;
41-
case mp::Log::Error: return BCLog::Level::Error;
42-
case mp::Log::Raise: return BCLog::Level::Error;
43-
} // no default case, so the compiler can warn about missing cases
44-
45-
// Be conservative and assume that if MP ever adds a new log level, it
46-
// should only be shown at our most verbose level.
47-
return BCLog::Level::Trace;
48-
}
49-
5034
mp::Log GetRequestedIPCLogLevel()
5135
{
5236
if (LogAcceptCategory(BCLog::IPC, BCLog::Level::Trace)) return mp::Log::Trace;
@@ -58,8 +42,30 @@ mp::Log GetRequestedIPCLogLevel()
5842

5943
void IpcLogFn(mp::LogMessage message)
6044
{
61-
LogPrintLevel(BCLog::IPC, ConvertIPCLogLevel(message.level), "%s\n", message.message);
62-
if (message.level == mp::Log::Raise) throw Exception(message.message);
45+
switch (message.level) {
46+
case mp::Log::Trace:
47+
LogTrace(BCLog::IPC, "%s", message.message);
48+
return;
49+
case mp::Log::Debug:
50+
LogDebug(BCLog::IPC, "%s", message.message);
51+
return;
52+
case mp::Log::Info:
53+
LogInfo("ipc: %s", message.message);
54+
return;
55+
case mp::Log::Warning:
56+
LogWarning("ipc: %s", message.message);
57+
return;
58+
case mp::Log::Error:
59+
LogError("ipc: %s", message.message);
60+
return;
61+
case mp::Log::Raise:
62+
LogError("ipc: %s", message.message);
63+
throw Exception(message.message);
64+
} // no default case, so the compiler can warn about missing cases
65+
66+
// Be conservative and assume that if MP ever adds a new log level, it
67+
// should only be shown at our most verbose level.
68+
LogTrace(BCLog::IPC, "%s", message.message);
6369
}
6470

6571
class CapnpProtocol : public Protocol

src/logging.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <crypto/siphash.h>
1010
#include <threadsafety.h>
1111
#include <tinyformat.h>
12+
#include <util/check.h>
1213
#include <util/fs.h>
1314
#include <util/string.h>
1415
#include <util/time.h>
@@ -372,22 +373,20 @@ inline void LogPrintFormatInternal(std::source_location&& source_loc, BCLog::Log
372373
// Use a macro instead of a function for conditional logging to prevent
373374
// evaluating arguments when logging for the category is not enabled.
374375

375-
// Log by prefixing the output with the passed category name and severity level. This can either
376-
// log conditionally if the category is allowed or unconditionally if level >= BCLog::Level::Info
377-
// is passed. If this function logs unconditionally, logging to disk is rate-limited. This is
378-
// important so that callers don't need to worry about accidentally introducing a disk-fill
379-
// vulnerability if level >= Info is used. Additionally, users specifying -debug are assumed to be
376+
// Log by prefixing the output with the passed category name and severity level. This logs conditionally if
377+
// the category is allowed. No rate limiting is applied, because users specifying -debug are assumed to be
380378
// developers or power users who are aware that -debug may cause excessive disk usage due to logging.
381-
#define LogPrintLevel(category, level, ...) \
379+
#define detail_LogIfCategoryAndLevelEnabled(category, level, ...) \
382380
do { \
383381
if (LogAcceptCategory((category), (level))) { \
384382
bool rate_limit{level >= BCLog::Level::Info}; \
383+
Assume(!rate_limit);/*Only called with the levels below*/ \
385384
LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \
386385
} \
387386
} while (0)
388387

389388
// Log conditionally, prefixing the output with the passed category name.
390-
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
391-
#define LogTrace(category, ...) LogPrintLevel(category, BCLog::Level::Trace, __VA_ARGS__)
389+
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__)
390+
#define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Trace, __VA_ARGS__)
392391

393392
#endif // BITCOIN_LOGGING_H

src/mapport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void ProcessPCP()
4747
// Local functor to handle result from PCP/NATPMP mapping.
4848
auto handle_mapping = [&](std::variant<MappingResult, MappingError> &res) -> void {
4949
if (MappingResult* mapping = std::get_if<MappingResult>(&res)) {
50-
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "portmap: Added mapping %s\n", mapping->ToString());
50+
LogInfo("portmap: Added mapping %s", mapping->ToString());
5151
AddLocal(mapping->external, LOCAL_MAPPED);
5252
ret = true;
5353
actual_lifetime = std::min(actual_lifetime, mapping->lifetime);

src/test/i2p_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(unlimited_recv)
5656
i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key", sam_proxy, interrupt);
5757

5858
{
59-
ASSERT_DEBUG_LOG("Creating persistent SAM session");
59+
ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");
6060
ASSERT_DEBUG_LOG("too many bytes without a terminator");
6161

6262
i2p::Connection conn;
@@ -121,10 +121,10 @@ BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)
121121

122122
i2p::Connection conn;
123123
for (size_t i = 0; i < 5; ++i) {
124-
ASSERT_DEBUG_LOG("Creating persistent SAM session");
125-
ASSERT_DEBUG_LOG("Persistent SAM session" /* ... created */);
124+
ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");
125+
ASSERT_DEBUG_LOG("Persistent I2P SAM session" /* ... created */);
126126
ASSERT_DEBUG_LOG("Error accepting");
127-
ASSERT_DEBUG_LOG("Destroying SAM session");
127+
ASSERT_DEBUG_LOG("Destroying I2P SAM session");
128128
BOOST_REQUIRE(session.Listen(conn));
129129
BOOST_REQUIRE(!session.Accept(conn));
130130
}
@@ -161,7 +161,7 @@ BOOST_AUTO_TEST_CASE(damaged_private_key)
161161
i2p::sam::Session session(i2p_private_key_file, sam_proxy, interrupt);
162162

163163
{
164-
ASSERT_DEBUG_LOG("Creating persistent SAM session");
164+
ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");
165165
ASSERT_DEBUG_LOG(expected_error);
166166

167167
i2p::Connection conn;

src/test/logging_tests.cpp

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,6 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintStr, LogSetup)
139139
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
140140
}
141141

142-
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacrosDeprecated, LogSetup)
143-
{
144-
LogInstance().EnableCategory(BCLog::NET);
145-
LogPrintLevel(BCLog::NET, BCLog::Level::Trace, "foo4: %s\n", "bar4"); // not logged
146-
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7");
147-
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
148-
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
149-
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
150-
std::vector<std::string> log_lines{ReadDebugLogLines()};
151-
std::vector<std::string> expected{
152-
"[net] foo7: bar7",
153-
"[net:info] foo8: bar8",
154-
"[net:warning] foo9: bar9",
155-
"[net:error] foo10: bar10",
156-
};
157-
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
158-
}
159-
160142
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
161143
{
162144
LogInstance().EnableCategory(BCLog::NET);
@@ -203,26 +185,25 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
203185

204186
BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
205187
{
188+
LogInstance().SetLogLevel(BCLog::Level::Debug);
206189
LogInstance().EnableCategory(BCLog::LogFlags::ALL);
207190
LogInstance().SetCategoryLogLevel(/*category_str=*/"net", /*level_str=*/"info");
208191

209192
// Global log level
210-
LogPrintLevel(BCLog::HTTP, BCLog::Level::Info, "foo1: %s\n", "bar1");
211-
LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Trace, "foo2: %s. This log level is lower than the global one.\n", "bar2");
212-
LogPrintLevel(BCLog::VALIDATION, BCLog::Level::Warning, "foo3: %s\n", "bar3");
213-
LogPrintLevel(BCLog::RPC, BCLog::Level::Error, "foo4: %s\n", "bar4");
193+
LogInfo("info_%s", 1);
194+
LogTrace(BCLog::HTTP, "trace_%s. This log level is lower than the global one.", 2);
195+
LogDebug(BCLog::HTTP, "debug_%s", 3);
196+
LogWarning("warn_%s", 4);
197+
LogError("err_%s", 5);
214198

215199
// Category-specific log level
216-
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo5: %s\n", "bar5");
217-
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo6: %s. This log level is the same as the global one but lower than the category-specific one, which takes precedence. \n", "bar6");
218-
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo7: %s\n", "bar7");
200+
LogDebug(BCLog::NET, "debug_%s. This log level is the same as the global one but lower than the category-specific one, which takes precedence.", 6);
219201

220202
std::vector<std::string> expected = {
221-
"[http:info] foo1: bar1",
222-
"[validation:warning] foo3: bar3",
223-
"[rpc:error] foo4: bar4",
224-
"[net:warning] foo5: bar5",
225-
"[net:error] foo7: bar7",
203+
"info_1",
204+
"[http] debug_3",
205+
"[warning] warn_4",
206+
"[error] err_5",
226207
};
227208
std::vector<std::string> log_lines{ReadDebugLogLines()};
228209
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());

test/functional/p2p_i2p_sessions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def run_test(self):
2323

2424
self.log.info("Ensure we create a persistent session when -i2pacceptincoming=1")
2525
node0 = self.nodes[0]
26-
with node0.assert_debug_log(expected_msgs=["Creating persistent SAM session"]):
26+
with node0.assert_debug_log(expected_msgs=["Creating persistent I2P SAM session"]):
2727
node0.addnode(node=addr, command="onetry")
2828

2929
self.log.info("Ensure we create a transient session when -i2pacceptincoming=0")
3030
node1 = self.nodes[1]
31-
with node1.assert_debug_log(expected_msgs=["Creating transient SAM session"]):
31+
with node1.assert_debug_log(expected_msgs=["Creating transient I2P SAM session"]):
3232
node1.addnode(node=addr, command="onetry")
3333

3434

0 commit comments

Comments
 (0)