Skip to content

Commit cfce581

Browse files
committed
[LevelDB] Plug leveldb logs to bitcoin logs
1 parent ce01e62 commit cfce581

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/dbwrapper.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,64 @@
1414
#include <leveldb/filter_policy.h>
1515
#include <memenv.h>
1616
#include <stdint.h>
17+
#include <algorithm>
18+
19+
class CBitcoinLevelDBLogger : public leveldb::Logger {
20+
public:
21+
// This code is adapted from posix_logger.h, which is why it is using vsprintf.
22+
// Please do not do this in normal code
23+
virtual void Logv(const char * format, va_list ap) override {
24+
if (!LogAcceptCategory("leveldb"))
25+
return;
26+
char buffer[500];
27+
for (int iter = 0; iter < 2; iter++) {
28+
char* base;
29+
int bufsize;
30+
if (iter == 0) {
31+
bufsize = sizeof(buffer);
32+
base = buffer;
33+
}
34+
else {
35+
bufsize = 30000;
36+
base = new char[bufsize];
37+
}
38+
char* p = base;
39+
char* limit = base + bufsize;
40+
41+
// Print the message
42+
if (p < limit) {
43+
va_list backup_ap;
44+
va_copy(backup_ap, ap);
45+
// Do not use vsnprintf elsewhere in bitcoin source code, see above.
46+
p += vsnprintf(p, limit - p, format, backup_ap);
47+
va_end(backup_ap);
48+
}
49+
50+
// Truncate to available space if necessary
51+
if (p >= limit) {
52+
if (iter == 0) {
53+
continue; // Try again with larger buffer
54+
}
55+
else {
56+
p = limit - 1;
57+
}
58+
}
59+
60+
// Add newline if necessary
61+
if (p == base || p[-1] != '\n') {
62+
*p++ = '\n';
63+
}
64+
65+
assert(p <= limit);
66+
base[std::min(bufsize - 1, (int)(p - base))] = '\0';
67+
LogPrintStr(base);
68+
if (base != buffer) {
69+
delete[] base;
70+
}
71+
break;
72+
}
73+
}
74+
};
1775

1876
static leveldb::Options GetOptions(size_t nCacheSize)
1977
{
@@ -23,6 +81,7 @@ static leveldb::Options GetOptions(size_t nCacheSize)
2381
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
2482
options.compression = leveldb::kNoCompression;
2583
options.max_open_files = 64;
84+
options.info_log = new CBitcoinLevelDBLogger();
2685
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
2786
// LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
2887
// on corruption in later versions.
@@ -82,6 +141,8 @@ CDBWrapper::~CDBWrapper()
82141
pdb = NULL;
83142
delete options.filter_policy;
84143
options.filter_policy = NULL;
144+
delete options.info_log;
145+
options.info_log = NULL;
85146
delete options.block_cache;
86147
options.block_cache = NULL;
87148
delete penv;

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ std::string HelpMessage(HelpMessageMode mode)
430430
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));
431431
strUsage += HelpMessageOpt("-bip9params=deployment:start:end", "Use given start/end times for specified BIP9 deployment (regtest-only)");
432432
}
433-
std::string debugCategories = "addrman, alert, bench, cmpctblock, coindb, db, http, libevent, lock, mempool, mempoolrej, net, proxy, prune, rand, reindex, rpc, selectcoins, tor, zmq"; // Don't translate these and qt below
433+
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
434434
if (mode == HMM_BITCOIN_QT)
435435
debugCategories += ", qt";
436436
strUsage += HelpMessageOpt("-debug=<category>", strprintf(_("Output debugging information (default: %u, supplying <category> is optional)"), 0) + ". " +

0 commit comments

Comments
 (0)