Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2019 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#ifndef BITCOIN_LOGGING_H
8#define BITCOIN_LOGGING_H
9
10#include <threadsafety.h>
11#include <tinyformat.h>
12#include <util/fs.h>
13#include <util/string.h>
14
15#include <atomic>
16#include <cstdint>
17#include <functional>
18#include <list>
19#include <mutex>
20#include <string>
21
22static const bool DEFAULT_LOGTIMEMICROS = false;
23static const bool DEFAULT_LOGIPS = false;
24static const bool DEFAULT_LOGTIMESTAMPS = true;
25static const bool DEFAULT_LOGTHREADNAMES = false;
26static const bool DEFAULT_LOGSOURCELOCATIONS = false;
27
28extern bool fLogIPs;
29extern const char *const DEFAULT_DEBUGLOGFILE;
30
32 std::string category;
33 bool active;
34};
35
36namespace BCLog {
37
39 NONE = 0,
40 NET = (1 << 0),
41 TOR = (1 << 1),
42 MEMPOOL = (1 << 2),
43 HTTP = (1 << 3),
44 BENCH = (1 << 4),
45 ZMQ = (1 << 5),
46 WALLETDB = (1 << 6),
47 RPC = (1 << 7),
48 ESTIMATEFEE = (1 << 8),
49 ADDRMAN = (1 << 9),
50 SELECTCOINS = (1 << 10),
51 REINDEX = (1 << 11),
52 CMPCTBLOCK = (1 << 12),
53 RAND = (1 << 13),
54 PRUNE = (1 << 14),
55 PROXY = (1 << 15),
56 MEMPOOLREJ = (1 << 16),
57 LIBEVENT = (1 << 17),
58 COINDB = (1 << 18),
59 QT = (1 << 19),
60 LEVELDB = (1 << 20),
61 VALIDATION = (1 << 21),
62 AVALANCHE = (1 << 22),
63 I2P = (1 << 23),
64 CHRONIK = (1 << 24),
66 LOCK = (1 << 25),
67#endif
68 BLOCKSTORE = (1 << 26),
69 NETDEBUG = (1 << 27),
70 TXPACKAGES = (1 << 28),
72};
73
74class Logger {
75private:
76 // Can not use Mutex from sync.h because in debug mode it would cause a
77 // deadlock when a potential deadlock was detected
78 mutable StdMutex m_cs;
79
81 std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
84
89 std::atomic_bool m_started_new_line{true};
90
94 std::atomic<uint32_t> m_categories{0};
95
96 std::string LogTimestampStr(const std::string &str);
97
99 std::list<std::function<void(const std::string &)>>
101
102public:
103 bool m_print_to_console = false;
104 bool m_print_to_file = false;
105
110
112 std::atomic<bool> m_reopen_file{false};
113
114 ~Logger();
115
117 void LogPrintStr(const std::string &str,
118 const std::string &logging_function,
119 const std::string &source_file, const int source_line);
120
122 bool Enabled() const {
125 !m_print_callbacks.empty();
126 }
127
129 std::list<std::function<void(const std::string &)>>::iterator
130 PushBackCallback(std::function<void(const std::string &)> fun) {
132 m_print_callbacks.push_back(std::move(fun));
133 return --m_print_callbacks.end();
134 }
135
138 std::list<std::function<void(const std::string &)>>::iterator it) {
140 m_print_callbacks.erase(it);
141 }
142
144 bool StartLogging();
147
148 void ShrinkDebugFile();
149
150 uint32_t GetCategoryMask() const { return m_categories.load(); }
151
152 void EnableCategory(LogFlags category);
153 bool EnableCategory(const std::string &str);
154 void DisableCategory(LogFlags category);
155 bool DisableCategory(const std::string &str);
156
158 bool WillLogCategory(LogFlags category) const;
159
161 std::vector<LogCategory> LogCategoriesList() const;
163 std::string LogCategoriesString() const {
164 return Join(LogCategoriesList(), ", ",
165 [&](const LogCategory &i) { return i.category; });
166 };
167
169 bool DefaultShrinkDebugFile() const;
170};
171
172} // namespace BCLog
173
175
177static inline bool LogAcceptCategory(BCLog::LogFlags category) {
178 return LogInstance().WillLogCategory(category);
179}
180
182bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str);
183
184// Be conservative when using LogPrintf/error or other things which
185// unconditionally log to debug.log! It should not be the case that an inbound
186// peer can fill up a user's disk with debug.log entries.
187template <typename... Args>
188static inline void
189LogPrintf_(const std::string &logging_function, const std::string &source_file,
190 const int source_line, const char *fmt, const Args &...args) {
191 if (LogInstance().Enabled()) {
192 std::string log_msg;
193 try {
194 log_msg = tfm::format(fmt, args...);
199 log_msg = "Error \"" + std::string(fmterr.what()) +
200 "\" while formatting log message: " + fmt;
201 }
204 }
205}
206
207#define LogPrintf(...) LogPrintf_(__func__, __FILE__, __LINE__, __VA_ARGS__)
208
209// Use a macro instead of a function for conditional logging to prevent
210// evaluating arguments when logging for the category is not enabled.
211#define LogPrint(category, ...) \
212 do { \
213 if (LogAcceptCategory((category))) { \
214 LogPrintf(__VA_ARGS__); \
215 } \
216 } while (0)
217
223#define LogPrintfToBeContinued LogPrintf
224#define LogPrintToBeContinued LogPrint
225
226template <typename... Args> bool error(const char *fmt, const Args &...args) {
227 LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
228 return false;
229}
230
231#endif // BITCOIN_LOGGING_H
FILE *m_fileout GUARDED_BY(m_cs)
bool m_buffering GUARDED_BY(m_cs)
Buffer messages before logging can be started.
bool WillLogCategory(LogFlags category) const
Return true if log accepts specified category.
Definition logging.cpp:351
bool Enabled() const
Returns whether logs will be written to any output.
Definition logging.h:122
std::string LogTimestampStr(const std::string &str)
Definition logging.cpp:184
void DisconnectTestLogger()
Only for testing.
Definition logging.cpp:89
std::list< std::function< void(conststd::string &)> >::iterator PushBackCallback(std::function< void(const std::string &)> fun)
Connect a slot to the print signal and return the connection.
Definition logging.h:130
std::list< std::string > m_msgs_before_open GUARDED_BY(m_cs)
std::atomic< uint32_t > m_categories
Log categories bitfield.
Definition logging.h:94
bool DefaultShrinkDebugFile() const
Default for whether ShrinkDebugFile should be run.
Definition logging.cpp:363
bool m_log_sourcelocations
Definition logging.h:109
fs::path m_file_path
Definition logging.h:111
bool m_log_time_micros
Definition logging.h:107
bool m_log_threadnames
Definition logging.h:108
std::atomic_bool m_started_new_line
m_started_new_line is a state variable that will suppress printing of the timestamp when multiple cal...
Definition logging.h:89
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
Definition logging.cpp:156
void DisableCategory(LogFlags category)
Definition logging.cpp:338
void EnableCategory(LogFlags category)
Definition logging.cpp:325
bool StartLogging()
Start logging (and flush all buffered messages)
Definition logging.cpp:44
bool m_log_timestamps
Definition logging.h:106
std::atomic< bool > m_reopen_file
Definition logging.h:112
void ShrinkDebugFile()
Definition logging.cpp:286
bool m_print_to_file
Definition logging.h:104
uint32_t GetCategoryMask() const
Definition logging.h:150
bool m_print_to_console
Definition logging.h:103
std::list< std::function< void(const std::string &)> > m_print_callbacks GUARDED_BY(m_cs)
Slots that connect to the print signal.
Definition logging.h:100
StdMutex m_cs
Definition logging.h:78
void LogPrintStr(const std::string &str, const std::string &logging_function, const std::string &source_file, const int source_line)
Send a string to the log output.
Definition logging.cpp:233
std::string LogCategoriesString() const
Returns a string with the log categories in alphabetical order.
Definition logging.h:163
void DeleteCallback(std::list< std::function< void(const std::string &)> >::iterator it)
Delete a connection.
Definition logging.h:137
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:30
bool error(const char *fmt, const Args &...args)
Definition logging.h:226
static const bool DEFAULT_LOGTIMESTAMPS
Definition logging.h:24
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition logging.h:177
static void LogPrintf_(const std::string &logging_function, const std::string &source_file, const int source_line, const char *fmt, const Args &...args)
Definition logging.h:189
static const bool DEFAULT_LOGIPS
Definition logging.h:23
static const bool DEFAULT_LOGTHREADNAMES
Definition logging.h:25
BCLog::Logger & LogInstance()
Definition logging.cpp:20
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
Definition logging.cpp:142
static const bool DEFAULT_LOGSOURCELOCATIONS
Definition logging.h:26
bool fLogIPs
Definition logging.cpp:17
static const bool DEFAULT_LOGTIMEMICROS
Definition logging.h:22
const char *const DEFAULT_DEBUGLOGFILE
Definition logging.cpp:18
#define LogPrintf(...)
Definition logging.h:207
LogFlags
Definition logging.h:38
@ ESTIMATEFEE
Definition logging.h:48
@ AVALANCHE
Definition logging.h:62
@ RAND
Definition logging.h:53
@ COINDB
Definition logging.h:58
@ REINDEX
Definition logging.h:51
@ TXPACKAGES
Definition logging.h:70
@ WALLETDB
Definition logging.h:46
@ ADDRMAN
Definition logging.h:49
@ ALL
Definition logging.h:71
@ NETDEBUG
Definition logging.h:69
@ RPC
Definition logging.h:47
@ HTTP
Definition logging.h:43
@ LEVELDB
Definition logging.h:60
@ NONE
Definition logging.h:39
@ VALIDATION
Definition logging.h:61
@ MEMPOOLREJ
Definition logging.h:56
@ PRUNE
Definition logging.h:54
@ TOR
Definition logging.h:41
@ LIBEVENT
Definition logging.h:57
@ CMPCTBLOCK
Definition logging.h:52
@ PROXY
Definition logging.h:55
@ CHRONIK
Definition logging.h:64
@ ZMQ
Definition logging.h:45
@ MEMPOOL
Definition logging.h:42
@ SELECTCOINS
Definition logging.h:50
@ I2P
Definition logging.h:63
@ BENCH
Definition logging.h:44
@ NET
Definition logging.h:40
@ QT
Definition logging.h:59
@ BLOCKSTORE
Definition logging.h:68
void format(std::ostream &out, const char *fmt, const Args &...args)
Format list of arguments to the stream according to given format string.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition string.h:63
bool active
Definition logging.h:33
std::string category
Definition logging.h:32
#define LOCK(cs)
Definition sync.h:306