Skip to content

Commit 6b3bb3d

Browse files
committed
Change LogAcceptCategory to use uint32_t rather than sets of strings.
This changes the logging categories to boolean flags instead of strings. This simplifies the acceptance testing by avoiding accessing a scoped static thread local pointer to a thread local set of strings. It eliminates the only use of boost::thread_specific_ptr outside of lockorder debugging. This change allows log entries to be directed to multiple categories and makes it easy to change the logging flags at runtime (e.g. via an RPC, though that isn't done by this commit.) It also eliminates the fDebug global. Configuration of unknown logging categories now produces a warning.
1 parent 351d0ad commit 6b3bb3d

30 files changed

+377
-273
lines changed

src/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void CAddrMan::Good_(const CService& addr, int64_t nTime)
233233
if (nUBucket == -1)
234234
return;
235235

236-
LogPrint("addrman", "Moving %s to tried\n", addr.ToString());
236+
LogPrint(BCLog::ADDRMAN, "Moving %s to tried\n", addr.ToString());
237237

238238
// move nId to the tried tables
239239
MakeTried(info, nId);

src/addrman.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ class CAddrMan
442442
}
443443
}
444444
if (nLost + nLostUnk > 0) {
445-
LogPrint("addrman", "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
445+
LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
446446
}
447447

448448
Check();
@@ -507,8 +507,9 @@ class CAddrMan
507507
Check();
508508
fRet |= Add_(addr, source, nTimePenalty);
509509
Check();
510-
if (fRet)
511-
LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
510+
if (fRet) {
511+
LogPrint(BCLog::ADDRMAN, "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
512+
}
512513
return fRet;
513514
}
514515

@@ -521,8 +522,9 @@ class CAddrMan
521522
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
522523
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
523524
Check();
524-
if (nAdd)
525-
LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
525+
if (nAdd) {
526+
LogPrint(BCLog::ADDRMAN, "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
527+
}
526528
return nAdd > 0;
527529
}
528530

src/blockencodings.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
164164
break;
165165
}
166166

167-
LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, SER_NETWORK, PROTOCOL_VERSION));
167+
LogPrint(BCLog::CMPCTBLOCK, "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, SER_NETWORK, PROTOCOL_VERSION));
168168

169169
return READ_STATUS_OK;
170170
}
@@ -209,10 +209,11 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
209209
return READ_STATUS_CHECKBLOCK_FAILED;
210210
}
211211

212-
LogPrint("cmpctblock", "Successfully reconstructed block %s with %lu txn prefilled, %lu txn from mempool (incl at least %lu from extra pool) and %lu txn requested\n", hash.ToString(), prefilled_count, mempool_count, extra_count, vtx_missing.size());
212+
LogPrint(BCLog::CMPCTBLOCK, "Successfully reconstructed block %s with %lu txn prefilled, %lu txn from mempool (incl at least %lu from extra pool) and %lu txn requested\n", hash.ToString(), prefilled_count, mempool_count, extra_count, vtx_missing.size());
213213
if (vtx_missing.size() < 5) {
214-
for (const auto& tx : vtx_missing)
215-
LogPrint("cmpctblock", "Reconstructed block %s required tx %s\n", hash.ToString(), tx->GetHash().ToString());
214+
for (const auto& tx : vtx_missing) {
215+
LogPrint(BCLog::CMPCTBLOCK, "Reconstructed block %s required tx %s\n", hash.ToString(), tx->GetHash().ToString());
216+
}
216217
}
217218

218219
return READ_STATUS_OK;

src/dbwrapper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class CBitcoinLevelDBLogger : public leveldb::Logger {
2121
// This code is adapted from posix_logger.h, which is why it is using vsprintf.
2222
// Please do not do this in normal code
2323
virtual void Logv(const char * format, va_list ap) override {
24-
if (!LogAcceptCategory("leveldb"))
24+
if (!LogAcceptCategory(BCLog::LEVELDB)) {
2525
return;
26+
}
2627
char buffer[500];
2728
for (int iter = 0; iter < 2; iter++) {
2829
char* base;

src/httprpc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static bool InitRPCAuthentication()
233233

234234
bool StartHTTPRPC()
235235
{
236-
LogPrint("rpc", "Starting HTTP RPC server\n");
236+
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
237237
if (!InitRPCAuthentication())
238238
return false;
239239

@@ -247,12 +247,12 @@ bool StartHTTPRPC()
247247

248248
void InterruptHTTPRPC()
249249
{
250-
LogPrint("rpc", "Interrupting HTTP RPC server\n");
250+
LogPrint(BCLog::RPC, "Interrupting HTTP RPC server\n");
251251
}
252252

253253
void StopHTTPRPC()
254254
{
255-
LogPrint("rpc", "Stopping HTTP RPC server\n");
255+
LogPrint(BCLog::RPC, "Stopping HTTP RPC server\n");
256256
UnregisterHTTPHandler("/", true);
257257
if (httpRPCTimerInterface) {
258258
RPCUnsetTimerInterface(httpRPCTimerInterface);

src/httpserver.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static bool InitHTTPAllowList()
213213
std::string strAllowed;
214214
for (const CSubNet& subnet : rpc_allow_subnets)
215215
strAllowed += subnet.ToString() + " ";
216-
LogPrint("http", "Allowing HTTP connections from: %s\n", strAllowed);
216+
LogPrint(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed);
217217
return true;
218218
}
219219

@@ -243,7 +243,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
243243
{
244244
std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
245245

246-
LogPrint("http", "Received a %s request for %s from %s\n",
246+
LogPrint(BCLog::HTTP, "Received a %s request for %s from %s\n",
247247
RequestMethodString(hreq->GetRequestMethod()), hreq->GetURI(), hreq->GetPeer().ToString());
248248

249249
// Early address-based allow check
@@ -293,18 +293,18 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
293293
/** Callback to reject HTTP requests after shutdown. */
294294
static void http_reject_request_cb(struct evhttp_request* req, void*)
295295
{
296-
LogPrint("http", "Rejecting request while shutting down\n");
296+
LogPrint(BCLog::HTTP, "Rejecting request while shutting down\n");
297297
evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
298298
}
299299

300300
/** Event dispatcher thread */
301301
static bool ThreadHTTP(struct event_base* base, struct evhttp* http)
302302
{
303303
RenameThread("bitcoin-http");
304-
LogPrint("http", "Entering http event loop\n");
304+
LogPrint(BCLog::HTTP, "Entering http event loop\n");
305305
event_base_dispatch(base);
306306
// Event loop will be interrupted by InterruptHTTPServer()
307-
LogPrint("http", "Exited http event loop\n");
307+
LogPrint(BCLog::HTTP, "Exited http event loop\n");
308308
return event_base_got_break(base) == 0;
309309
}
310310

@@ -336,7 +336,7 @@ static bool HTTPBindAddresses(struct evhttp* http)
336336

337337
// Bind addresses
338338
for (std::vector<std::pair<std::string, uint16_t> >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) {
339-
LogPrint("http", "Binding RPC on address %s port %i\n", i->first, i->second);
339+
LogPrint(BCLog::HTTP, "Binding RPC on address %s port %i\n", i->first, i->second);
340340
evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? NULL : i->first.c_str(), i->second);
341341
if (bind_handle) {
342342
boundSockets.push_back(bind_handle);
@@ -364,7 +364,7 @@ static void libevent_log_cb(int severity, const char *msg)
364364
if (severity >= EVENT_LOG_WARN) // Log warn messages and higher without debug category
365365
LogPrintf("libevent: %s\n", msg);
366366
else
367-
LogPrint("libevent", "libevent: %s\n", msg);
367+
LogPrint(BCLog::LIBEVENT, "libevent: %s\n", msg);
368368
}
369369

370370
bool InitHTTPServer()
@@ -387,10 +387,11 @@ bool InitHTTPServer()
387387
#if LIBEVENT_VERSION_NUMBER >= 0x02010100
388388
// If -debug=libevent, set full libevent debugging.
389389
// Otherwise, disable all libevent debugging.
390-
if (LogAcceptCategory("libevent"))
390+
if (LogAcceptCategory(BCLog::LIBEVENT)) {
391391
event_enable_debug_logging(EVENT_DBG_ALL);
392-
else
392+
} else {
393393
event_enable_debug_logging(EVENT_DBG_NONE);
394+
}
394395
#endif
395396
#ifdef WIN32
396397
evthread_use_windows_threads();
@@ -424,7 +425,7 @@ bool InitHTTPServer()
424425
return false;
425426
}
426427

427-
LogPrint("http", "Initialized HTTP server\n");
428+
LogPrint(BCLog::HTTP, "Initialized HTTP server\n");
428429
int workQueueDepth = std::max((long)GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
429430
LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth);
430431

@@ -439,7 +440,7 @@ std::future<bool> threadResult;
439440

440441
bool StartHTTPServer()
441442
{
442-
LogPrint("http", "Starting HTTP server\n");
443+
LogPrint(BCLog::HTTP, "Starting HTTP server\n");
443444
int rpcThreads = std::max((long)GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
444445
LogPrintf("HTTP: starting %d worker threads\n", rpcThreads);
445446
std::packaged_task<bool(event_base*, evhttp*)> task(ThreadHTTP);
@@ -455,7 +456,7 @@ bool StartHTTPServer()
455456

456457
void InterruptHTTPServer()
457458
{
458-
LogPrint("http", "Interrupting HTTP server\n");
459+
LogPrint(BCLog::HTTP, "Interrupting HTTP server\n");
459460
if (eventHTTP) {
460461
// Unlisten sockets
461462
for (evhttp_bound_socket *socket : boundSockets) {
@@ -470,15 +471,15 @@ void InterruptHTTPServer()
470471

471472
void StopHTTPServer()
472473
{
473-
LogPrint("http", "Stopping HTTP server\n");
474+
LogPrint(BCLog::HTTP, "Stopping HTTP server\n");
474475
if (workQueue) {
475-
LogPrint("http", "Waiting for HTTP worker threads to exit\n");
476+
LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
476477
workQueue->WaitExit();
477478
delete workQueue;
478479
workQueue = nullptr;
479480
}
480481
if (eventBase) {
481-
LogPrint("http", "Waiting for HTTP event thread to exit\n");
482+
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
482483
// Give event loop a few seconds to exit (to send back last RPC responses), then break it
483484
// Before this was solved with event_base_loopexit, but that didn't work as expected in
484485
// at least libevent 2.0.21 and always introduced a delay. In libevent
@@ -499,7 +500,7 @@ void StopHTTPServer()
499500
event_base_free(eventBase);
500501
eventBase = 0;
501502
}
502-
LogPrint("http", "Stopped HTTP server\n");
503+
LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
503504
}
504505

505506
struct event_base* EventBase()
@@ -646,7 +647,7 @@ HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod()
646647

647648
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
648649
{
649-
LogPrint("http", "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
650+
LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
650651
pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
651652
}
652653

@@ -659,7 +660,7 @@ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
659660
break;
660661
if (i != iend)
661662
{
662-
LogPrint("http", "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
663+
LogPrint(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
663664
pathHandlers.erase(i);
664665
}
665666
}

src/init.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void OnRPCStopped()
312312
uiInterface.NotifyBlockTip.disconnect(&RPCNotifyBlockChange);
313313
RPCNotifyBlockChange(false, nullptr);
314314
cvBlockChange.notify_all();
315-
LogPrint("rpc", "RPC stopped.\n");
315+
LogPrint(BCLog::RPC, "RPC stopped.\n");
316316
}
317317

318318
void OnRPCPreCommand(const CRPCCommand& cmd)
@@ -441,11 +441,8 @@ std::string HelpMessage(HelpMessageMode mode)
441441
strUsage += HelpMessageOpt("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT));
442442
strUsage += HelpMessageOpt("-bip9params=deployment:start:end", "Use given start/end times for specified BIP9 deployment (regtest-only)");
443443
}
444-
std::string debugCategories = "addrman, alert, bench, cmpctblock, coindb, db, http, leveldb, libevent, lock, mempool, mempoolrej, net, proxy, prune, rand, reindex, rpc, selectcoins, tor, zmq"; // Don't translate these and qt below
445-
if (mode == HMM_BITCOIN_QT)
446-
debugCategories += ", qt";
447444
strUsage += HelpMessageOpt("-debug=<category>", strprintf(_("Output debugging information (default: %u, supplying <category> is optional)"), 0) + ". " +
448-
_("If <category> is not supplied or if <category> = 1, output all debugging information.") + _("<category> can be:") + " " + debugCategories + ".");
445+
_("If <category> is not supplied or if <category> = 1, output all debugging information.") + " " + _("<category> can be:") + " " + ListLogCategories() + ".");
449446
if (showDebug)
450447
strUsage += HelpMessageOpt("-nodebug", "Turn off debugging messages, same as -debug=0");
451448
strUsage += HelpMessageOpt("-help-debug", _("Show all debugging options (usage: --help -help-debug)"));
@@ -909,12 +906,19 @@ bool AppInitParameterInteraction()
909906

910907
// ********************************************************* Step 3: parameter-to-internal-flags
911908

912-
fDebug = mapMultiArgs.count("-debug");
913-
// Special-case: if -debug=0/-nodebug is set, turn off debugging messages
914-
if (fDebug) {
909+
if (mapMultiArgs.count("-debug") > 0) {
910+
// Special-case: if -debug=0/-nodebug is set, turn off debugging messages
915911
const std::vector<std::string>& categories = mapMultiArgs.at("-debug");
916-
if (GetBoolArg("-nodebug", false) || find(categories.begin(), categories.end(), std::string("0")) != categories.end())
917-
fDebug = false;
912+
913+
if (!(GetBoolArg("-nodebug", false) || find(categories.begin(), categories.end(), std::string("0")) != categories.end())) {
914+
for (const auto& cat : categories) {
915+
uint32_t flag;
916+
if (!GetLogCategory(&flag, &cat)) {
917+
InitWarning(strprintf(_("Unsupported logging category %s.\n"), cat));
918+
}
919+
logCategories |= flag;
920+
}
921+
}
918922
}
919923

920924
// Check for -debugnet
@@ -1168,7 +1172,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
11681172
#ifndef WIN32
11691173
CreatePidFile(GetPidFile(), getpid());
11701174
#endif
1171-
if (GetBoolArg("-shrinkdebugfile", !fDebug)) {
1175+
if (GetBoolArg("-shrinkdebugfile", logCategories != BCLog::NONE)) {
11721176
// Do this first since it both loads a bunch of debug.log into memory,
11731177
// and because this needs to happen before any other debug.log printing
11741178
ShrinkDebugFile();
@@ -1492,7 +1496,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14921496
break;
14931497
}
14941498
} catch (const std::exception& e) {
1495-
if (fDebug) LogPrintf("%s\n", e.what());
1499+
LogPrintf("%s\n", e.what());
14961500
strLoadError = _("Error opening block database");
14971501
break;
14981502
}

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
204204
}
205205
int64_t nTime2 = GetTimeMicros();
206206

207-
LogPrint("bench", "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n", 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart));
207+
LogPrint(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n", 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart));
208208

209209
return std::move(pblocktemplate);
210210
}

0 commit comments

Comments
 (0)