2018-04-20 07:02:12 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2023-04-25 13:51:26 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2018-04-20 07:02:12 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <logging.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/threadnames.h>
|
|
|
|
#include <util/time.h>
|
2018-04-20 07:02:12 +02:00
|
|
|
|
Merge bitcoin/bitcoin#22530: log: sort logging categories alphabetically
d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb test: assert logging categories are sorted in rpc and help (Jon Atack)
17bbff3b88132c0c95b29b59100456b85e26df75 log, refactor: use guard clause in LogCategoriesList() (Jon Atack)
7c57297319bc386afaf06528778384fe58576ef9 log: sort LogCategoriesList and LogCategoriesString alphabetically (Jon Atack)
f720cfa824f1be863349e7016080f8fb1c3c76c2 test: verify number of categories returned by logging RPC (Jon Atack)
Pull request description:
Sorting the logging categories seems more user-friendly with the number of categories we now have, allowing CLI users to more quickly find a particular category.
before
```
$ bitcoin-cli help logging
...
The valid logging categories are: net, tor, mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman, selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej, libevent, coindb, qt, leveldb, validation, i2p, ipc
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: net, tor,
mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman,
selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej,
libevent, coindb, qt, leveldb, validation, i2p, ipc.
$ bitcoin-cli logging [] '["addrman"]'
{
"net": false,
"tor": true,
"mempool": false,
"http": false,
"bench": false,
"zmq": false,
"walletdb": false,
"rpc": false,
"estimatefee": false,
"addrman": false,
"selectcoins": false,
"reindex": false,
"cmpctblock": false,
"rand": false,
"prune": false,
"proxy": true,
"mempoolrej": false,
"libevent": false,
"coindb": false,
"qt": false,
"leveldb": false,
"validation": false,
"i2p": true,
"ipc": false
}
```
after
```
$ bitcoin-cli help logging
...
The valid logging categories are: addrman, bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb, libevent, mempool, mempoolrej, net, proxy, prune, qt, rand, reindex, rpc, selectcoins, tor, validation, walletdb, zmq
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: addrman,
bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb,
libevent, mempool, mempoolrej, net, proxy, prune, qt, rand,
reindex, rpc, selectcoins, tor, validation, walletdb, zmq.
$ bitcoin-cli logging [] '["addrman"]'
{
"addrman": false,
"bench": false,
"cmpctblock": false,
"coindb": false,
"estimatefee": false,
"http": false,
"i2p": false,
"ipc": false,
"leveldb": false,
"libevent": false,
"mempool": false,
"mempoolrej": false,
"net": false,
"proxy": false,
"prune": false,
"qt": false,
"rand": false,
"reindex": false,
"rpc": false,
"selectcoins": false,
"tor": false,
"validation": false,
"walletdb": false,
"zmq": false
}
```
ACKs for top commit:
theStack:
re-ACK d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb
Tree-SHA512: d546257f562b0a288d1b19a028f1a510aaf21bd21da058e7c84653d305ea8662ecb4647ebefd2b97411f845fe5b0b841d40d3fe6814eefcb8ce82df341dfce22
2021-07-28 14:29:53 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
|
2018-04-20 07:02:12 +02:00
|
|
|
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
|
|
|
|
|
2019-02-04 20:26:02 +01:00
|
|
|
BCLog::Logger& LogInstance()
|
|
|
|
{
|
2018-04-20 07:02:12 +02:00
|
|
|
/**
|
2018-05-01 04:13:30 +02:00
|
|
|
* NOTE: the logger instances is leaked on exit. This is ugly, but will be
|
|
|
|
* cleaned up by the OS/libc. Defining a logger as a global object doesn't work
|
|
|
|
* since the order of destruction of static/global objects is undefined.
|
|
|
|
* Consider if the logger gets destroyed, and then some later destructor calls
|
|
|
|
* LogPrintf, maybe indirectly, and you get a core dump at shutdown trying to
|
|
|
|
* access the logger. When the shutdown sequence is fully audited and tested,
|
|
|
|
* explicit destruction of these objects can be implemented by changing this
|
|
|
|
* from a raw pointer to a std::unique_ptr.
|
2020-05-05 07:07:47 +02:00
|
|
|
* Since the ~Logger() destructor is never called, the Logger class and all
|
|
|
|
* its subclasses must have implicitly-defined destructors.
|
2018-04-20 07:02:12 +02:00
|
|
|
*
|
2018-05-01 04:13:30 +02:00
|
|
|
* This method of initialization was originally introduced in
|
|
|
|
* ee3374234c60aba2cc4c5cd5cac1c0aefc2d817c.
|
2018-04-20 07:02:12 +02:00
|
|
|
*/
|
2019-02-04 20:26:02 +01:00
|
|
|
static BCLog::Logger* g_logger{new BCLog::Logger()};
|
|
|
|
return *g_logger;
|
|
|
|
}
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
bool fLogIPs = DEFAULT_LOGIPS;
|
2018-04-20 07:02:12 +02:00
|
|
|
|
|
|
|
static int FileWriteStr(const std::string &str, FILE *fp)
|
|
|
|
{
|
|
|
|
return fwrite(str.data(), 1, str.size(), fp);
|
|
|
|
}
|
|
|
|
|
2021-06-08 04:19:35 +02:00
|
|
|
bool BCLog::Logger::StartLogging()
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
2021-06-09 14:02:42 +02:00
|
|
|
StdLockGuard scoped_lock(m_cs);
|
2018-05-01 04:13:30 +02:00
|
|
|
|
2021-06-08 04:19:35 +02:00
|
|
|
assert(m_buffering);
|
2018-05-01 04:13:30 +02:00
|
|
|
assert(m_fileout == nullptr);
|
|
|
|
|
2021-06-08 04:19:35 +02:00
|
|
|
if (m_print_to_file) {
|
|
|
|
assert(!m_file_path.empty());
|
|
|
|
m_fileout = fsbridge::fopen(m_file_path, "a");
|
|
|
|
if (!m_fileout) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setbuf(m_fileout, nullptr); // unbuffered
|
|
|
|
|
|
|
|
// Add newlines to the logfile to distinguish this execution from the
|
|
|
|
// last one.
|
|
|
|
FileWriteStr("\n\n\n\n\n", m_fileout);
|
2018-05-01 04:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// dump buffered messages from before we opened the log
|
2021-06-08 04:19:35 +02:00
|
|
|
m_buffering = false;
|
2018-05-01 04:13:30 +02:00
|
|
|
while (!m_msgs_before_open.empty()) {
|
2021-06-08 04:19:35 +02:00
|
|
|
const std::string& s = m_msgs_before_open.front();
|
|
|
|
|
|
|
|
if (m_print_to_file) FileWriteStr(s, m_fileout);
|
|
|
|
if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
|
2022-02-26 06:07:36 +01:00
|
|
|
for (const auto& cb : m_print_callbacks) {
|
|
|
|
cb(s);
|
|
|
|
}
|
2021-06-08 04:19:35 +02:00
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
m_msgs_before_open.pop_front();
|
|
|
|
}
|
2021-06-08 04:19:35 +02:00
|
|
|
if (m_print_to_console) fflush(stdout);
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
return true;
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 12:13:08 +02:00
|
|
|
void BCLog::Logger::DisconnectTestLogger()
|
|
|
|
{
|
|
|
|
StdLockGuard scoped_lock(m_cs);
|
|
|
|
m_buffering = true;
|
|
|
|
if (m_fileout != nullptr) fclose(m_fileout);
|
|
|
|
m_fileout = nullptr;
|
2022-02-26 06:07:36 +01:00
|
|
|
m_print_callbacks.clear();
|
2019-06-25 12:13:08 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
2018-05-01 04:13:30 +02:00
|
|
|
m_categories |= flag;
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
bool BCLog::Logger::EnableCategory(const std::string& str)
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
2018-05-01 04:13:30 +02:00
|
|
|
BCLog::LogFlags flag;
|
2019-11-10 10:50:36 +01:00
|
|
|
if (!GetLogCategory(flag, str)) {
|
|
|
|
if (str == "db") {
|
|
|
|
// DEPRECATION: Added in 0.20, should start returning an error in 0.21
|
|
|
|
LogPrintf("Warning: logging category 'db' is deprecated, use 'walletdb' instead\n");
|
|
|
|
EnableCategory(BCLog::WALLETDB);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-01 04:13:30 +02:00
|
|
|
EnableCategory(flag);
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-20 07:02:12 +02:00
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
|
|
|
|
{
|
|
|
|
m_categories &= ~flag;
|
|
|
|
}
|
2018-04-20 07:02:12 +02:00
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
bool BCLog::Logger::DisableCategory(const std::string& str)
|
|
|
|
{
|
|
|
|
BCLog::LogFlags flag;
|
|
|
|
if (!GetLogCategory(flag, str)) return false;
|
|
|
|
DisableCategory(flag);
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-20 07:02:12 +02:00
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
|
|
|
|
{
|
|
|
|
return (m_categories.load(std::memory_order_relaxed) & category) != 0;
|
|
|
|
}
|
2018-04-20 07:02:12 +02:00
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
bool BCLog::Logger::DefaultShrinkDebugFile() const
|
|
|
|
{
|
|
|
|
return m_categories == BCLog::NONE;
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
|
Merge bitcoin/bitcoin#22530: log: sort logging categories alphabetically
d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb test: assert logging categories are sorted in rpc and help (Jon Atack)
17bbff3b88132c0c95b29b59100456b85e26df75 log, refactor: use guard clause in LogCategoriesList() (Jon Atack)
7c57297319bc386afaf06528778384fe58576ef9 log: sort LogCategoriesList and LogCategoriesString alphabetically (Jon Atack)
f720cfa824f1be863349e7016080f8fb1c3c76c2 test: verify number of categories returned by logging RPC (Jon Atack)
Pull request description:
Sorting the logging categories seems more user-friendly with the number of categories we now have, allowing CLI users to more quickly find a particular category.
before
```
$ bitcoin-cli help logging
...
The valid logging categories are: net, tor, mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman, selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej, libevent, coindb, qt, leveldb, validation, i2p, ipc
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: net, tor,
mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman,
selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej,
libevent, coindb, qt, leveldb, validation, i2p, ipc.
$ bitcoin-cli logging [] '["addrman"]'
{
"net": false,
"tor": true,
"mempool": false,
"http": false,
"bench": false,
"zmq": false,
"walletdb": false,
"rpc": false,
"estimatefee": false,
"addrman": false,
"selectcoins": false,
"reindex": false,
"cmpctblock": false,
"rand": false,
"prune": false,
"proxy": true,
"mempoolrej": false,
"libevent": false,
"coindb": false,
"qt": false,
"leveldb": false,
"validation": false,
"i2p": true,
"ipc": false
}
```
after
```
$ bitcoin-cli help logging
...
The valid logging categories are: addrman, bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb, libevent, mempool, mempoolrej, net, proxy, prune, qt, rand, reindex, rpc, selectcoins, tor, validation, walletdb, zmq
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: addrman,
bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb,
libevent, mempool, mempoolrej, net, proxy, prune, qt, rand,
reindex, rpc, selectcoins, tor, validation, walletdb, zmq.
$ bitcoin-cli logging [] '["addrman"]'
{
"addrman": false,
"bench": false,
"cmpctblock": false,
"coindb": false,
"estimatefee": false,
"http": false,
"i2p": false,
"ipc": false,
"leveldb": false,
"libevent": false,
"mempool": false,
"mempoolrej": false,
"net": false,
"proxy": false,
"prune": false,
"qt": false,
"rand": false,
"reindex": false,
"rpc": false,
"selectcoins": false,
"tor": false,
"validation": false,
"walletdb": false,
"zmq": false
}
```
ACKs for top commit:
theStack:
re-ACK d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb
Tree-SHA512: d546257f562b0a288d1b19a028f1a510aaf21bd21da058e7c84653d305ea8662ecb4647ebefd2b97411f845fe5b0b841d40d3fe6814eefcb8ce82df341dfce22
2021-07-28 14:29:53 +02:00
|
|
|
struct CLogCategoryDesc {
|
2018-05-01 04:13:30 +02:00
|
|
|
BCLog::LogFlags flag;
|
2018-04-20 07:02:12 +02:00
|
|
|
std::string category;
|
|
|
|
};
|
|
|
|
|
|
|
|
const CLogCategoryDesc LogCategories[] =
|
|
|
|
{
|
|
|
|
{BCLog::NONE, "0"},
|
|
|
|
{BCLog::NONE, "none"},
|
|
|
|
{BCLog::NET, "net"},
|
|
|
|
{BCLog::TOR, "tor"},
|
|
|
|
{BCLog::MEMPOOL, "mempool"},
|
|
|
|
{BCLog::HTTP, "http"},
|
2020-07-18 21:22:44 +02:00
|
|
|
{BCLog::BENCHMARK, "bench"},
|
2018-04-20 07:02:12 +02:00
|
|
|
{BCLog::ZMQ, "zmq"},
|
2019-11-10 10:50:36 +01:00
|
|
|
{BCLog::WALLETDB, "walletdb"},
|
2018-04-20 07:02:12 +02:00
|
|
|
{BCLog::RPC, "rpc"},
|
|
|
|
{BCLog::ESTIMATEFEE, "estimatefee"},
|
|
|
|
{BCLog::ADDRMAN, "addrman"},
|
|
|
|
{BCLog::SELECTCOINS, "selectcoins"},
|
|
|
|
{BCLog::REINDEX, "reindex"},
|
|
|
|
{BCLog::CMPCTBLOCK, "cmpctblock"},
|
2020-07-18 21:22:44 +02:00
|
|
|
{BCLog::RANDOM, "rand"},
|
2018-04-20 07:02:12 +02:00
|
|
|
{BCLog::PRUNE, "prune"},
|
|
|
|
{BCLog::PROXY, "proxy"},
|
|
|
|
{BCLog::MEMPOOLREJ, "mempoolrej"},
|
|
|
|
{BCLog::LIBEVENT, "libevent"},
|
|
|
|
{BCLog::COINDB, "coindb"},
|
|
|
|
{BCLog::QT, "qt"},
|
|
|
|
{BCLog::LEVELDB, "leveldb"},
|
2020-01-09 20:37:09 +01:00
|
|
|
{BCLog::VALIDATION, "validation"},
|
2020-11-18 17:13:27 +01:00
|
|
|
{BCLog::I2P, "i2p"},
|
Merge bitcoin/bitcoin#22736: log, sync: change lock contention from preprocessor directive to log category
7e698732836121912f179b7c743a72dd6fdffa72 sync: remove DEBUG_LOCKCONTENTION preprocessor directives (Jon Atack)
9b08006bc502e67956d6ab518388fad6397cac8d log, sync: improve lock contention logging and add time duration (Jon Atack)
3f4c6b87f1098436693c4990f2082515ec0ece26 log, timer: add timing macro in usec LOG_TIME_MICROS_WITH_CATEGORY (Jon Atack)
b7a17444e0746c562ae97b26eba431577947b06a log, sync: add LOCK logging category, apply it to lock contention (Jon Atack)
Pull request description:
To enable lock contention logging, `DEBUG_LOCKCONTENTION` has to be defined at compilation. Once built, the logging is not limited to a category and is high frequency, verbose and in all-caps. With these factors combined, it seems likely to be rarely used.
This patch:
- adds a `lock` logging category
- adds a timing macro in microseconds, `LOG_TIME_MICROS_WITH_CATEGORY`
- updates `BCLog::LogMsg()` to omit irrelevant decimals for microseconds and skip unneeded code and math
- improves the lock contention logging, drops the all-caps, and displays the duration in microseconds
- removes the conditional compilation directives
- allows lock contentions to be logged on startup with `-debug=lock` or at run time with `bitcoin-cli logging '["lock"]'`
```
$ bitcoind -signet -debug=lock
2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1920 started
2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1920 completed (4μs)
2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1302 started
2021-09-01T12:40:01Z LockContention: cs_vNodes, net.cpp:1302 completed (4μs)
2021-09-01T12:40:02Z LockContention: cs_vNodes, net.cpp:2242 started
2021-09-01T12:40:02Z LockContention: cs_vNodes, net.cpp:2242 completed (20μs)
2021-09-01T12:43:04Z LockContention: ::cs_main, validation.cpp:4980 started
2021-09-01T12:43:04Z LockContention: ::cs_main, validation.cpp:4980 completed (3μs)
$ bitcoin-cli -signet logging
"lock": true,
$ bitcoin-cli -signet logging [] '["lock"]'
"lock": false,
$ bitcoin-cli -signet logging '["lock"]'
"lock": true,
```
I've tested this with Clang 13 and GCC 10.2.1, on Debian, with and without `--enable-debug`.
ACKs for top commit:
hebasto:
re-ACK 7e698732836121912f179b7c743a72dd6fdffa72, added a contention duration to the log message since my [previous](https://github.com/bitcoin/bitcoin/pull/22736#pullrequestreview-743764606) review.
theStack:
re-ACK 7e698732836121912f179b7c743a72dd6fdffa72 🔏 ⏲️
Tree-SHA512: c4b5eb88d3a2c051acaa842b3055ce30efde1f114f61da6e55fcaa27476c1c33a60bc419f7f5ccda532e1bdbe70815222ec2b2b6d9226f29c8e94e598aacfee7
2021-09-06 10:31:05 +02:00
|
|
|
{BCLog::LOCK, "lock"},
|
2018-04-20 07:02:12 +02:00
|
|
|
{BCLog::ALL, "1"},
|
|
|
|
{BCLog::ALL, "all"},
|
|
|
|
|
|
|
|
//Start Dash
|
|
|
|
{BCLog::CHAINLOCKS, "chainlocks"},
|
|
|
|
{BCLog::GOBJECT, "gobject"},
|
|
|
|
{BCLog::INSTANTSEND, "instantsend"},
|
|
|
|
{BCLog::LLMQ, "llmq"},
|
|
|
|
{BCLog::LLMQ_DKG, "llmq-dkg"},
|
|
|
|
{BCLog::LLMQ_SIGS, "llmq-sigs"},
|
|
|
|
{BCLog::MNPAYMENTS, "mnpayments"},
|
|
|
|
{BCLog::MNSYNC, "mnsync"},
|
2021-03-17 23:36:11 +01:00
|
|
|
{BCLog::COINJOIN, "coinjoin"},
|
2018-04-20 07:02:12 +02:00
|
|
|
{BCLog::SPORK, "spork"},
|
|
|
|
{BCLog::NETCONN, "netconn"},
|
2023-11-16 18:56:34 +01:00
|
|
|
{BCLog::CREDITPOOL, "creditpool"},
|
|
|
|
{BCLog::EHF, "ehf"},
|
2018-05-01 04:13:30 +02:00
|
|
|
{BCLog::DASH, "dash"},
|
2018-04-20 07:02:12 +02:00
|
|
|
//End Dash
|
|
|
|
};
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
2018-05-01 04:13:30 +02:00
|
|
|
if (str == "") {
|
|
|
|
flag = BCLog::ALL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (const CLogCategoryDesc& category_desc : LogCategories) {
|
|
|
|
if (category_desc.category == str) {
|
|
|
|
flag = category_desc.flag;
|
2018-04-20 07:02:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-28 14:47:45 +02:00
|
|
|
std::vector<LogCategory> BCLog::Logger::LogCategoriesList(bool enabled_only) const
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
Merge bitcoin/bitcoin#22530: log: sort logging categories alphabetically
d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb test: assert logging categories are sorted in rpc and help (Jon Atack)
17bbff3b88132c0c95b29b59100456b85e26df75 log, refactor: use guard clause in LogCategoriesList() (Jon Atack)
7c57297319bc386afaf06528778384fe58576ef9 log: sort LogCategoriesList and LogCategoriesString alphabetically (Jon Atack)
f720cfa824f1be863349e7016080f8fb1c3c76c2 test: verify number of categories returned by logging RPC (Jon Atack)
Pull request description:
Sorting the logging categories seems more user-friendly with the number of categories we now have, allowing CLI users to more quickly find a particular category.
before
```
$ bitcoin-cli help logging
...
The valid logging categories are: net, tor, mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman, selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej, libevent, coindb, qt, leveldb, validation, i2p, ipc
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: net, tor,
mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman,
selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej,
libevent, coindb, qt, leveldb, validation, i2p, ipc.
$ bitcoin-cli logging [] '["addrman"]'
{
"net": false,
"tor": true,
"mempool": false,
"http": false,
"bench": false,
"zmq": false,
"walletdb": false,
"rpc": false,
"estimatefee": false,
"addrman": false,
"selectcoins": false,
"reindex": false,
"cmpctblock": false,
"rand": false,
"prune": false,
"proxy": true,
"mempoolrej": false,
"libevent": false,
"coindb": false,
"qt": false,
"leveldb": false,
"validation": false,
"i2p": true,
"ipc": false
}
```
after
```
$ bitcoin-cli help logging
...
The valid logging categories are: addrman, bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb, libevent, mempool, mempoolrej, net, proxy, prune, qt, rand, reindex, rpc, selectcoins, tor, validation, walletdb, zmq
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: addrman,
bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb,
libevent, mempool, mempoolrej, net, proxy, prune, qt, rand,
reindex, rpc, selectcoins, tor, validation, walletdb, zmq.
$ bitcoin-cli logging [] '["addrman"]'
{
"addrman": false,
"bench": false,
"cmpctblock": false,
"coindb": false,
"estimatefee": false,
"http": false,
"i2p": false,
"ipc": false,
"leveldb": false,
"libevent": false,
"mempool": false,
"mempoolrej": false,
"net": false,
"proxy": false,
"prune": false,
"qt": false,
"rand": false,
"reindex": false,
"rpc": false,
"selectcoins": false,
"tor": false,
"validation": false,
"walletdb": false,
"zmq": false
}
```
ACKs for top commit:
theStack:
re-ACK d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb
Tree-SHA512: d546257f562b0a288d1b19a028f1a510aaf21bd21da058e7c84653d305ea8662ecb4647ebefd2b97411f845fe5b0b841d40d3fe6814eefcb8ce82df341dfce22
2021-07-28 14:29:53 +02:00
|
|
|
// Sort log categories by alphabetical order.
|
|
|
|
std::array<CLogCategoryDesc, std::size(LogCategories)> categories;
|
|
|
|
std::copy(std::begin(LogCategories), std::end(LogCategories), categories.begin());
|
|
|
|
std::sort(categories.begin(), categories.end(), [](auto a, auto b) { return a.category < b.category; });
|
|
|
|
|
2020-04-27 01:57:33 +02:00
|
|
|
std::vector<LogCategory> ret;
|
Merge bitcoin/bitcoin#22530: log: sort logging categories alphabetically
d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb test: assert logging categories are sorted in rpc and help (Jon Atack)
17bbff3b88132c0c95b29b59100456b85e26df75 log, refactor: use guard clause in LogCategoriesList() (Jon Atack)
7c57297319bc386afaf06528778384fe58576ef9 log: sort LogCategoriesList and LogCategoriesString alphabetically (Jon Atack)
f720cfa824f1be863349e7016080f8fb1c3c76c2 test: verify number of categories returned by logging RPC (Jon Atack)
Pull request description:
Sorting the logging categories seems more user-friendly with the number of categories we now have, allowing CLI users to more quickly find a particular category.
before
```
$ bitcoin-cli help logging
...
The valid logging categories are: net, tor, mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman, selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej, libevent, coindb, qt, leveldb, validation, i2p, ipc
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: net, tor,
mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman,
selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej,
libevent, coindb, qt, leveldb, validation, i2p, ipc.
$ bitcoin-cli logging [] '["addrman"]'
{
"net": false,
"tor": true,
"mempool": false,
"http": false,
"bench": false,
"zmq": false,
"walletdb": false,
"rpc": false,
"estimatefee": false,
"addrman": false,
"selectcoins": false,
"reindex": false,
"cmpctblock": false,
"rand": false,
"prune": false,
"proxy": true,
"mempoolrej": false,
"libevent": false,
"coindb": false,
"qt": false,
"leveldb": false,
"validation": false,
"i2p": true,
"ipc": false
}
```
after
```
$ bitcoin-cli help logging
...
The valid logging categories are: addrman, bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb, libevent, mempool, mempoolrej, net, proxy, prune, qt, rand, reindex, rpc, selectcoins, tor, validation, walletdb, zmq
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: addrman,
bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb,
libevent, mempool, mempoolrej, net, proxy, prune, qt, rand,
reindex, rpc, selectcoins, tor, validation, walletdb, zmq.
$ bitcoin-cli logging [] '["addrman"]'
{
"addrman": false,
"bench": false,
"cmpctblock": false,
"coindb": false,
"estimatefee": false,
"http": false,
"i2p": false,
"ipc": false,
"leveldb": false,
"libevent": false,
"mempool": false,
"mempoolrej": false,
"net": false,
"proxy": false,
"prune": false,
"qt": false,
"rand": false,
"reindex": false,
"rpc": false,
"selectcoins": false,
"tor": false,
"validation": false,
"walletdb": false,
"zmq": false
}
```
ACKs for top commit:
theStack:
re-ACK d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb
Tree-SHA512: d546257f562b0a288d1b19a028f1a510aaf21bd21da058e7c84653d305ea8662ecb4647ebefd2b97411f845fe5b0b841d40d3fe6814eefcb8ce82df341dfce22
2021-07-28 14:29:53 +02:00
|
|
|
for (const CLogCategoryDesc& category_desc : categories) {
|
|
|
|
if (category_desc.flag == BCLog::NONE || category_desc.flag == BCLog::ALL || category_desc.flag == BCLog::DASH) continue;
|
|
|
|
LogCategory catActive;
|
|
|
|
catActive.category = category_desc.category;
|
|
|
|
catActive.active = WillLogCategory(category_desc.flag);
|
|
|
|
if (!enabled_only || catActive.active) {
|
|
|
|
ret.push_back(catActive);
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:54:20 +02:00
|
|
|
std::string BCLog::Logger::LogTimestampStr(const std::string& str)
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
|
|
|
std::string strStamped;
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
if (!m_log_timestamps)
|
2018-04-20 07:02:12 +02:00
|
|
|
return str;
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
if (m_started_new_line) {
|
2018-04-20 07:02:12 +02:00
|
|
|
int64_t nTimeMicros = GetTimeMicros();
|
2020-12-16 09:55:24 +01:00
|
|
|
strStamped = FormatISO8601DateTime(nTimeMicros/1000000);
|
2018-05-01 04:13:30 +02:00
|
|
|
if (m_log_time_micros) {
|
2020-12-16 09:55:24 +01:00
|
|
|
strStamped.pop_back();
|
|
|
|
strStamped += strprintf(".%06dZ", nTimeMicros%1000000);
|
|
|
|
}
|
2024-01-16 21:11:49 +01:00
|
|
|
std::chrono::seconds mocktime = GetMockTime();
|
|
|
|
if (mocktime > 0s) {
|
|
|
|
strStamped += " (mocktime: " + FormatISO8601DateTime(count_seconds(mocktime)) + ")";
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
strStamped += ' ' + str;
|
|
|
|
} else
|
|
|
|
strStamped = str;
|
|
|
|
|
|
|
|
return strStamped;
|
|
|
|
}
|
|
|
|
|
2019-10-10 13:25:08 +02:00
|
|
|
namespace BCLog {
|
|
|
|
/** Belts and suspenders: make sure outgoing log messages don't contain
|
|
|
|
* potentially suspicious characters, such as terminal control codes.
|
|
|
|
*
|
|
|
|
* This escapes control characters except newline ('\n') in C syntax.
|
|
|
|
* It escapes instead of removes them to still allow for troubleshooting
|
|
|
|
* issues where they accidentally end up in strings.
|
|
|
|
*/
|
|
|
|
std::string LogEscapeMessage(const std::string& str) {
|
|
|
|
std::string ret;
|
|
|
|
for (char ch_in : str) {
|
|
|
|
uint8_t ch = (uint8_t)ch_in;
|
|
|
|
if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
|
|
|
|
ret += ch_in;
|
|
|
|
} else {
|
|
|
|
ret += strprintf("\\x%02x", ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
Merge bitcoin/bitcoin#22530: log: sort logging categories alphabetically
d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb test: assert logging categories are sorted in rpc and help (Jon Atack)
17bbff3b88132c0c95b29b59100456b85e26df75 log, refactor: use guard clause in LogCategoriesList() (Jon Atack)
7c57297319bc386afaf06528778384fe58576ef9 log: sort LogCategoriesList and LogCategoriesString alphabetically (Jon Atack)
f720cfa824f1be863349e7016080f8fb1c3c76c2 test: verify number of categories returned by logging RPC (Jon Atack)
Pull request description:
Sorting the logging categories seems more user-friendly with the number of categories we now have, allowing CLI users to more quickly find a particular category.
before
```
$ bitcoin-cli help logging
...
The valid logging categories are: net, tor, mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman, selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej, libevent, coindb, qt, leveldb, validation, i2p, ipc
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: net, tor,
mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman,
selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej,
libevent, coindb, qt, leveldb, validation, i2p, ipc.
$ bitcoin-cli logging [] '["addrman"]'
{
"net": false,
"tor": true,
"mempool": false,
"http": false,
"bench": false,
"zmq": false,
"walletdb": false,
"rpc": false,
"estimatefee": false,
"addrman": false,
"selectcoins": false,
"reindex": false,
"cmpctblock": false,
"rand": false,
"prune": false,
"proxy": true,
"mempoolrej": false,
"libevent": false,
"coindb": false,
"qt": false,
"leveldb": false,
"validation": false,
"i2p": true,
"ipc": false
}
```
after
```
$ bitcoin-cli help logging
...
The valid logging categories are: addrman, bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb, libevent, mempool, mempoolrej, net, proxy, prune, qt, rand, reindex, rpc, selectcoins, tor, validation, walletdb, zmq
$ bitcoind -h | grep -A8 "debug=<category>"
-debug=<category>
...
output all debugging information. <category> can be: addrman,
bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb,
libevent, mempool, mempoolrej, net, proxy, prune, qt, rand,
reindex, rpc, selectcoins, tor, validation, walletdb, zmq.
$ bitcoin-cli logging [] '["addrman"]'
{
"addrman": false,
"bench": false,
"cmpctblock": false,
"coindb": false,
"estimatefee": false,
"http": false,
"i2p": false,
"ipc": false,
"leveldb": false,
"libevent": false,
"mempool": false,
"mempoolrej": false,
"net": false,
"proxy": false,
"prune": false,
"qt": false,
"rand": false,
"reindex": false,
"rpc": false,
"selectcoins": false,
"tor": false,
"validation": false,
"walletdb": false,
"zmq": false
}
```
ACKs for top commit:
theStack:
re-ACK d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb
Tree-SHA512: d546257f562b0a288d1b19a028f1a510aaf21bd21da058e7c84653d305ea8662ecb4647ebefd2b97411f845fe5b0b841d40d3fe6814eefcb8ce82df341dfce22
2021-07-28 14:29:53 +02:00
|
|
|
} // namespace BCLog
|
2019-10-10 13:25:08 +02:00
|
|
|
|
2021-06-08 04:19:35 +02:00
|
|
|
void BCLog::Logger::LogPrintStr(const std::string& str)
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
2021-06-09 14:02:42 +02:00
|
|
|
StdLockGuard scoped_lock(m_cs);
|
2019-10-10 13:25:08 +02:00
|
|
|
std::string str_prefixed = LogEscapeMessage(str);
|
2021-06-24 19:54:20 +02:00
|
|
|
|
|
|
|
if (m_log_threadnames && m_started_new_line) {
|
|
|
|
// 16 chars total, "dash-" is 5 of them and another 1 is a NUL terminator
|
|
|
|
str_prefixed.insert(0, "[" + strprintf("%10s", util::ThreadGetInternalName()) + "] ");
|
|
|
|
}
|
|
|
|
|
|
|
|
str_prefixed = LogTimestampStr(str_prefixed);
|
2018-04-20 07:02:12 +02:00
|
|
|
|
2021-06-24 19:54:20 +02:00
|
|
|
m_started_new_line = !str.empty() && str[str.size()-1] == '\n';
|
2018-04-20 07:02:12 +02:00
|
|
|
|
2021-06-08 04:19:35 +02:00
|
|
|
if (m_buffering) {
|
|
|
|
// buffer if we haven't started logging yet
|
2021-06-24 19:54:20 +02:00
|
|
|
m_msgs_before_open.push_back(str_prefixed);
|
2021-06-08 04:19:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
if (m_print_to_console) {
|
2018-04-20 07:02:12 +02:00
|
|
|
// print to console
|
2021-06-24 19:54:20 +02:00
|
|
|
fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
|
2018-04-20 07:02:12 +02:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2022-02-26 06:07:36 +01:00
|
|
|
for (const auto& cb : m_print_callbacks) {
|
|
|
|
cb(str_prefixed);
|
|
|
|
}
|
2018-05-01 04:13:30 +02:00
|
|
|
if (m_print_to_file) {
|
2021-06-08 04:19:35 +02:00
|
|
|
assert(m_fileout != nullptr);
|
|
|
|
|
|
|
|
// reopen the log file, if requested
|
|
|
|
if (m_reopen_file) {
|
|
|
|
m_reopen_file = false;
|
|
|
|
FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
|
|
|
|
if (new_fileout) {
|
|
|
|
setbuf(new_fileout, nullptr); // unbuffered
|
|
|
|
fclose(m_fileout);
|
|
|
|
m_fileout = new_fileout;
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 19:54:20 +02:00
|
|
|
FileWriteStr(str_prefixed, m_fileout);
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
void BCLog::Logger::ShrinkDebugFile()
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
|
|
|
// Amount of debug.log to save at end when shrinking (must fit in memory)
|
|
|
|
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
assert(!m_file_path.empty());
|
|
|
|
|
2018-04-20 07:02:12 +02:00
|
|
|
// Scroll debug.log if it's getting too big
|
2018-05-01 04:13:30 +02:00
|
|
|
FILE* file = fsbridge::fopen(m_file_path, "r");
|
2018-04-17 17:07:19 +02:00
|
|
|
|
|
|
|
// Special files (e.g. device nodes) may not have a size.
|
|
|
|
size_t log_size = 0;
|
|
|
|
try {
|
2018-05-01 04:13:30 +02:00
|
|
|
log_size = fs::file_size(m_file_path);
|
2018-08-15 17:07:34 +02:00
|
|
|
} catch (const fs::filesystem_error&) {}
|
2018-04-17 17:07:19 +02:00
|
|
|
|
2018-04-20 07:02:12 +02:00
|
|
|
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
|
|
|
|
// trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
|
2018-04-17 17:07:19 +02:00
|
|
|
if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
|
|
|
// Restart the file with some of the end
|
|
|
|
std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
|
2018-05-07 13:39:13 +02:00
|
|
|
if (fseek(file, -((long)vch.size()), SEEK_END)) {
|
|
|
|
LogPrintf("Failed to shrink debug log file: fseek(...) failed\n");
|
|
|
|
fclose(file);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-20 07:02:12 +02:00
|
|
|
int nBytes = fread(vch.data(), 1, vch.size(), file);
|
|
|
|
fclose(file);
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
file = fsbridge::fopen(m_file_path, "w");
|
2018-04-20 07:02:12 +02:00
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
fwrite(vch.data(), 1, nBytes, file);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (file != nullptr)
|
|
|
|
fclose(file);
|
2020-07-18 21:22:44 +02:00
|
|
|
}
|