Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_LOGGING_H
7 : : #define BITCOIN_LOGGING_H
8 : :
9 : : #include <threadsafety.h>
10 : : #include <tinyformat.h>
11 : : #include <util/fs.h>
12 : : #include <util/string.h>
13 : :
14 : : #include <atomic>
15 : : #include <cstdint>
16 : : #include <functional>
17 : : #include <list>
18 : : #include <mutex>
19 : : #include <string>
20 : : #include <unordered_map>
21 : : #include <vector>
22 : :
23 : : static const bool DEFAULT_LOGTIMEMICROS = false;
24 : : static const bool DEFAULT_LOGIPS = false;
25 : : static const bool DEFAULT_LOGTIMESTAMPS = true;
26 : : static const bool DEFAULT_LOGTHREADNAMES = false;
27 : : static const bool DEFAULT_LOGSOURCELOCATIONS = false;
28 : : static constexpr bool DEFAULT_LOGLEVELALWAYS = false;
29 : : extern const char * const DEFAULT_DEBUGLOGFILE;
30 : :
31 : : extern bool fLogIPs;
32 : :
33 : : struct LogCategory {
34 : : std::string category;
35 : : bool active;
36 : : };
37 : :
38 : : namespace BCLog {
39 : : enum LogFlags : uint32_t {
40 : : NONE = 0,
41 : : NET = (1 << 0),
42 : : TOR = (1 << 1),
43 : : MEMPOOL = (1 << 2),
44 : : HTTP = (1 << 3),
45 : : BENCH = (1 << 4),
46 : : ZMQ = (1 << 5),
47 : : WALLETDB = (1 << 6),
48 : : RPC = (1 << 7),
49 : : ESTIMATEFEE = (1 << 8),
50 : : ADDRMAN = (1 << 9),
51 : : SELECTCOINS = (1 << 10),
52 : : REINDEX = (1 << 11),
53 : : CMPCTBLOCK = (1 << 12),
54 : : RAND = (1 << 13),
55 : : PRUNE = (1 << 14),
56 : : PROXY = (1 << 15),
57 : : MEMPOOLREJ = (1 << 16),
58 : : LIBEVENT = (1 << 17),
59 : : COINDB = (1 << 18),
60 : : QT = (1 << 19),
61 : : LEVELDB = (1 << 20),
62 : : VALIDATION = (1 << 21),
63 : : I2P = (1 << 22),
64 : : IPC = (1 << 23),
65 : : #ifdef DEBUG_LOCKCONTENTION
66 : : LOCK = (1 << 24),
67 : : #endif
68 : : BLOCKSTORAGE = (1 << 25),
69 : : TXRECONCILIATION = (1 << 26),
70 : : SCAN = (1 << 27),
71 : : TXPACKAGES = (1 << 28),
72 : : ALL = ~(uint32_t)0,
73 : : };
74 : : enum class Level {
75 : : Trace = 0, // High-volume or detailed logging for development/debugging
76 : : Debug, // Reasonably noisy logging, but still usable in production
77 : : Info, // Default
78 : : Warning,
79 : : Error,
80 : : };
81 : : constexpr auto DEFAULT_LOG_LEVEL{Level::Debug};
82 : :
83 : 12 : class Logger
84 : : {
85 : : private:
86 : : 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
87 : :
88 : 3 : FILE* m_fileout GUARDED_BY(m_cs) = nullptr;
89 : : std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
90 : 3 : bool m_buffering GUARDED_BY(m_cs) = true; //!< Buffer messages before logging can be started.
91 : :
92 : : /**
93 : : * m_started_new_line is a state variable that will suppress printing of
94 : : * the timestamp when multiple calls are made that don't end in a
95 : : * newline.
96 : : */
97 : 3 : std::atomic_bool m_started_new_line{true};
98 : :
99 : : //! Category-specific log level. Overrides `m_log_level`.
100 : : std::unordered_map<LogFlags, Level> m_category_log_levels GUARDED_BY(m_cs);
101 : :
102 : : //! If there is no category-specific log level, all logs with a severity
103 : : //! level lower than `m_log_level` will be ignored.
104 : 3 : std::atomic<Level> m_log_level{DEFAULT_LOG_LEVEL};
105 : :
106 : : /** Log categories bitfield. */
107 : 3 : std::atomic<uint32_t> m_categories{0};
108 : :
109 : : std::string LogTimestampStr(const std::string& str);
110 : :
111 : : /** Slots that connect to the print signal */
112 : 3 : std::list<std::function<void(const std::string&)>> m_print_callbacks GUARDED_BY(m_cs) {};
113 : :
114 : : public:
115 : 3 : bool m_print_to_console = false;
116 : 3 : bool m_print_to_file = false;
117 : :
118 : 3 : bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
119 : 3 : bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
120 : 3 : bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
121 : 3 : bool m_log_sourcelocations = DEFAULT_LOGSOURCELOCATIONS;
122 : 3 : bool m_always_print_category_level = DEFAULT_LOGLEVELALWAYS;
123 : :
124 : : fs::path m_file_path;
125 : 3 : std::atomic<bool> m_reopen_file{false};
126 : :
127 : : std::string GetLogPrefix(LogFlags category, Level level) const;
128 : :
129 : : /** Send a string to the log output */
130 : : void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level);
131 : :
132 : : /** Returns whether logs will be written to any output */
133 : 7422 : bool Enabled() const
134 : : {
135 : 7422 : StdLockGuard scoped_lock(m_cs);
136 [ + + + - : 7422 : return m_buffering || m_print_to_console || m_print_to_file || !m_print_callbacks.empty();
- + ]
137 : 7422 : }
138 : :
139 : : /** Connect a slot to the print signal and return the connection */
140 : 0 : std::list<std::function<void(const std::string&)>>::iterator PushBackCallback(std::function<void(const std::string&)> fun)
141 : : {
142 : 0 : StdLockGuard scoped_lock(m_cs);
143 [ # # ]: 0 : m_print_callbacks.push_back(std::move(fun));
144 : 0 : return --m_print_callbacks.end();
145 : 0 : }
146 : :
147 : : /** Delete a connection */
148 : 0 : void DeleteCallback(std::list<std::function<void(const std::string&)>>::iterator it)
149 : : {
150 : 0 : StdLockGuard scoped_lock(m_cs);
151 : 0 : m_print_callbacks.erase(it);
152 : 0 : }
153 : :
154 : : /** Start logging (and flush all buffered messages) */
155 : : bool StartLogging();
156 : : /** Only for testing */
157 : : void DisconnectTestLogger();
158 : :
159 : : void ShrinkDebugFile();
160 : :
161 : : std::unordered_map<LogFlags, Level> CategoryLevels() const
162 : : {
163 : : StdLockGuard scoped_lock(m_cs);
164 : : return m_category_log_levels;
165 : : }
166 : : void SetCategoryLogLevel(const std::unordered_map<LogFlags, Level>& levels)
167 : : {
168 : : StdLockGuard scoped_lock(m_cs);
169 : : m_category_log_levels = levels;
170 : : }
171 : : bool SetCategoryLogLevel(const std::string& category_str, const std::string& level_str);
172 : :
173 : 0 : Level LogLevel() const { return m_log_level.load(); }
174 : : void SetLogLevel(Level level) { m_log_level = level; }
175 : : bool SetLogLevel(const std::string& level);
176 : :
177 : 0 : uint32_t GetCategoryMask() const { return m_categories.load(); }
178 : :
179 : : void EnableCategory(LogFlags flag);
180 : : bool EnableCategory(const std::string& str);
181 : : void DisableCategory(LogFlags flag);
182 : : bool DisableCategory(const std::string& str);
183 : :
184 : : bool WillLogCategory(LogFlags category) const;
185 : : bool WillLogCategoryLevel(LogFlags category, Level level) const;
186 : :
187 : : /** Returns a vector of the log categories in alphabetical order. */
188 : : std::vector<LogCategory> LogCategoriesList() const;
189 : : /** Returns a string with the log categories in alphabetical order. */
190 : 8 : std::string LogCategoriesString() const
191 : : {
192 [ + - ]: 232 : return Join(LogCategoriesList(), ", ", [&](const LogCategory& i) { return i.category; });
193 : 0 : };
194 : :
195 : : //! Returns a string with all user-selectable log levels.
196 : : std::string LogLevelsString() const;
197 : :
198 : : //! Returns the string representation of a log level.
199 : : static std::string LogLevelToStr(BCLog::Level level);
200 : :
201 : : bool DefaultShrinkDebugFile() const;
202 : : };
203 : :
204 : : } // namespace BCLog
205 : :
206 : : BCLog::Logger& LogInstance();
207 : :
208 : : /** Return true if log accepts specified category, at the specified level. */
209 : 216228 : static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
210 : : {
211 : 216228 : return LogInstance().WillLogCategoryLevel(category, level);
212 : : }
213 : :
214 : : /** Return true if str parses as a log category and set the flag */
215 : : bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
216 : :
217 : : // Be conservative when using functions that
218 : : // unconditionally log to debug.log! It should not be the case that an inbound
219 : : // peer can fill up a user's disk with debug.log entries.
220 : :
221 : : template <typename... Args>
222 : 7422 : static inline void LogPrintf_(const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, const char* fmt, const Args&... args)
223 : : {
224 [ + + + - : 7422 : if (LogInstance().Enabled()) {
+ - + - +
- + - + -
+ - + - #
# + - # #
+ - # # +
- # # + -
# # # # #
# # # # #
+ - + - #
# # # + -
# # # # #
# # # # #
# # # # #
# # # + -
# # # # #
# # # # #
# # + - #
# # # # #
+ - # # #
# # # ]
225 : 6 : std::string log_msg;
226 : : try {
227 [ + - # # : 6 : log_msg = tfm::format(fmt, args...);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
228 [ # # # # : 6 : } catch (tinyformat::format_error& fmterr) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
229 : : /* Original format string will have newline so don't add one here */
230 [ # # # # : 0 : log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
231 [ # # # # : 0 : }
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
232 [ + - + - : 6 : LogInstance().LogPrintStr(log_msg, logging_function, source_file, source_line, flag, level);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
233 : 6 : }
234 : 7422 : }
235 : :
236 : : #define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
237 : :
238 : : // Log unconditionally.
239 : : #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
240 : : #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, __VA_ARGS__)
241 : : #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__)
242 : :
243 : : // Deprecated unconditional logging.
244 : : #define LogPrintf(...) LogInfo(__VA_ARGS__)
245 : : #define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::Info, __VA_ARGS__)
246 : :
247 : : // Use a macro instead of a function for conditional logging to prevent
248 : : // evaluating arguments when logging for the category is not enabled.
249 : :
250 : : // Log conditionally, prefixing the output with the passed category name and severity level.
251 : : #define LogPrintLevel(category, level, ...) \
252 : : do { \
253 : : if (LogAcceptCategory((category), (level))) { \
254 : : LogPrintLevel_(category, level, __VA_ARGS__); \
255 : : } \
256 : : } while (0)
257 : :
258 : : // Log conditionally, prefixing the output with the passed category name.
259 : : #define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
260 : : #define LogTrace(category, ...) LogPrintLevel(category, BCLog::Level::Trace, __VA_ARGS__)
261 : :
262 : : // Deprecated conditional logging
263 : : #define LogPrint(category, ...) LogDebug(category, __VA_ARGS__)
264 : :
265 : : #endif // BITCOIN_LOGGING_H
|