Skip to content

Commit 4901631

Browse files
committed
Merge bitcoin#25202: log: Use severity-based logging for leveldb/libevent messages, reverse LogPrintLevel order
c4e7717 refactor: Change LogPrintLevel order to category, severity (laanwj) ce92071 leveldb: Log messages from leveldb with category and debug level (laanwj) 18ec120 http: Use severity-based logging for messages from libevent (laanwj) bd971bf logging: Unconditionally log levels >= WARN (laanwj) Pull request description: Log messages from leveldb and libevent libraries in the severity+level based log format introduced in bitcoin#24464. Example of messages before: ``` 2022-05-24T18:11:57Z [libevent] libevent: event_add: event: 0x55da963fcc10 (fd 10), EV_READ call 0x7f1c7a254620 2022-05-24T18:11:57Z [libevent] libevent: Epoll ADD(1) on fd 10 okay. Old events were 0; read change was 1 (add); write change was 0 (none); close change was 0 (none) 2022-05-24T18:12:08Z leveldb: Generated table #609127@1: 6445 keys, 312916 bytes 2022-05-24T18:12:08Z leveldb: Generated table #609128@1: 5607 keys, 268548 bytes 2022-05-24T18:12:08Z leveldb: Generated table #609129@1: 189 keys, 9384 bytes 2022-05-24T18:12:08Z leveldb: Generated table #609130@1: 293 keys, 13818 bytes ``` Example of messages after: ``` 2022-05-24T17:59:52Z [libevent:debug] event_add: event: 0x5652f44dac10 (fd 10), EV_READ call 0x7f210f2e6620 2022-05-24T17:59:52Z [libevent:debug] Epoll ADD(1) on fd 10 okay. Old events were 0; read change was 1 (add); write change was 0 (none); close change was 0 (none) 2022-05-24T17:59:53Z [leveldb:debug] Recovering log dashpay#1072 2022-05-24T17:59:53Z [leveldb:debug] Level-0 table dashpay#1082: started 2022-05-24T17:59:53Z [leveldb:debug] Level-0 table dashpay#1082: 193 bytes OK 2022-05-24T17:59:53Z [leveldb:debug] Delete type=3 dashpay#1070 2022-05-24T17:59:53Z [leveldb:debug] Delete type=0 dashpay#1072 ``` The first commit changes it so that messages with level Warning and Error are always logged independent of the `-debug` setting. I think this is useful to make sure warnings and errors, which tend to be important, are not lost. In the future this should be made more configurable. Last commit changes LogPrintLevel argument order to category, severity: This is more consistent with the other functions, as well as with the logging output itself. If we want to make this change, we should do it before it's all over the place. ACKs for top commit: jonatack: Tested ACK c4e7717 Tree-SHA512: ea48a1517f8c1b23760e59933bbd64ebf09c32eb947e31b8c2696b4630ac1f4303b126720183e2de052bcede3a17e475bbf3fbb6378a12b0fa8f3582d610930d
2 parents cacbdba + c4e7717 commit 4901631

File tree

7 files changed

+42
-25
lines changed

7 files changed

+42
-25
lines changed

src/dbwrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CBitcoinLevelDBLogger : public leveldb::Logger {
1919
// This code is adapted from posix_logger.h, which is why it is using vsprintf.
2020
// Please do not do this in normal code
2121
void Logv(const char * format, va_list ap) override {
22-
if (!LogAcceptCategory(BCLog::LEVELDB)) {
22+
if (!LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug)) {
2323
return;
2424
}
2525
char buffer[500];
@@ -63,7 +63,7 @@ class CBitcoinLevelDBLogger : public leveldb::Logger {
6363

6464
assert(p <= limit);
6565
base[std::min(bufsize - 1, (int)(p - base))] = '\0';
66-
LogPrintf("leveldb: %s", base); /* Continued */
66+
LogPrintLevel(BCLog::LEVELDB, BCLog::Level::Debug, "%s", base); /* Continued */
6767
if (base != buffer) {
6868
delete[] base;
6969
}
@@ -186,7 +186,7 @@ CDBWrapper::~CDBWrapper()
186186

187187
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
188188
{
189-
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB);
189+
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug);
190190
double mem_before = 0;
191191
if (log_memory) {
192192
mem_before = DynamicMemoryUsage() / 1024.0 / 1024;

src/httpserver.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,22 @@ static void HTTPWorkQueueRun(WorkQueue<HTTPClosure>* queue, int worker_num)
344344
/** libevent event log callback */
345345
static void libevent_log_cb(int severity, const char *msg)
346346
{
347-
if (severity >= EVENT_LOG_WARN) // Log warn messages and higher without debug category
348-
LogPrintf("libevent: %s\n", msg);
349-
else
350-
LogPrint(BCLog::LIBEVENT, "libevent: %s\n", msg);
347+
BCLog::Level level;
348+
switch (severity) {
349+
case EVENT_LOG_DEBUG:
350+
level = BCLog::Level::Debug;
351+
break;
352+
case EVENT_LOG_MSG:
353+
level = BCLog::Level::Info;
354+
break;
355+
case EVENT_LOG_WARN:
356+
level = BCLog::Level::Warning;
357+
break;
358+
default: // EVENT_LOG_ERR and others are mapped to error
359+
level = BCLog::Level::Error;
360+
break;
361+
}
362+
LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg);
351363
}
352364

353365
bool InitHTTPServer()

src/logging.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,14 @@ namespace BCLog {
166166

167167
BCLog::Logger& LogInstance();
168168

169-
/** Return true if log accepts specified category */
170-
static inline bool LogAcceptCategory(BCLog::LogFlags category)
169+
/** Return true if log accepts specified category, at the specified level. */
170+
static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
171171
{
172+
// Log messages at Warning and Error level unconditionally, so that
173+
// important troubleshooting information doesn't get lost.
174+
if (level >= BCLog::Level::Warning) {
175+
return true;
176+
}
172177
return LogInstance().WillLogCategory(category);
173178
}
174179

@@ -203,14 +208,14 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
203208
// evaluating arguments when logging for the category is not enabled.
204209
#define LogPrint(category, ...) \
205210
do { \
206-
if (LogAcceptCategory((category))) { \
211+
if (LogAcceptCategory((category), BCLog::Level::Debug)) { \
207212
LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__); \
208213
} \
209214
} while (0)
210215

211-
#define LogPrintLevel(level, category, ...) \
216+
#define LogPrintLevel(category, level, ...) \
212217
do { \
213-
if (LogAcceptCategory((category))) { \
218+
if (LogAcceptCategory((category), (level))) { \
214219
LogPrintLevel_(category, level, __VA_ARGS__); \
215220
} \
216221
} while (0)

src/net.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ static CAddress GetBindAddress(SOCKET sock)
430430
if (!getsockname(sock, (struct sockaddr*)&sockaddr_bind, &sockaddr_bind_len)) {
431431
addr_bind.SetSockAddr((const struct sockaddr*)&sockaddr_bind);
432432
} else {
433-
LogPrintLevel(BCLog::Level::Warning, BCLog::NET, "getsockname failed\n");
433+
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "getsockname failed\n");
434434
}
435435
}
436436
return addr_bind;
@@ -454,7 +454,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
454454
}
455455

456456
/// debug print
457-
LogPrintLevel(BCLog::Level::Debug, BCLog::NET, "trying connection %s lastseen=%.1fhrs\n",
457+
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "trying connection %s lastseen=%.1fhrs\n",
458458
pszDest ? pszDest : addrConnect.ToString(),
459459
pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime) / 3600.0);
460460

@@ -1140,7 +1140,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
11401140
}
11411141

11421142
if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr)) {
1143-
LogPrintLevel(BCLog::Level::Warning, BCLog::NET, "Unknown socket family\n");
1143+
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "Unknown socket family\n");
11441144
} else {
11451145
addr = CAddress{MaybeFlipIPv6toCJDNS(addr), NODE_NONE};
11461146
}
@@ -2398,14 +2398,14 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
23982398
if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
23992399
{
24002400
strError = strprintf(Untranslated("Bind address family for %s not supported"), addrBind.ToString());
2401-
LogPrintLevel(BCLog::Level::Error, BCLog::NET, "%s\n", strError.original);
2401+
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
24022402
return false;
24032403
}
24042404

24052405
std::unique_ptr<Sock> sock = CreateSock(addrBind);
24062406
if (!sock) {
24072407
strError = strprintf(Untranslated("Couldn't open socket for incoming connections (socket returned error %s)"), NetworkErrorString(WSAGetLastError()));
2408-
LogPrintLevel(BCLog::Level::Error, BCLog::NET, "%s\n", strError.original);
2408+
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
24092409
return false;
24102410
}
24112411

@@ -2441,7 +2441,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
24412441
strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), PACKAGE_NAME);
24422442
else
24432443
strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
2444-
LogPrintLevel(BCLog::Level::Error, BCLog::NET, "%s\n", strError.original);
2444+
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
24452445
return false;
24462446
}
24472447
LogPrintf("Bound to %s\n", addrBind.ToString());
@@ -2450,7 +2450,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
24502450
if (listen(sock->Get(), SOMAXCONN) == SOCKET_ERROR)
24512451
{
24522452
strError = strprintf(_("Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
2453-
LogPrintLevel(BCLog::Level::Error, BCLog::NET, "%s\n", strError.original);
2453+
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
24542454
return false;
24552455
}
24562456

src/test/logging_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
9999

100100
LogPrintf("foo5: %s\n", "bar5");
101101
LogPrint(BCLog::NET, "foo6: %s\n", "bar6");
102-
LogPrintLevel(BCLog::Level::Debug, BCLog::NET, "foo7: %s\n", "bar7");
103-
LogPrintLevel(BCLog::Level::Info, BCLog::NET, "foo8: %s\n", "bar8");
104-
LogPrintLevel(BCLog::Level::Warning, BCLog::NET, "foo9: %s\n", "bar9");
105-
LogPrintLevel(BCLog::Level::Error, BCLog::NET, "foo10: %s\n", "bar10");
102+
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7");
103+
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
104+
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
105+
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
106106
std::ifstream file{tmp_log_path};
107107
std::vector<std::string> log_lines;
108108
for (std::string log; std::getline(file, log);) {

src/timedata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nOffsetSample)
9999
}
100100
}
101101

102-
if (LogAcceptCategory(BCLog::NET)) {
102+
if (LogAcceptCategory(BCLog::NET, BCLog::Level::Debug)) {
103103
std::string log_message{"time data samples: "};
104104
for (const int64_t n : vSorted) {
105105
log_message += strprintf("%+d ", n);

src/wallet/coinselection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups,
307307
}
308308
}
309309

310-
if (LogAcceptCategory(BCLog::SELECTCOINS)) {
310+
if (LogAcceptCategory(BCLog::SELECTCOINS, BCLog::Level::Debug)) {
311311
std::string log_message{"Coin selection best subset: "};
312312
for (unsigned int i = 0; i < applicable_groups.size(); i++) {
313313
if (vfBest[i]) {

0 commit comments

Comments
 (0)