Bitcoin Core 28.99.0
P2P Digital Currency
logging.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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#include <logging.h>
7#include <memusage.h>
8#include <util/check.h>
9#include <util/fs.h>
10#include <util/string.h>
11#include <util/threadnames.h>
12#include <util/time.h>
13
14#include <array>
15#include <map>
16#include <optional>
17
18using util::Join;
20
21const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
23
25{
41 static BCLog::Logger* g_logger{new BCLog::Logger()};
42 return *g_logger;
43}
44
46
47static int FileWriteStr(std::string_view str, FILE *fp)
48{
49 return fwrite(str.data(), 1, str.size(), fp);
50}
51
53{
54 StdLockGuard scoped_lock(m_cs);
55
56 assert(m_buffering);
57 assert(m_fileout == nullptr);
58
59 if (m_print_to_file) {
60 assert(!m_file_path.empty());
61 m_fileout = fsbridge::fopen(m_file_path, "a");
62 if (!m_fileout) {
63 return false;
64 }
65
66 setbuf(m_fileout, nullptr); // unbuffered
67
68 // Add newlines to the logfile to distinguish this execution from the
69 // last one.
70 FileWriteStr("\n\n\n\n\n", m_fileout);
71 }
72
73 // dump buffered messages from before we opened the log
74 m_buffering = false;
75 if (m_buffer_lines_discarded > 0) {
76 LogPrintStr_(strprintf("Early logging buffer overflowed, %d log lines discarded.\n", m_buffer_lines_discarded), __func__, __FILE__, __LINE__, BCLog::ALL, Level::Info);
77 }
78 while (!m_msgs_before_open.empty()) {
79 const auto& buflog = m_msgs_before_open.front();
80 std::string s{buflog.str};
81 FormatLogStrInPlace(s, buflog.category, buflog.level, buflog.source_file, buflog.source_line, buflog.logging_function, buflog.threadname, buflog.now, buflog.mocktime);
82 m_msgs_before_open.pop_front();
83
84 if (m_print_to_file) FileWriteStr(s, m_fileout);
85 if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
86 for (const auto& cb : m_print_callbacks) {
87 cb(s);
88 }
89 }
90 m_cur_buffer_memusage = 0;
91 if (m_print_to_console) fflush(stdout);
92
93 return true;
94}
95
97{
98 StdLockGuard scoped_lock(m_cs);
99 m_buffering = true;
100 if (m_fileout != nullptr) fclose(m_fileout);
101 m_fileout = nullptr;
102 m_print_callbacks.clear();
103 m_max_buffer_memusage = DEFAULT_MAX_LOG_BUFFER;
104 m_cur_buffer_memusage = 0;
105 m_buffer_lines_discarded = 0;
106 m_msgs_before_open.clear();
107}
108
110{
111 {
112 StdLockGuard scoped_lock(m_cs);
113 assert(m_buffering);
114 assert(m_print_callbacks.empty());
115 }
116 m_print_to_file = false;
117 m_print_to_console = false;
118 StartLogging();
119}
120
122{
123 m_categories |= flag;
124}
125
126bool BCLog::Logger::EnableCategory(std::string_view str)
127{
128 BCLog::LogFlags flag;
129 if (!GetLogCategory(flag, str)) return false;
130 EnableCategory(flag);
131 return true;
132}
133
135{
136 m_categories &= ~flag;
137}
138
139bool BCLog::Logger::DisableCategory(std::string_view str)
140{
141 BCLog::LogFlags flag;
142 if (!GetLogCategory(flag, str)) return false;
143 DisableCategory(flag);
144 return true;
145}
146
148{
149 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
150}
151
153{
154 // Log messages at Info, Warning and Error level unconditionally, so that
155 // important troubleshooting information doesn't get lost.
156 if (level >= BCLog::Level::Info) return true;
157
158 if (!WillLogCategory(category)) return false;
159
160 StdLockGuard scoped_lock(m_cs);
161 const auto it{m_category_log_levels.find(category)};
162 return level >= (it == m_category_log_levels.end() ? LogLevel() : it->second);
163}
164
166{
167 return m_categories == BCLog::NONE;
168}
169
170static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
171 {"net", BCLog::NET},
172 {"tor", BCLog::TOR},
173 {"mempool", BCLog::MEMPOOL},
174 {"http", BCLog::HTTP},
175 {"bench", BCLog::BENCH},
176 {"zmq", BCLog::ZMQ},
177 {"walletdb", BCLog::WALLETDB},
178 {"rpc", BCLog::RPC},
179 {"estimatefee", BCLog::ESTIMATEFEE},
180 {"addrman", BCLog::ADDRMAN},
181 {"selectcoins", BCLog::SELECTCOINS},
182 {"reindex", BCLog::REINDEX},
183 {"cmpctblock", BCLog::CMPCTBLOCK},
184 {"rand", BCLog::RAND},
185 {"prune", BCLog::PRUNE},
186 {"proxy", BCLog::PROXY},
187 {"mempoolrej", BCLog::MEMPOOLREJ},
188 {"libevent", BCLog::LIBEVENT},
189 {"coindb", BCLog::COINDB},
190 {"qt", BCLog::QT},
191 {"leveldb", BCLog::LEVELDB},
192 {"validation", BCLog::VALIDATION},
193 {"i2p", BCLog::I2P},
194 {"ipc", BCLog::IPC},
195#ifdef DEBUG_LOCKCONTENTION
196 {"lock", BCLog::LOCK},
197#endif
198 {"blockstorage", BCLog::BLOCKSTORAGE},
199 {"txreconciliation", BCLog::TXRECONCILIATION},
200 {"scan", BCLog::SCAN},
201 {"txpackages", BCLog::TXPACKAGES},
202};
203
204static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
205 // Swap keys and values from LOG_CATEGORIES_BY_STR.
206 [](const auto& in) {
207 std::unordered_map<BCLog::LogFlags, std::string> out;
208 for (const auto& [k, v] : in) {
209 const bool inserted{out.emplace(v, k).second};
210 assert(inserted);
211 }
212 return out;
214};
215
216bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)
217{
218 if (str.empty() || str == "1" || str == "all") {
219 flag = BCLog::ALL;
220 return true;
221 }
222 auto it = LOG_CATEGORIES_BY_STR.find(str);
223 if (it != LOG_CATEGORIES_BY_STR.end()) {
224 flag = it->second;
225 return true;
226 }
227 return false;
228}
229
231{
232 switch (level) {
234 return "trace";
236 return "debug";
238 return "info";
240 return "warning";
242 return "error";
243 }
244 assert(false);
245}
246
247static std::string LogCategoryToStr(BCLog::LogFlags category)
248{
249 if (category == BCLog::ALL) {
250 return "all";
251 }
252 auto it = LOG_CATEGORIES_BY_FLAG.find(category);
253 assert(it != LOG_CATEGORIES_BY_FLAG.end());
254 return it->second;
255}
256
257static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str)
258{
259 if (level_str == "trace") {
260 return BCLog::Level::Trace;
261 } else if (level_str == "debug") {
262 return BCLog::Level::Debug;
263 } else if (level_str == "info") {
264 return BCLog::Level::Info;
265 } else if (level_str == "warning") {
267 } else if (level_str == "error") {
268 return BCLog::Level::Error;
269 } else {
270 return std::nullopt;
271 }
272}
273
274std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
275{
276 std::vector<LogCategory> ret;
277 ret.reserve(LOG_CATEGORIES_BY_STR.size());
278 for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) {
279 ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
280 }
281 return ret;
282}
283
285static constexpr std::array<BCLog::Level, 3> LogLevelsList()
286{
288}
289
291{
292 const auto& levels = LogLevelsList();
293 return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [](BCLog::Level level) { return LogLevelToStr(level); });
294}
295
296std::string BCLog::Logger::LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
297{
298 std::string strStamped;
299
300 if (!m_log_timestamps)
301 return strStamped;
302
303 const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
304 strStamped = FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now_seconds));
305 if (m_log_time_micros && !strStamped.empty()) {
306 strStamped.pop_back();
307 strStamped += strprintf(".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
308 }
309 if (mocktime > 0s) {
310 strStamped += " (mocktime: " + FormatISO8601DateTime(count_seconds(mocktime)) + ")";
311 }
312 strStamped += ' ';
313
314 return strStamped;
315}
316
317namespace BCLog {
325 std::string LogEscapeMessage(std::string_view str) {
326 std::string ret;
327 for (char ch_in : str) {
328 uint8_t ch = (uint8_t)ch_in;
329 if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
330 ret += ch_in;
331 } else {
332 ret += strprintf("\\x%02x", ch);
333 }
334 }
335 return ret;
336 }
337} // namespace BCLog
338
340{
341 if (category == LogFlags::NONE) category = LogFlags::ALL;
342
343 const bool has_category{m_always_print_category_level || category != LogFlags::ALL};
344
345 // If there is no category, Info is implied
346 if (!has_category && level == Level::Info) return {};
347
348 std::string s{"["};
349 if (has_category) {
350 s += LogCategoryToStr(category);
351 }
352
353 if (m_always_print_category_level || !has_category || level != Level::Debug) {
354 // If there is a category, Debug is implied, so don't add the level
355
356 // Only add separator if we have a category
357 if (has_category) s += ":";
358 s += Logger::LogLevelToStr(level);
359 }
360
361 s += "] ";
362 return s;
363}
364
365static size_t MemUsage(const BCLog::Logger::BufferedLog& buflog)
366{
367 return buflog.str.size() + buflog.logging_function.size() + buflog.source_file.size() + buflog.threadname.size() + memusage::MallocUsage(sizeof(memusage::list_node<BCLog::Logger::BufferedLog>));
368}
369
370void BCLog::Logger::FormatLogStrInPlace(std::string& str, BCLog::LogFlags category, BCLog::Level level, std::string_view source_file, int source_line, std::string_view logging_function, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
371{
372 if (!str.ends_with('\n')) str.push_back('\n');
373
374 str.insert(0, GetLogPrefix(category, level));
375
376 if (m_log_sourcelocations) {
377 str.insert(0, strprintf("[%s:%d] [%s] ", RemovePrefixView(source_file, "./"), source_line, logging_function));
378 }
379
380 if (m_log_threadnames) {
381 str.insert(0, strprintf("[%s] ", (threadname.empty() ? "unknown" : threadname)));
382 }
383
384 str.insert(0, LogTimestampStr(now, mocktime));
385}
386
387void BCLog::Logger::LogPrintStr(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
388{
389 StdLockGuard scoped_lock(m_cs);
390 return LogPrintStr_(str, logging_function, source_file, source_line, category, level);
391}
392
393void BCLog::Logger::LogPrintStr_(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
394{
395 std::string str_prefixed = LogEscapeMessage(str);
396
397 if (m_buffering) {
398 {
399 BufferedLog buf{
400 .now=SystemClock::now(),
401 .mocktime=GetMockTime(),
402 .str=str_prefixed,
403 .logging_function=std::string(logging_function),
404 .source_file=std::string(source_file),
405 .threadname=util::ThreadGetInternalName(),
406 .source_line=source_line,
407 .category=category,
408 .level=level,
409 };
410 m_cur_buffer_memusage += MemUsage(buf);
411 m_msgs_before_open.push_back(std::move(buf));
412 }
413
414 while (m_cur_buffer_memusage > m_max_buffer_memusage) {
415 if (m_msgs_before_open.empty()) {
416 m_cur_buffer_memusage = 0;
417 break;
418 }
419 m_cur_buffer_memusage -= MemUsage(m_msgs_before_open.front());
420 m_msgs_before_open.pop_front();
421 ++m_buffer_lines_discarded;
422 }
423
424 return;
425 }
426
427 FormatLogStrInPlace(str_prefixed, category, level, source_file, source_line, logging_function, util::ThreadGetInternalName(), SystemClock::now(), GetMockTime());
428
429 if (m_print_to_console) {
430 // print to console
431 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
432 fflush(stdout);
433 }
434 for (const auto& cb : m_print_callbacks) {
435 cb(str_prefixed);
436 }
437 if (m_print_to_file) {
438 assert(m_fileout != nullptr);
439
440 // reopen the log file, if requested
441 if (m_reopen_file) {
442 m_reopen_file = false;
443 FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
444 if (new_fileout) {
445 setbuf(new_fileout, nullptr); // unbuffered
446 fclose(m_fileout);
447 m_fileout = new_fileout;
448 }
449 }
450 FileWriteStr(str_prefixed, m_fileout);
451 }
452}
453
455{
456 // Amount of debug.log to save at end when shrinking (must fit in memory)
457 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
458
459 assert(!m_file_path.empty());
460
461 // Scroll debug.log if it's getting too big
462 FILE* file = fsbridge::fopen(m_file_path, "r");
463
464 // Special files (e.g. device nodes) may not have a size.
465 size_t log_size = 0;
466 try {
467 log_size = fs::file_size(m_file_path);
468 } catch (const fs::filesystem_error&) {}
469
470 // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
471 // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
472 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
473 {
474 // Restart the file with some of the end
475 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
476 if (fseek(file, -((long)vch.size()), SEEK_END)) {
477 LogPrintf("Failed to shrink debug log file: fseek(...) failed\n");
478 fclose(file);
479 return;
480 }
481 int nBytes = fread(vch.data(), 1, vch.size(), file);
482 fclose(file);
483
484 file = fsbridge::fopen(m_file_path, "w");
485 if (file)
486 {
487 fwrite(vch.data(), 1, nBytes, file);
488 fclose(file);
489 }
490 }
491 else if (file != nullptr)
492 fclose(file);
493}
494
495bool BCLog::Logger::SetLogLevel(std::string_view level_str)
496{
497 const auto level = GetLogLevel(level_str);
498 if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
499 m_log_level = level.value();
500 return true;
501}
502
503bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
504{
505 BCLog::LogFlags flag;
506 if (!GetLogCategory(flag, category_str)) return false;
507
508 const auto level = GetLogLevel(level_str);
509 if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
510
511 StdLockGuard scoped_lock(m_cs);
512 m_category_log_levels[flag] = level.value();
513 return true;
514}
int ret
static std::string LogLevelToStr(BCLog::Level level)
Returns the string representation of a log level.
Definition: logging.cpp:230
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:147
std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
Definition: logging.cpp:296
bool DefaultShrinkDebugFile() const
Definition: logging.cpp:165
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition: logging.h:190
void SetLogLevel(Level level)
Definition: logging.h:198
bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition: logging.cpp:152
fs::path m_file_path
Definition: logging.h:141
void DisableLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Disable logging This offers a slight speedup and slightly smaller memory usage compared to leaving th...
Definition: logging.cpp:109
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
Definition: logging.cpp:274
bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Start logging (and flush all buffered messages)
Definition: logging.cpp:52
void EnableCategory(LogFlags flag)
Definition: logging.cpp:121
void FormatLogStrInPlace(std::string &str, LogFlags category, Level level, std::string_view source_file, int source_line, std::string_view logging_function, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
Definition: logging.cpp:370
std::string GetLogPrefix(LogFlags category, Level level) const
Definition: logging.cpp:339
void LogPrintStr(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Send a string to the log output.
Definition: logging.cpp:387
void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Only for testing.
Definition: logging.cpp:96
std::string LogLevelsString() const
Returns a string with all user-selectable log levels.
Definition: logging.cpp:290
void ShrinkDebugFile()
Definition: logging.cpp:454
bool m_print_to_file
Definition: logging.h:133
bool m_print_to_console
Definition: logging.h:132
StdMutex m_cs
Definition: logging.h:99
void DisableCategory(LogFlags flag)
Definition: logging.cpp:134
void LogPrintStr_(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level) EXCLUSIVE_LOCKS_REQUIRED(m_cs)
Send a string to the log output (internal)
Definition: logging.cpp:393
static constexpr std::array< BCLog::Level, 3 > LogLevelsList()
Log severity levels that can be selected by the user.
Definition: logging.cpp:285
static int FileWriteStr(std::string_view str, FILE *fp)
Definition: logging.cpp:47
static std::string LogCategoryToStr(BCLog::LogFlags category)
Definition: logging.cpp:247
bool GetLogCategory(BCLog::LogFlags &flag, std::string_view str)
Return true if str parses as a log category and set the flag.
Definition: logging.cpp:216
static size_t MemUsage(const BCLog::Logger::BufferedLog &buflog)
Definition: logging.cpp:365
static const std::map< std::string, BCLog::LogFlags, std::less<> > LOG_CATEGORIES_BY_STR
Definition: logging.cpp:170
BCLog::Logger & LogInstance()
Definition: logging.cpp:24
bool fLogIPs
Definition: logging.cpp:45
static const std::unordered_map< BCLog::LogFlags, std::string > LOG_CATEGORIES_BY_FLAG
Definition: logging.cpp:204
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:21
static std::optional< BCLog::Level > GetLogLevel(std::string_view level_str)
Definition: logging.cpp:257
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL
Definition: logging.cpp:22
static const bool DEFAULT_LOGIPS
Definition: logging.h:25
#define LogPrintf(...)
Definition: logging.h:266
Level
Definition: logging.h:76
std::string LogEscapeMessage(std::string_view str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
Definition: logging.cpp:325
constexpr size_t DEFAULT_MAX_LOG_BUFFER
Definition: logging.h:84
LogFlags
Definition: logging.h:41
@ ESTIMATEFEE
Definition: logging.h:51
@ TXRECONCILIATION
Definition: logging.h:71
@ RAND
Definition: logging.h:56
@ BLOCKSTORAGE
Definition: logging.h:70
@ COINDB
Definition: logging.h:61
@ REINDEX
Definition: logging.h:54
@ TXPACKAGES
Definition: logging.h:73
@ WALLETDB
Definition: logging.h:49
@ SCAN
Definition: logging.h:72
@ ADDRMAN
Definition: logging.h:52
@ ALL
Definition: logging.h:74
@ RPC
Definition: logging.h:50
@ HTTP
Definition: logging.h:46
@ LEVELDB
Definition: logging.h:63
@ NONE
Definition: logging.h:42
@ VALIDATION
Definition: logging.h:64
@ MEMPOOLREJ
Definition: logging.h:59
@ PRUNE
Definition: logging.h:57
@ TOR
Definition: logging.h:44
@ LIBEVENT
Definition: logging.h:60
@ CMPCTBLOCK
Definition: logging.h:55
@ PROXY
Definition: logging.h:58
@ ZMQ
Definition: logging.h:48
@ IPC
Definition: logging.h:66
@ MEMPOOL
Definition: logging.h:45
@ SELECTCOINS
Definition: logging.h:53
@ I2P
Definition: logging.h:65
@ BENCH
Definition: logging.h:47
@ NET
Definition: logging.h:43
@ QT
Definition: logging.h:62
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
bool StartLogging(const ArgsManager &args)
Definition: common.cpp:105
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:52
std::string ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:47
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:169
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:192
std::string threadname
Definition: logging.h:92
std::string source_file
Definition: logging.h:92
SystemClock::time_point now
Definition: logging.h:90
std::string logging_function
Definition: logging.h:92
std::string category
Definition: logging.h:35
#define LOCK(cs)
Definition: sync.h:257
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:42
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:49
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:56
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165
assert(!tx.IsCoinBase())