Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 <cstring>
16#include <map>
17#include <optional>
18#include <utility>
19
20using util::Join;
22
23const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
24constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info};
25
27{
43 static BCLog::Logger* g_logger{new BCLog::Logger()};
44 return *g_logger;
45}
46
48
49static int FileWriteStr(std::string_view str, FILE *fp)
50{
51 return fwrite(str.data(), 1, str.size(), fp);
52}
53
55{
57
58 assert(m_buffering);
59 assert(m_fileout == nullptr);
60
61 if (m_print_to_file) {
62 assert(!m_file_path.empty());
63 m_fileout = fsbridge::fopen(m_file_path, "a");
64 if (!m_fileout) {
65 return false;
66 }
67
68 setbuf(m_fileout, nullptr); // unbuffered
69
70 // Add newlines to the logfile to distinguish this execution from the
71 // last one.
72 FileWriteStr("\n\n\n\n\n", m_fileout);
73 }
74
75 // dump buffered messages from before we opened the log
76 m_buffering = false;
77 if (m_buffer_lines_discarded > 0) {
78 LogPrint_({
79 .category = BCLog::ALL,
80 .level = Level::Info,
81 .should_ratelimit = false,
82 .source_loc = SourceLocation{__func__},
83 .message = strprintf("Early logging buffer overflowed, %d log lines discarded.", m_buffer_lines_discarded),
84 });
85 }
86 while (!m_msgs_before_open.empty()) {
87 const auto& buflog = m_msgs_before_open.front();
88 std::string s{Format(buflog)};
89 m_msgs_before_open.pop_front();
90
91 if (m_print_to_file) FileWriteStr(s, m_fileout);
92 if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
93 for (const auto& cb : m_print_callbacks) {
94 cb(s);
95 }
96 }
97 m_cur_buffer_memusage = 0;
98 if (m_print_to_console) fflush(stdout);
99
100 return true;
101}
102
104{
105 STDLOCK(m_cs);
106 m_buffering = true;
107 if (m_fileout != nullptr) fclose(m_fileout);
108 m_fileout = nullptr;
109 m_print_callbacks.clear();
110 m_max_buffer_memusage = DEFAULT_MAX_LOG_BUFFER;
111 m_cur_buffer_memusage = 0;
112 m_buffer_lines_discarded = 0;
113 m_msgs_before_open.clear();
114}
115
117{
118 {
119 STDLOCK(m_cs);
120 assert(m_buffering);
121 assert(m_print_callbacks.empty());
122 }
123 m_print_to_file = false;
124 m_print_to_console = false;
125 StartLogging();
126}
127
132
133bool BCLog::Logger::EnableCategory(std::string_view str)
134{
135 if (const auto flag{GetLogCategory(str)}) {
136 EnableCategory(*flag);
137 return true;
138 }
139 return false;
140}
141
146
147bool BCLog::Logger::DisableCategory(std::string_view str)
148{
149 if (const auto flag{GetLogCategory(str)}) {
150 DisableCategory(*flag);
151 return true;
152 }
153 return false;
154}
155
157{
158 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
159}
160
162{
163 // Log messages at Info, Warning and Error level unconditionally, so that
164 // important troubleshooting information doesn't get lost.
165 if (level >= BCLog::Level::Info) return true;
166
167 if (!WillLogCategory(category)) return false;
168
169 STDLOCK(m_cs);
170 const auto it{m_category_log_levels.find(category)};
171 return level >= (it == m_category_log_levels.end() ? LogLevel() : it->second);
172}
173
178
179static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
180 {"net", BCLog::NET},
181 {"tor", BCLog::TOR},
182 {"mempool", BCLog::MEMPOOL},
183 {"http", BCLog::HTTP},
184 {"bench", BCLog::BENCH},
185 {"zmq", BCLog::ZMQ},
186 {"walletdb", BCLog::WALLETDB},
187 {"rpc", BCLog::RPC},
188 {"estimatefee", BCLog::ESTIMATEFEE},
189 {"addrman", BCLog::ADDRMAN},
190 {"selectcoins", BCLog::SELECTCOINS},
191 {"reindex", BCLog::REINDEX},
192 {"cmpctblock", BCLog::CMPCTBLOCK},
193 {"rand", BCLog::RAND},
194 {"prune", BCLog::PRUNE},
195 {"proxy", BCLog::PROXY},
196 {"mempoolrej", BCLog::MEMPOOLREJ},
197 {"coindb", BCLog::COINDB},
198 {"qt", BCLog::QT},
199 {"leveldb", BCLog::LEVELDB},
200 {"validation", BCLog::VALIDATION},
201 {"i2p", BCLog::I2P},
202 {"ipc", BCLog::IPC},
203#ifdef DEBUG_LOCKCONTENTION
204 {"lock", BCLog::LOCK},
205#endif
206 {"blockstorage", BCLog::BLOCKSTORAGE},
207 {"txreconciliation", BCLog::TXRECONCILIATION},
208 {"scan", BCLog::SCAN},
209 {"txpackages", BCLog::TXPACKAGES},
210 {"kernel", BCLog::KERNEL},
211 {"privatebroadcast", BCLog::PRIVBROADCAST},
212 {"mining", BCLog::MINING},
213};
214
215static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
216 // Swap keys and values from LOG_CATEGORIES_BY_STR.
217 [](const auto& in) {
218 std::unordered_map<BCLog::LogFlags, std::string> out;
219 for (const auto& [k, v] : in) {
220 const bool inserted{out.emplace(v, k).second};
221 assert(inserted);
222 }
223 return out;
225};
226
227std::optional<BCLog::LogFlags> BCLog::Logger::GetLogCategory(std::string_view str)
228{
229 if (str.empty() || str == "1" || str == "all") {
230 return BCLog::ALL;
231 }
232 auto it = LOG_CATEGORIES_BY_STR.find(str);
233 if (it != LOG_CATEGORIES_BY_STR.end()) {
234 return it->second;
235 }
236 if (str == "libevent") {
237 LogWarning("The logging category `%s` is deprecated, does nothing, and will be removed in a future version", str);
238 return BCLog::NONE;
239 }
240 return std::nullopt;
241}
242
244{
245 switch (level) {
246 case BCLog::Level::Trace:
247 return "trace";
248 case BCLog::Level::Debug:
249 return "debug";
250 case BCLog::Level::Info:
251 return "info";
252 case BCLog::Level::Warning:
253 return "warning";
254 case BCLog::Level::Error:
255 return "error";
256 }
257 assert(false);
258}
259
260static std::string LogCategoryToStr(BCLog::LogFlags category)
261{
262 if (category == BCLog::ALL) {
263 return "all";
264 }
265 auto it = LOG_CATEGORIES_BY_FLAG.find(category);
266 assert(it != LOG_CATEGORIES_BY_FLAG.end());
267 return it->second;
268}
269
270static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str)
271{
272 if (level_str == "trace") {
273 return BCLog::Level::Trace;
274 } else if (level_str == "debug") {
275 return BCLog::Level::Debug;
276 } else if (level_str == "info") {
277 return BCLog::Level::Info;
278 } else if (level_str == "warning") {
279 return BCLog::Level::Warning;
280 } else if (level_str == "error") {
281 return BCLog::Level::Error;
282 } else {
283 return std::nullopt;
284 }
285}
286
287std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
288{
289 std::vector<LogCategory> ret;
290 ret.reserve(LOG_CATEGORIES_BY_STR.size());
291 for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) {
292 ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
293 }
294 return ret;
295}
296
298static constexpr std::array<BCLog::Level, 3> LogLevelsList()
299{
300 return {BCLog::Level::Info, BCLog::Level::Debug, BCLog::Level::Trace};
301}
302
304{
305 const auto& levels = LogLevelsList();
306 return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [](BCLog::Level level) { return LogLevelToStr(level); });
307}
308
309std::string BCLog::Logger::LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
310{
311 std::string strStamped;
312
313 if (!m_log_timestamps)
314 return strStamped;
315
316 const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
318 if (m_log_time_micros && !strStamped.empty()) {
319 strStamped.pop_back();
320 strStamped += strprintf(".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
321 }
322 if (mocktime > 0s) {
323 strStamped += " (mocktime: " + FormatISO8601DateTime(count_seconds(mocktime)) + ")";
324 }
325 strStamped += ' ';
326
327 return strStamped;
328}
329
330namespace BCLog {
338 std::string LogEscapeMessage(std::string_view str) {
339 std::string ret;
340 for (char ch_in : str) {
341 uint8_t ch = (uint8_t)ch_in;
342 if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
343 ret += ch_in;
344 } else {
345 ret += strprintf("\\x%02x", ch);
346 }
347 }
348 return ret;
349 }
350} // namespace BCLog
351
353{
354 if (category == LogFlags::NONE) category = LogFlags::ALL;
355
356 const bool has_category{m_always_print_category_level || category != LogFlags::ALL};
357
358 // If there is no category, Info is implied
359 if (!has_category && level == Level::Info) return {};
360
361 std::string s{"["};
362 if (has_category) {
363 s += LogCategoryToStr(category);
364 }
365
366 if (m_always_print_category_level || !has_category || level != Level::Debug) {
367 // If there is a category, Debug is implied, so don't add the level
368
369 // Only add separator if we have a category
370 if (has_category) s += ":";
371 s += Logger::LogLevelToStr(level);
372 }
373
374 s += "] ";
375 return s;
376}
377
378static size_t MemUsage(const util::log::Entry& log)
379{
380 return memusage::DynamicUsage(log.message) +
381 memusage::DynamicUsage(log.thread_name) +
382 memusage::MallocUsage(sizeof(memusage::list_node<util::log::Entry>));
383}
384
385BCLog::LogRateLimiter::LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window)
386 : m_max_bytes{max_bytes}, m_reset_window{reset_window} {}
387
388std::shared_ptr<BCLog::LogRateLimiter> BCLog::LogRateLimiter::Create(
389 SchedulerFunction&& scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window)
390{
391 auto limiter{std::shared_ptr<LogRateLimiter>(new LogRateLimiter(max_bytes, reset_window))};
392 std::weak_ptr<LogRateLimiter> weak_limiter{limiter};
393 auto reset = [weak_limiter] {
394 if (auto shared_limiter{weak_limiter.lock()}) shared_limiter->Reset();
395 };
396 scheduler_func(reset, limiter->m_reset_window);
397 return limiter;
398}
399
401 const SourceLocation& source_loc,
402 const std::string& str)
403{
405 auto& stats{m_source_locations.try_emplace(source_loc, m_max_bytes).first->second};
406 Status status{stats.m_dropped_bytes > 0 ? Status::STILL_SUPPRESSED : Status::UNSUPPRESSED};
407
408 if (!stats.Consume(str.size()) && status == Status::UNSUPPRESSED) {
411 }
412
413 return status;
414}
415
416std::string BCLog::Logger::Format(const util::log::Entry& entry) const
417{
418 std::string result{LogTimestampStr(entry.timestamp, entry.mocktime)};
419
420 if (m_log_threadnames) {
421 result += strprintf("[%s] ", (entry.thread_name.empty() ? "unknown" : entry.thread_name));
422 }
423
425 result += strprintf("[%s:%d] [%s] ", RemovePrefixView(entry.source_loc.file_name(), "./"), entry.source_loc.line(), entry.source_loc.function_name_short());
426 }
427
428 result += GetLogPrefix(static_cast<LogFlags>(entry.category), entry.level);
429 result += LogEscapeMessage(entry.message);
430
431 if (!result.ends_with('\n')) result += '\n';
432 return result;
433}
434
436{
437 STDLOCK(m_cs);
438 return LogPrint_(std::move(entry));
439}
440
441// NOLINTNEXTLINE(misc-no-recursion)
443{
444 if (m_buffering) {
445 {
446 m_cur_buffer_memusage += MemUsage(entry);
447 m_msgs_before_open.push_back(std::move(entry));
448 }
449
450 while (m_cur_buffer_memusage > m_max_buffer_memusage) {
451 if (m_msgs_before_open.empty()) {
452 m_cur_buffer_memusage = 0;
453 break;
454 }
455 m_cur_buffer_memusage -= MemUsage(m_msgs_before_open.front());
456 m_msgs_before_open.pop_front();
457 ++m_buffer_lines_discarded;
458 }
459
460 return;
461 }
462
463 std::string str_prefixed{Format(entry)};
464 bool ratelimit{false};
465 if (entry.should_ratelimit && m_limiter) {
466 auto status{m_limiter->Consume(entry.source_loc, str_prefixed)};
468 // NOLINTNEXTLINE(misc-no-recursion)
469 LogPrint_({
470 .category = LogFlags::ALL,
471 .level = Level::Warning,
472 .should_ratelimit = false, // with should_ratelimit=false, this cannot lead to infinite recursion
473 .source_loc = SourceLocation{__func__},
474 .message = strprintf(
475 "Excessive logging detected from %s:%d (%s): >%d bytes logged during "
476 "the last time window of %is. Suppressing logging to disk from this "
477 "source location until time window resets. Console logging "
478 "unaffected. Last log entry.",
480 m_limiter->m_max_bytes,
481 Ticks<std::chrono::seconds>(m_limiter->m_reset_window)),
482 });
483 } else if (status == LogRateLimiter::Status::STILL_SUPPRESSED) {
484 ratelimit = true;
485 }
486 }
487
488 // To avoid confusion caused by dropped log messages when debugging an issue,
489 // we prefix log lines with "[*]" when there are any suppressed source locations.
490 if (m_limiter && m_limiter->SuppressionsActive()) {
491 str_prefixed.insert(0, "[*] ");
492 }
493
494 if (m_print_to_console) {
495 // print to console
496 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
497 fflush(stdout);
498 }
499 for (const auto& cb : m_print_callbacks) {
500 cb(str_prefixed);
501 }
502 if (m_print_to_file && !ratelimit) {
503 assert(m_fileout != nullptr);
504
505 // reopen the log file, if requested
506 if (m_reopen_file) {
507 m_reopen_file = false;
508 FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
509 if (new_fileout) {
510 setbuf(new_fileout, nullptr); // unbuffered
511 fclose(m_fileout);
512 m_fileout = new_fileout;
513 }
514 }
515 FileWriteStr(str_prefixed, m_fileout);
516 }
517}
518
520{
521 STDLOCK(m_cs);
522
523 // Amount of debug.log to save at end when shrinking (must fit in memory)
524 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
525
526 assert(!m_file_path.empty());
527
528 // Scroll debug.log if it's getting too big
529 FILE* file = fsbridge::fopen(m_file_path, "r");
530
531 // Special files (e.g. device nodes) may not have a size.
532 size_t log_size = 0;
533 try {
534 log_size = fs::file_size(m_file_path);
535 } catch (const fs::filesystem_error&) {}
536
537 // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
538 // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
539 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
540 {
541 // Restart the file with some of the end
542 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
543 if (fseek(file, -((long)vch.size()), SEEK_END)) {
544 // LogWarning, except with m_cs held
545 LogPrint_({
546 .category = BCLog::ALL,
547 .level = Level::Warning,
548 .should_ratelimit = true,
549 .source_loc = SourceLocation{__func__},
550 .message = "Failed to shrink debug log file: fseek(...) failed",
551 });
552 fclose(file);
553 return;
554 }
555 int nBytes = fread(vch.data(), 1, vch.size(), file);
556 fclose(file);
557
558 file = fsbridge::fopen(m_file_path, "w");
559 if (file)
560 {
561 fwrite(vch.data(), 1, nBytes, file);
562 fclose(file);
563 }
564 }
565 else if (file != nullptr)
566 fclose(file);
567}
568
570{
571 decltype(m_source_locations) source_locations;
572 {
574 source_locations.swap(m_source_locations);
575 m_suppression_active = false;
576 }
577 for (const auto& [source_loc, stats] : source_locations) {
578 if (stats.m_dropped_bytes == 0) continue;
580 "Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.",
581 source_loc.file_name(), source_loc.line(), source_loc.function_name_short(),
582 stats.m_dropped_bytes, Ticks<std::chrono::seconds>(m_reset_window));
583 }
584}
585
587{
588 if (bytes > m_available_bytes) {
589 m_dropped_bytes += bytes;
591 return false;
592 }
593
594 m_available_bytes -= bytes;
595 return true;
596}
597
598bool BCLog::Logger::SetLogLevel(std::string_view level_str)
599{
600 const auto level = GetLogLevel(level_str);
601 if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
602 m_log_level = level.value();
603 return true;
604}
605
606bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
607{
608 const auto flag{GetLogCategory(category_str)};
609 if (!flag) return false;
610
611 const auto level = GetLogLevel(level_str);
612 if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
613 if (*flag == BCLog::NONE) return true;
614
615 STDLOCK(m_cs);
616 m_category_log_levels[*flag] = level.value();
617 return true;
618}
619
624
629
631{
632 BCLog::Logger& logger{LogInstance()};
633 if (logger.Enabled()) {
634 logger.LogPrint(std::move(entry));
635 }
636}
int ret
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition string.h:209
static std::shared_ptr< LogRateLimiter > Create(SchedulerFunction &&scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window)
Definition logging.cpp:388
std::function< void(std::function< void()>, std::chrono::milliseconds)> SchedulerFunction
Definition logging.h:96
const uint64_t m_max_bytes
Maximum number of bytes logged per location per window.
Definition logging.h:110
LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window)
Definition logging.cpp:385
Status Consume(const SourceLocation &source_loc, const std::string &str) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition logging.cpp:400
const std::chrono::seconds m_reset_window
Interval after which the window is reset.
Definition logging.h:112
Status
Suppression status of a source log location.
Definition logging.h:114
void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Resets all usage to zero. Called periodically by the scheduler.
Definition logging.cpp:569
std::atomic< bool > m_suppression_active
Whether any log locations are suppressed. Cached view on m_source_locations for performance reasons.
Definition logging.h:92
static std::string LogLevelToStr(BCLog::Level level)
Returns the string representation of a log level.
Definition logging.cpp:243
bool m_always_print_category_level
Definition logging.h:175
bool WillLogCategory(LogFlags category) const
Definition logging.cpp:156
std::atomic< CategoryMask > m_categories
Log categories bitfield.
Definition logging.h:153
std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
Definition logging.cpp:309
bool DefaultShrinkDebugFile() const
Definition logging.cpp:174
void LogPrint_(util::log::Entry log_entry) EXCLUSIVE_LOCKS_REQUIRED(m_cs)
Send an entry to the log output (internal).
Definition logging.cpp:442
bool m_log_sourcelocations
Definition logging.h:174
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition logging.h:237
void SetLogLevel(Level level)
Definition logging.h:250
std::atomic< Level > m_log_level
Definition logging.h:150
Level LogLevel() const
Definition logging.h:249
void ShrinkDebugFile() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition logging.cpp:519
bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition logging.cpp:161
fs::path m_file_path
Definition logging.h:177
bool m_log_time_micros
Definition logging.h:172
bool m_log_threadnames
Definition logging.h:173
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:116
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
Definition logging.cpp:287
static std::optional< BCLog::LogFlags > GetLogCategory(std::string_view str)
Return log flag if str parses as a log category.
Definition logging.cpp:227
bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Start logging (and flush all buffered messages).
Definition logging.cpp:54
void EnableCategory(LogFlags flag)
Definition logging.cpp:128
bool m_log_timestamps
Definition logging.h:171
std::string GetLogPrefix(LogFlags category, Level level) const
Definition logging.cpp:352
void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Only for testing.
Definition logging.cpp:103
std::string LogLevelsString() const
Returns a string with all user-selectable log levels.
Definition logging.cpp:303
std::atomic< bool > m_reopen_file
Definition logging.h:178
void LogPrint(util::log::Entry log_entry) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Send an entry to the log output.
Definition logging.cpp:435
bool m_print_to_file
Definition logging.h:169
bool m_print_to_console
Definition logging.h:168
StdMutex m_cs
Definition logging.h:133
std::string Format(const util::log::Entry &entry) const
Definition logging.cpp:416
bool Enabled() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Returns whether logs will be written to any output.
Definition logging.h:184
void DisableCategory(LogFlags flag)
Definition logging.cpp:142
Like std::source_location, but allowing to override the function name.
Definition log.h:23
std::string_view function_name_short() const
Definition log.h:35
std::string_view file_name() const
Definition log.h:33
std::uint_least32_t line() const
Definition log.h:34
#define LogWarning(...)
Definition log.h:126
static constexpr std::array< BCLog::Level, 3 > LogLevelsList()
Log severity levels that can be selected by the user.
Definition logging.cpp:298
static int FileWriteStr(std::string_view str, FILE *fp)
Definition logging.cpp:49
static std::string LogCategoryToStr(BCLog::LogFlags category)
Definition logging.cpp:260
static const std::map< std::string, BCLog::LogFlags, std::less<> > LOG_CATEGORIES_BY_STR
Definition logging.cpp:179
BCLog::Logger & LogInstance()
Definition logging.cpp:26
bool fLogIPs
Definition logging.cpp:47
static const std::unordered_map< BCLog::LogFlags, std::string > LOG_CATEGORIES_BY_FLAG
Definition logging.cpp:215
const char *const DEFAULT_DEBUGLOGFILE
Definition logging.cpp:23
static std::optional< BCLog::Level > GetLogLevel(std::string_view level_str)
Definition logging.cpp:270
static size_t MemUsage(const util::log::Entry &log)
Definition logging.cpp:378
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL
Definition logging.cpp:24
constexpr bool DEFAULT_LOGIPS
Definition logging.h:31
std::string LogEscapeMessage(std::string_view str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
Definition logging.cpp:338
util::log::Level Level
Alias for compatibility. Prefer util::log::Level over BCLog::Level in new code.
Definition log.h:114
constexpr size_t DEFAULT_MAX_LOG_BUFFER
Definition logging.h:65
@ ESTIMATEFEE
Definition categories.h:24
@ MINING
Definition categories.h:48
@ TXRECONCILIATION
Definition categories.h:43
@ BLOCKSTORAGE
Definition categories.h:42
@ COINDB
Definition categories.h:33
@ REINDEX
Definition categories.h:27
@ TXPACKAGES
Definition categories.h:45
@ WALLETDB
Definition categories.h:22
@ PRIVBROADCAST
Definition categories.h:47
@ ADDRMAN
Definition categories.h:25
@ LEVELDB
Definition categories.h:35
@ VALIDATION
Definition categories.h:36
@ MEMPOOLREJ
Definition categories.h:32
@ PRUNE
Definition categories.h:30
@ CMPCTBLOCK
Definition categories.h:28
@ PROXY
Definition categories.h:31
@ MEMPOOL
Definition categories.h:18
@ SELECTCOINS
Definition categories.h:26
@ BENCH
Definition categories.h:20
@ KERNEL
Definition categories.h:46
FILE * fopen(const fs::path &p, const char *mode)
Definition fs.cpp:23
void Log(Entry entry)
Send message to be logged.
Definition logging.cpp:630
bool ShouldDebugLog(Category category)
Return whether messages with specified category should be debug logged.
Definition logging.cpp:620
bool ShouldTraceLog(Category category)
Return whether messages with specified category should be trace logged.
Definition logging.cpp:625
constexpr NoRateLimitTag NO_RATE_LIMIT
Definition log.h:50
uint64_t Category
Opaque to util::log; interpreted by consumers (e.g., BCLog::LogFlags).
Definition log.h:44
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition string.h:209
std::string_view RemovePrefixView(std::string_view str LIFETIMEBOUND, std::string_view prefix)
Definition string.h:186
std::string_view RemovePrefixView(std::string_view str LIFETIMEBOUND, std::string_view prefix)
Definition string.h:186
#define STDLOCK(cs)
Definition stdmutex.h:41
uint64_t m_available_bytes
Remaining bytes.
Definition logging.h:77
bool Consume(uint64_t bytes)
Updates internal accounting and returns true if enough available_bytes were remaining.
Definition logging.cpp:586
uint64_t m_dropped_bytes
Number of bytes that were consumed but didn't fit in the available bytes.
Definition logging.h:79
Definition log.h:60
SourceLocation source_loc
Definition log.h:67
SystemClock::time_point timestamp
Definition log.h:64
bool should_ratelimit
Hint for consumers if this entry should be ratelimited.
Definition log.h:63
std::string thread_name
Definition log.h:66
Level level
Definition log.h:62
std::string message
Definition log.h:68
std::chrono::seconds mocktime
Definition log.h:65
Category category
Definition log.h:61
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition time.cpp:90
constexpr auto TicksSinceEpoch(Timepoint t)
Definition time.h:93
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition time.h:97
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())