Skip to content

Commit 9e4fbeb

Browse files
committed
Merge bitcoin/bitcoin#25306: logging: add LogPrintfCategory to log unconditionally with category
ecff20d logging: use LogPrintfCategory rather than a manual category (Jon Atack) eb8aab7 logging: add LogPrintfCategory to log unconditionally with category (Jon Atack) Pull request description: These are the next two commits from #25203. - Add `LogPrintfCategory` to log unconditionally while prefixing the output with the passed category name. Add documentation and a unit test, and update the `lint-logs.py` and `lint-format-strings.py` scripts. - Replace the log messages that manually print a category, with `LogPrintfCategory`. In upcoming commits, it will likely be used in many other cases, such as to replace `LogPrintf` where it makes sense. ACKs for top commit: klementtan: Code Review ACK ecff20d laanwj: Code review ACK ecff20d brunoerg: ACK ecff20d Tree-SHA512: ad3a82835254f7606efcd14b88f3d9072f1eb9b25db1321ed38ef6a4ec60efd555d78f5e19d93736f2f8500251d06f8beee9d694a153f24bf5cce3590a2a45a5
2 parents 1557014 + ecff20d commit 9e4fbeb

File tree

8 files changed

+21
-11
lines changed

8 files changed

+21
-11
lines changed

src/httpserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ bool InitHTTPServer()
400400

401401
LogPrint(BCLog::HTTP, "Initialized HTTP server\n");
402402
int workQueueDepth = std::max((long)gArgs.GetIntArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
403-
LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth);
403+
LogPrintfCategory(BCLog::HTTP, "creating work queue of depth %d\n", workQueueDepth);
404404

405405
g_work_queue = std::make_unique<WorkQueue<HTTPClosure>>(workQueueDepth);
406406
// transfer ownership to eventBase/HTTP via .release()
@@ -424,7 +424,7 @@ void StartHTTPServer()
424424
{
425425
LogPrint(BCLog::HTTP, "Starting HTTP server\n");
426426
int rpcThreads = std::max((long)gArgs.GetIntArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
427-
LogPrintf("HTTP: starting %d worker threads\n", rpcThreads);
427+
LogPrintfCategory(BCLog::HTTP, "starting %d worker threads\n", rpcThreads);
428428
g_thread_http = std::thread(ThreadHTTP, eventBase);
429429

430430
for (int i = 0; i < rpcThreads; i++) {

src/i2p.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ void Session::CreateIfNotCreatedAlready()
376376
m_session_id = session_id;
377377
m_control_sock = std::move(sock);
378378

379-
LogPrintf("I2P: SAM session created: session id=%s, my address=%s\n", m_session_id,
380-
m_my_addr.ToString());
379+
LogPrintfCategory(BCLog::I2P, "SAM session created: session id=%s, my address=%s\n",
380+
m_session_id, m_my_addr.ToString());
381381
}
382382

383383
std::unique_ptr<Sock> Session::StreamAccept()

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,8 +1506,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15061506
uiInterface.InitMessage(_("Verifying blocks…").translated);
15071507
auto check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
15081508
if (chainman.m_blockman.m_have_pruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
1509-
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks\n",
1510-
MIN_BLOCKS_TO_KEEP);
1509+
LogPrintfCategory(BCLog::PRUNE, "pruned datadir may not have more than %d blocks; only checking available blocks\n",
1510+
MIN_BLOCKS_TO_KEEP);
15111511
}
15121512
maybe_verify_error = VerifyLoadedChainstate(chainman,
15131513
fReset,

src/logging.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,26 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
199199
}
200200
}
201201

202-
203202
#define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
204203

204+
// Log unconditionally.
205205
#define LogPrintf(...) LogPrintLevel_(BCLog::LogFlags::NONE, BCLog::Level::None, __VA_ARGS__)
206206

207+
// Log unconditionally, prefixing the output with the passed category name.
208+
#define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__)
209+
207210
// Use a macro instead of a function for conditional logging to prevent
208211
// evaluating arguments when logging for the category is not enabled.
212+
213+
// Log conditionally, prefixing the output with the passed category name.
209214
#define LogPrint(category, ...) \
210215
do { \
211216
if (LogAcceptCategory((category), BCLog::Level::Debug)) { \
212217
LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__); \
213218
} \
214219
} while (0)
215220

221+
// Log conditionally, prefixing the output with the passed category name and severity level.
216222
#define LogPrintLevel(category, level, ...) \
217223
do { \
218224
if (LogAcceptCategory((category), (level))) { \

src/test/logging_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
103103
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
104104
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
105105
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
106+
LogPrintfCategory(BCLog::VALIDATION, "foo11: %s\n", "bar11");
106107
std::ifstream file{tmp_log_path};
107108
std::vector<std::string> log_lines;
108109
for (std::string log; std::getline(file, log);) {
@@ -114,7 +115,9 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
114115
"[net:debug] foo7: bar7",
115116
"[net:info] foo8: bar8",
116117
"[net:warning] foo9: bar9",
117-
"[net:error] foo10: bar10"};
118+
"[net:error] foo10: bar10",
119+
"[validation] foo11: bar11",
120+
};
118121
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
119122
}
120123

src/torcontrol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
421421
return;
422422
}
423423
service = LookupNumeric(std::string(service_id+".onion"), Params().GetDefaultPort());
424-
LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString());
424+
LogPrintfCategory(BCLog::TOR, "Got service ID %s, advertising service %s\n", service_id, service.ToString());
425425
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
426426
LogPrint(BCLog::TOR, "Cached service private key to %s\n", fs::PathToString(GetPrivateKeyFile()));
427427
} else {

test/lint/lint-format-strings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'LogConnectFailure,1',
2323
'LogPrint,1',
2424
'LogPrintf,0',
25+
'LogPrintfCategory,1',
2526
'LogPrintLevel,2',
2627
'printf,0',
2728
'snprintf,2',

test/lint/lint-logs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717

1818
def main():
19-
logs_list = check_output(["git", "grep", "--extended-regexp", r"(LogPrintLevel|LogPrintf?)\(", "--", "*.cpp"], universal_newlines=True, encoding="utf8").splitlines()
19+
logs_list = check_output(["git", "grep", "--extended-regexp", r"(LogPrintLevel|LogPrintfCategory|LogPrintf?)\(", "--", "*.cpp"], universal_newlines=True, encoding="utf8").splitlines()
2020

2121
unterminated_logs = [line for line in logs_list if not re.search(r'(\\n"|/\* Continued \*/)', line)]
2222

2323
if unterminated_logs != []:
24-
print("All calls to LogPrintf(), LogPrint(), LogPrintLevel(), and WalletLogPrintf() should be terminated with \"\\n\".")
24+
print("All calls to LogPrintf(), LogPrintfCategory(), LogPrint(), LogPrintLevel(), and WalletLogPrintf() should be terminated with \"\\n\".")
2525
print("")
2626

2727
for line in unterminated_logs:

0 commit comments

Comments
 (0)