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.
|
|
|
|
|
2024-08-06 19:40:56 +02:00
|
|
|
#include <fs.h>
|
2018-04-20 07:02:12 +02:00
|
|
|
#include <logging.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/threadnames.h>
|
Merge #19809: log: Prefix log messages with function name and source code location if -logsourcelocations is set
b4511e2e2ed1a6077ae6826a9ee6b7a311293d08 log: Prefix log messages with function name if -logsourcelocations is set (practicalswift)
Pull request description:
Prefix log messages with function name if `-logfunctionnames` is set.
Yes, exactly like `-logthreadnames` but for function names instead of thread names :)
This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.
For me it beats the ordinary cycle of 1.) try to figure out a regexp matching the static part of the dynamic log message, 2.) `git grep -E 'Using .* MiB out of .* requested for signature cache'`, 3.) `mcedit filename.cpp` (`openemacs filename.cpp` works too!) and 4.) search for log message and scroll up to find the function name :)
Without any logging parameters:
```
$ src/bitcoind -regtest
2020-08-25T03:29:04Z Using RdRand as an additional entropy source
2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2020-08-25T03:29:04Z Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
2020-08-25T03:29:04Z block tree size = 1
2020-08-25T03:29:04Z nBestHeight = 0
2020-08-25T03:29:04Z Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2020-08-25T03:29:04Z 0 addresses found from DNS seeds
```
With `-logthreadnames` and `-logfunctionnames`:
```
$ src/bitcoind -regtest -logthreadnames -logfunctionnames
2020-08-25T03:29:04Z [init] [ReportHardwareRand] Using RdRand as an additional entropy source
2020-08-25T03:29:04Z [init] [InitSignatureCache] Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2020-08-25T03:29:04Z [init] [InitScriptExecutionCache] Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2020-08-25T03:29:04Z [init] [LoadChainTip] Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
2020-08-25T03:29:04Z [init] [AppInitMain] block tree size = 1
2020-08-25T03:29:04Z [init] [AppInitMain] nBestHeight = 0
2020-08-25T03:29:04Z [loadblk] [LoadMempool] Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2020-08-25T03:29:04Z [dnsseed] [ThreadDNSAddressSeed] 0 addresses found from DNS seeds
```
ACKs for top commit:
laanwj:
Code review ACK b4511e2e2ed1a6077ae6826a9ee6b7a311293d08
MarcoFalke:
review ACK b4511e2e2ed1a6077ae6826a9ee6b7a311293d08 🌃
Tree-SHA512: d100f5364630c323f31d275259864c597f7725e462d5f4bdedcc7033ea616d7fc0d16ef1b2af557e692f4deea73c6773ccfc681589e7bf6ba970b9ec169040c7
2021-02-18 14:20:42 +01:00
|
|
|
#include <util/string.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#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;
|
2020-06-07 18:30:58 +02:00
|
|
|
if (!GetLogCategory(flag, str)) 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"},
|
2021-04-27 10:17:19 +02:00
|
|
|
{BCLog::IPC, "ipc"},
|
2024-11-18 11:47:10 +01:00
|
|
|
#ifdef DEBUG_LOCKCONTENTION
|
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"},
|
2024-11-18 11:47:10 +01:00
|
|
|
#endif
|
2024-11-18 11:45:27 +01:00
|
|
|
{BCLog::BLOCKSTORE, "blockstorage"},
|
2024-10-20 11:22:25 +02:00
|
|
|
{BCLog::TXRECONCILIATION, "txreconciliation"},
|
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
|
|
|
{
|
2024-11-17 18:14:18 +01:00
|
|
|
if (str.empty()) {
|
2018-05-01 04:13:30 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-11-17 18:14:18 +01:00
|
|
|
std::string LogLevelToStr(BCLog::Level level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case BCLog::Level::None:
|
|
|
|
return "none";
|
|
|
|
case BCLog::Level::Debug:
|
|
|
|
return "debug";
|
|
|
|
case BCLog::Level::Info:
|
|
|
|
return "info";
|
|
|
|
case BCLog::Level::Warning:
|
|
|
|
return "warning";
|
|
|
|
case BCLog::Level::Error:
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LogCategoryToStr(BCLog::LogFlags category)
|
|
|
|
{
|
|
|
|
// Each log category string representation should sync with LogCategories
|
|
|
|
switch (category) {
|
|
|
|
case BCLog::LogFlags::NONE:
|
|
|
|
return "none";
|
|
|
|
case BCLog::LogFlags::NET:
|
|
|
|
return "net";
|
|
|
|
case BCLog::LogFlags::TOR:
|
|
|
|
return "tor";
|
|
|
|
case BCLog::LogFlags::MEMPOOL:
|
|
|
|
return "mempool";
|
|
|
|
case BCLog::LogFlags::HTTP:
|
|
|
|
return "http";
|
|
|
|
case BCLog::LogFlags::BENCHMARK:
|
|
|
|
return "bench";
|
|
|
|
case BCLog::LogFlags::ZMQ:
|
|
|
|
return "zmq";
|
|
|
|
case BCLog::LogFlags::WALLETDB:
|
|
|
|
return "walletdb";
|
|
|
|
case BCLog::LogFlags::RPC:
|
|
|
|
return "rpc";
|
|
|
|
case BCLog::LogFlags::ESTIMATEFEE:
|
|
|
|
return "estimatefee";
|
|
|
|
case BCLog::LogFlags::ADDRMAN:
|
|
|
|
return "addrman";
|
|
|
|
case BCLog::LogFlags::SELECTCOINS:
|
|
|
|
return "selectcoins";
|
|
|
|
case BCLog::LogFlags::REINDEX:
|
|
|
|
return "reindex";
|
|
|
|
case BCLog::LogFlags::CMPCTBLOCK:
|
|
|
|
return "cmpctblock";
|
|
|
|
case BCLog::LogFlags::RANDOM:
|
|
|
|
return "rand";
|
|
|
|
case BCLog::LogFlags::PRUNE:
|
|
|
|
return "prune";
|
|
|
|
case BCLog::LogFlags::PROXY:
|
|
|
|
return "proxy";
|
|
|
|
case BCLog::LogFlags::MEMPOOLREJ:
|
|
|
|
return "mempoolrej";
|
|
|
|
case BCLog::LogFlags::LIBEVENT:
|
|
|
|
return "libevent";
|
|
|
|
case BCLog::LogFlags::COINDB:
|
|
|
|
return "coindb";
|
|
|
|
case BCLog::LogFlags::QT:
|
|
|
|
return "qt";
|
|
|
|
case BCLog::LogFlags::LEVELDB:
|
|
|
|
return "leveldb";
|
|
|
|
case BCLog::LogFlags::VALIDATION:
|
|
|
|
return "validation";
|
|
|
|
case BCLog::LogFlags::I2P:
|
|
|
|
return "i2p";
|
|
|
|
case BCLog::LogFlags::IPC:
|
|
|
|
return "ipc";
|
|
|
|
#ifdef DEBUG_LOCKCONTENTION
|
|
|
|
case BCLog::LogFlags::LOCK:
|
|
|
|
return "lock";
|
|
|
|
#endif
|
|
|
|
case BCLog::LogFlags::BLOCKSTORE:
|
|
|
|
return "blockstorage";
|
|
|
|
case BCLog::LogFlags::TXRECONCILIATION:
|
|
|
|
return "txreconciliation";
|
|
|
|
/* Start Dash */
|
|
|
|
case BCLog::LogFlags::CHAINLOCKS:
|
|
|
|
return "chainlocks";
|
|
|
|
case BCLog::LogFlags::GOBJECT:
|
|
|
|
return "gobject";
|
|
|
|
case BCLog::LogFlags::INSTANTSEND:
|
|
|
|
return "instantsend";
|
|
|
|
case BCLog::LogFlags::LLMQ:
|
|
|
|
return "llmq";
|
|
|
|
case BCLog::LogFlags::LLMQ_DKG:
|
|
|
|
return "llmq-dkg";
|
|
|
|
case BCLog::LogFlags::LLMQ_SIGS:
|
|
|
|
return "llmq-sigs";
|
|
|
|
case BCLog::LogFlags::MNPAYMENTS:
|
|
|
|
return "mnpayments";
|
|
|
|
case BCLog::LogFlags::MNSYNC:
|
|
|
|
return "mnsync";
|
|
|
|
case BCLog::LogFlags::COINJOIN:
|
|
|
|
return "coinjoin";
|
|
|
|
case BCLog::LogFlags::SPORK:
|
|
|
|
return "spork";
|
|
|
|
case BCLog::LogFlags::NETCONN:
|
|
|
|
return "netconn";
|
|
|
|
case BCLog::LogFlags::CREDITPOOL:
|
|
|
|
return "creditpool";
|
|
|
|
case BCLog::LogFlags::EHF:
|
|
|
|
return "ehf";
|
|
|
|
case BCLog::LogFlags::DASH:
|
|
|
|
return "dash";
|
|
|
|
case BCLog::LogFlags::NET_NETCONN:
|
|
|
|
return "net|netconn";
|
|
|
|
/* End Dash */
|
|
|
|
case BCLog::LogFlags::ALL:
|
|
|
|
return "all";
|
|
|
|
}
|
|
|
|
assert(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
|
|
|
|
2024-11-17 18:14:18 +01:00
|
|
|
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags category, const BCLog::Level level)
|
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
|
|
|
|
2024-11-17 18:14:18 +01:00
|
|
|
if ((category != LogFlags::NONE || level != Level::None) && m_started_new_line) {
|
|
|
|
std::string s{"["};
|
|
|
|
|
|
|
|
if (category != LogFlags::NONE) {
|
|
|
|
s += LogCategoryToStr(category);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category != LogFlags::NONE && level != Level::None) {
|
|
|
|
// Only add separator if both flag and level are not NONE
|
|
|
|
s += ":";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level != Level::None) {
|
|
|
|
s += LogLevelToStr(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
s += "] ";
|
|
|
|
str_prefixed.insert(0, s);
|
|
|
|
}
|
|
|
|
|
Merge #19809: log: Prefix log messages with function name and source code location if -logsourcelocations is set
b4511e2e2ed1a6077ae6826a9ee6b7a311293d08 log: Prefix log messages with function name if -logsourcelocations is set (practicalswift)
Pull request description:
Prefix log messages with function name if `-logfunctionnames` is set.
Yes, exactly like `-logthreadnames` but for function names instead of thread names :)
This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.
For me it beats the ordinary cycle of 1.) try to figure out a regexp matching the static part of the dynamic log message, 2.) `git grep -E 'Using .* MiB out of .* requested for signature cache'`, 3.) `mcedit filename.cpp` (`openemacs filename.cpp` works too!) and 4.) search for log message and scroll up to find the function name :)
Without any logging parameters:
```
$ src/bitcoind -regtest
2020-08-25T03:29:04Z Using RdRand as an additional entropy source
2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2020-08-25T03:29:04Z Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
2020-08-25T03:29:04Z block tree size = 1
2020-08-25T03:29:04Z nBestHeight = 0
2020-08-25T03:29:04Z Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2020-08-25T03:29:04Z 0 addresses found from DNS seeds
```
With `-logthreadnames` and `-logfunctionnames`:
```
$ src/bitcoind -regtest -logthreadnames -logfunctionnames
2020-08-25T03:29:04Z [init] [ReportHardwareRand] Using RdRand as an additional entropy source
2020-08-25T03:29:04Z [init] [InitSignatureCache] Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2020-08-25T03:29:04Z [init] [InitScriptExecutionCache] Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2020-08-25T03:29:04Z [init] [LoadChainTip] Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
2020-08-25T03:29:04Z [init] [AppInitMain] block tree size = 1
2020-08-25T03:29:04Z [init] [AppInitMain] nBestHeight = 0
2020-08-25T03:29:04Z [loadblk] [LoadMempool] Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2020-08-25T03:29:04Z [dnsseed] [ThreadDNSAddressSeed] 0 addresses found from DNS seeds
```
ACKs for top commit:
laanwj:
Code review ACK b4511e2e2ed1a6077ae6826a9ee6b7a311293d08
MarcoFalke:
review ACK b4511e2e2ed1a6077ae6826a9ee6b7a311293d08 🌃
Tree-SHA512: d100f5364630c323f31d275259864c597f7725e462d5f4bdedcc7033ea616d7fc0d16ef1b2af557e692f4deea73c6773ccfc681589e7bf6ba970b9ec169040c7
2021-02-18 14:20:42 +01:00
|
|
|
if (m_log_sourcelocations && m_started_new_line) {
|
|
|
|
str_prefixed.insert(0, "[" + RemovePrefix(source_file, "./") + ":" + ToString(source_line) + "] [" + logging_function + "] ");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|