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.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_LOGGING_H
|
|
|
|
#define BITCOIN_LOGGING_H
|
|
|
|
|
|
|
|
#include <fs.h>
|
|
|
|
#include <tinyformat.h>
|
2021-06-09 14:06:31 +02:00
|
|
|
#include <threadsafety.h>
|
2020-04-27 01:57:33 +02:00
|
|
|
#include <util/string.h>
|
2018-04-20 07:02:12 +02:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <cstdint>
|
2018-05-01 04:13:30 +02:00
|
|
|
#include <list>
|
|
|
|
#include <mutex>
|
2018-04-20 07:02:12 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
static const bool DEFAULT_LOGTIMEMICROS = false;
|
|
|
|
static const bool DEFAULT_LOGIPS = false;
|
|
|
|
static const bool DEFAULT_LOGTIMESTAMPS = true;
|
|
|
|
static const bool DEFAULT_LOGTHREADNAMES = false;
|
|
|
|
extern const char * const DEFAULT_DEBUGLOGFILE;
|
|
|
|
|
|
|
|
extern bool fLogThreadNames;
|
|
|
|
extern bool fLogIPs;
|
|
|
|
|
2020-04-27 01:57:33 +02:00
|
|
|
struct LogCategory {
|
2018-04-20 07:02:12 +02:00
|
|
|
std::string category;
|
|
|
|
bool active;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace BCLog {
|
|
|
|
enum LogFlags : uint64_t {
|
|
|
|
NONE = 0,
|
|
|
|
NET = (1 << 0),
|
|
|
|
TOR = (1 << 1),
|
|
|
|
MEMPOOL = (1 << 2),
|
|
|
|
HTTP = (1 << 3),
|
|
|
|
BENCHMARK = (1 << 4),
|
|
|
|
ZMQ = (1 << 5),
|
2019-11-10 10:50:36 +01:00
|
|
|
WALLETDB = (1 << 6),
|
2018-04-20 07:02:12 +02:00
|
|
|
RPC = (1 << 7),
|
|
|
|
ESTIMATEFEE = (1 << 8),
|
|
|
|
ADDRMAN = (1 << 9),
|
|
|
|
SELECTCOINS = (1 << 10),
|
|
|
|
REINDEX = (1 << 11),
|
|
|
|
CMPCTBLOCK = (1 << 12),
|
|
|
|
RANDOM = (1 << 13),
|
|
|
|
PRUNE = (1 << 14),
|
|
|
|
PROXY = (1 << 15),
|
|
|
|
MEMPOOLREJ = (1 << 16),
|
|
|
|
LIBEVENT = (1 << 17),
|
|
|
|
COINDB = (1 << 18),
|
|
|
|
QT = (1 << 19),
|
|
|
|
LEVELDB = (1 << 20),
|
2020-01-09 20:37:09 +01:00
|
|
|
VALIDATION = (1 << 21),
|
2020-11-18 17:13:27 +01:00
|
|
|
I2P = (1 << 22),
|
2018-04-20 07:02:12 +02:00
|
|
|
|
|
|
|
//Start Dash
|
|
|
|
CHAINLOCKS = ((uint64_t)1 << 32),
|
|
|
|
GOBJECT = ((uint64_t)1 << 33),
|
|
|
|
INSTANTSEND = ((uint64_t)1 << 34),
|
|
|
|
LLMQ = ((uint64_t)1 << 36),
|
|
|
|
LLMQ_DKG = ((uint64_t)1 << 37),
|
|
|
|
LLMQ_SIGS = ((uint64_t)1 << 38),
|
|
|
|
MNPAYMENTS = ((uint64_t)1 << 39),
|
|
|
|
MNSYNC = ((uint64_t)1 << 40),
|
2021-03-17 23:36:11 +01:00
|
|
|
COINJOIN = ((uint64_t)1 << 41),
|
2018-04-20 07:02:12 +02:00
|
|
|
SPORK = ((uint64_t)1 << 42),
|
|
|
|
NETCONN = ((uint64_t)1 << 43),
|
2023-11-06 16:26:36 +01:00
|
|
|
EHF = ((uint64_t)1 << 44),
|
|
|
|
CREDITPOOL = ((uint64_t)1 << 45),
|
2018-05-01 04:13:30 +02:00
|
|
|
|
2021-12-22 10:53:11 +01:00
|
|
|
DASH = CHAINLOCKS | GOBJECT | INSTANTSEND | LLMQ | LLMQ_DKG
|
2023-11-06 16:26:36 +01:00
|
|
|
| LLMQ_SIGS | MNPAYMENTS | MNSYNC | COINJOIN | SPORK | NETCONN
|
|
|
|
| EHF | CREDITPOOL,
|
2018-04-20 07:02:12 +02:00
|
|
|
|
|
|
|
NET_NETCONN = NET | NETCONN, // use this to have something logged in NET and NETCONN as well
|
2018-05-01 04:13:30 +02:00
|
|
|
//End Dash
|
2018-04-20 07:02:12 +02:00
|
|
|
|
|
|
|
ALL = ~(uint64_t)0,
|
|
|
|
};
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
class Logger
|
|
|
|
{
|
|
|
|
private:
|
2021-06-09 14:02:42 +02:00
|
|
|
mutable StdMutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected
|
2021-06-09 14:06:31 +02:00
|
|
|
|
|
|
|
FILE* m_fileout GUARDED_BY(m_cs) = nullptr;
|
|
|
|
std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
|
|
|
|
bool m_buffering GUARDED_BY(m_cs) = true; //!< Buffer messages before logging can be started.
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* m_started_new_line is a state variable that will suppress printing of
|
|
|
|
* the timestamp when multiple calls are made that don't end in a
|
|
|
|
* newline.
|
|
|
|
*/
|
|
|
|
std::atomic_bool m_started_new_line{true};
|
|
|
|
|
|
|
|
/** Log categories bitfield. */
|
|
|
|
std::atomic<uint64_t> m_categories{0};
|
|
|
|
|
|
|
|
std::string LogTimestampStr(const std::string& str);
|
|
|
|
std::string LogThreadNameStr(const std::string &str);
|
|
|
|
|
2022-02-26 06:07:36 +01:00
|
|
|
/** Slots that connect to the print signal */
|
|
|
|
std::list<std::function<void(const std::string&)>> m_print_callbacks /* GUARDED_BY(m_cs) */ {};
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
public:
|
|
|
|
bool m_print_to_console = false;
|
|
|
|
bool m_print_to_file = false;
|
|
|
|
|
|
|
|
bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
|
|
|
|
bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
|
2021-06-24 19:54:20 +02:00
|
|
|
bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
fs::path m_file_path;
|
|
|
|
std::atomic<bool> m_reopen_file{false};
|
|
|
|
|
|
|
|
/** Send a string to the log output */
|
2021-06-08 04:19:35 +02:00
|
|
|
void LogPrintStr(const std::string& str);
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
/** Returns whether logs will be written to any output */
|
2021-06-08 04:19:35 +02:00
|
|
|
bool Enabled() const
|
|
|
|
{
|
2021-06-09 14:02:42 +02:00
|
|
|
StdLockGuard scoped_lock(m_cs);
|
2022-02-26 06:07:36 +01:00
|
|
|
return m_buffering || m_print_to_console || m_print_to_file || !m_print_callbacks.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Connect a slot to the print signal and return the connection */
|
|
|
|
std::list<std::function<void(const std::string&)>>::iterator PushBackCallback(std::function<void(const std::string&)> fun)
|
|
|
|
{
|
|
|
|
StdLockGuard scoped_lock(m_cs);
|
|
|
|
m_print_callbacks.push_back(std::move(fun));
|
|
|
|
return --m_print_callbacks.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Delete a connection */
|
|
|
|
void DeleteCallback(std::list<std::function<void(const std::string&)>>::iterator it)
|
|
|
|
{
|
|
|
|
StdLockGuard scoped_lock(m_cs);
|
|
|
|
m_print_callbacks.erase(it);
|
2021-06-08 04:19:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Start logging (and flush all buffered messages) */
|
|
|
|
bool StartLogging();
|
2019-06-25 12:13:08 +02:00
|
|
|
/** Only for testing */
|
|
|
|
void DisconnectTestLogger();
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
void ShrinkDebugFile();
|
|
|
|
|
|
|
|
uint64_t GetCategoryMask() const { return m_categories.load(); }
|
|
|
|
|
|
|
|
void EnableCategory(LogFlags flag);
|
|
|
|
bool EnableCategory(const std::string& str);
|
|
|
|
void DisableCategory(LogFlags flag);
|
|
|
|
bool DisableCategory(const std::string& str);
|
|
|
|
|
|
|
|
bool WillLogCategory(LogFlags category) const;
|
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
|
|
|
/** Returns a vector of the log categories in alphabetical order. */
|
2023-09-28 14:47:45 +02:00
|
|
|
std::vector<LogCategory> LogCategoriesList(bool enabled_only = false) const;
|
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
|
|
|
/** Returns a string with the log categories in alphabetical order. */
|
2023-09-28 14:47:45 +02:00
|
|
|
std::string LogCategoriesString(bool enabled_only = false) const
|
2020-04-27 01:57:33 +02:00
|
|
|
{
|
2023-09-28 14:47:45 +02:00
|
|
|
return Join(LogCategoriesList(enabled_only), ", ", [&](const LogCategory& i) { return i.category; });
|
2020-04-27 01:57:33 +02:00
|
|
|
};
|
2018-05-01 04:13:30 +02:00
|
|
|
|
|
|
|
bool DefaultShrinkDebugFile() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace BCLog
|
|
|
|
|
2019-02-04 20:26:02 +01:00
|
|
|
BCLog::Logger& LogInstance();
|
2018-05-01 04:13:30 +02:00
|
|
|
|
2018-04-20 07:02:12 +02:00
|
|
|
/** Return true if log accepts specified category */
|
2018-05-01 04:13:30 +02:00
|
|
|
static inline bool LogAcceptCategory(BCLog::LogFlags category)
|
2018-04-20 07:02:12 +02:00
|
|
|
{
|
2019-02-04 20:26:02 +01:00
|
|
|
return LogInstance().WillLogCategory(category);
|
2018-04-20 07:02:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 04:13:30 +02:00
|
|
|
/** Return true if str parses as a log category and set the flag */
|
|
|
|
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
|
2018-04-20 07:02:12 +02:00
|
|
|
|
|
|
|
/** Formats a string without throwing exceptions. Instead, it'll return an error string instead of formatted string. */
|
|
|
|
template<typename... Args>
|
|
|
|
std::string SafeStringFormat(const std::string& fmt, const Args&... args)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return tinyformat::format(fmt, args...);
|
|
|
|
} catch (std::runtime_error& fmterr) {
|
|
|
|
std::string message = tinyformat::format("\n****TINYFORMAT ERROR****\n err=\"%s\"\n fmt=\"%s\"\n", fmterr.what(), fmt);
|
2022-03-21 18:28:10 +01:00
|
|
|
tfm::format(std::cerr, "%s", message);
|
2018-04-20 07:02:12 +02:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be conservative when using LogPrintf/error or other things which
|
|
|
|
// unconditionally log to debug.log! It should not be the case that an inbound
|
|
|
|
// peer can fill up a user's disk with debug.log entries.
|
|
|
|
|
2018-09-12 17:21:01 +02:00
|
|
|
template <typename... Args>
|
|
|
|
static inline void LogPrintf(const char* fmt, const Args&... args)
|
|
|
|
{
|
2019-02-04 20:26:02 +01:00
|
|
|
if (LogInstance().Enabled()) {
|
2018-09-12 17:21:01 +02:00
|
|
|
std::string log_msg;
|
|
|
|
try {
|
|
|
|
log_msg = tfm::format(fmt, args...);
|
|
|
|
} catch (tinyformat::format_error& fmterr) {
|
|
|
|
/* Original format string will have newline so don't add one here */
|
|
|
|
log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
|
|
|
|
}
|
2019-02-04 20:26:02 +01:00
|
|
|
LogInstance().LogPrintStr(log_msg);
|
2018-09-12 17:21:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 15:14:07 +01:00
|
|
|
// Use a macro instead of a function for conditional logging to prevent
|
|
|
|
// evaluating arguments when logging for the category is not enabled.
|
|
|
|
#define LogPrint(category, ...) \
|
|
|
|
do { \
|
|
|
|
if (LogAcceptCategory((category))) { \
|
|
|
|
LogPrintf(__VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2018-04-20 07:02:12 +02:00
|
|
|
|
|
|
|
#endif // BITCOIN_LOGGING_H
|