Bitcoin ABC 0.32.4
P2P Digital Currency
logging.cpp
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#include <logging.h>
8#include <memusage.h>
9#include <util/fs.h>
10
11#include <util/string.h>
12#include <util/threadnames.h>
13#include <util/time.h>
14
15#include <array>
16#include <cstring>
17#include <map>
18#include <optional>
19#include <unordered_map>
20
22const char *const DEFAULT_DEBUGLOGFILE = "debug.log";
24
41 static BCLog::Logger *g_logger{new BCLog::Logger()};
42 return *g_logger;
43}
44
45static int FileWriteStr(std::string_view str, FILE *fp) {
46 return fwrite(str.data(), 1, str.size(), fp);
47}
48
50 StdLockGuard scoped_lock(m_cs);
51
52 assert(m_buffering);
53 assert(m_fileout == nullptr);
54
55 if (m_print_to_file) {
56 assert(!m_file_path.empty());
57 m_fileout = fsbridge::fopen(m_file_path, "a");
58 if (!m_fileout) {
59 return false;
60 }
61
62 // Unbuffered.
63 setbuf(m_fileout, nullptr);
64
65 // Add newlines to the logfile to distinguish this execution from the
66 // last one.
67 FileWriteStr("\n\n\n\n\n", m_fileout);
68 }
69
70 // Dump buffered messages from before we opened the log.
71 m_buffering = false;
72 if (m_buffer_lines_discarded > 0) {
75 "Early logging buffer overflowed, %d log lines discarded.\n",
76 m_buffer_lines_discarded),
77 std::source_location::current(), BCLog::ALL, Level::Info,
78 /*should_ratelimit=*/false);
79 }
80 while (!m_msgs_before_open.empty()) {
81 const auto &buflog = m_msgs_before_open.front();
82 std::string s{buflog.str};
83 FormatLogStrInPlace(s, buflog.category, buflog.level, buflog.source_loc,
84 buflog.threadname, buflog.now, buflog.mocktime);
85 m_msgs_before_open.pop_front();
86
87 if (m_print_to_file) {
88 FileWriteStr(s, m_fileout);
89 }
91 fwrite(s.data(), 1, s.size(), stdout);
92 }
93 for (const auto &cb : m_print_callbacks) {
94 cb(s);
95 }
96 }
97 m_cur_buffer_memusage = 0;
99 fflush(stdout);
100 }
101
102 return true;
103}
104
106 StdLockGuard scoped_lock(m_cs);
107 m_buffering = true;
108 if (m_fileout != nullptr) {
109 fclose(m_fileout);
110 }
111 m_fileout = nullptr;
112 m_print_callbacks.clear();
113 m_max_buffer_memusage = DEFAULT_MAX_LOG_BUFFER;
114 m_cur_buffer_memusage = 0;
115 m_buffer_lines_discarded = 0;
116 m_msgs_before_open.clear();
117}
118
120 {
121 StdLockGuard scoped_lock(m_cs);
122 assert(m_buffering);
123 assert(m_print_callbacks.empty());
124 }
125 m_print_to_file = false;
126 m_print_to_console = false;
127 StartLogging();
128}
129
130static const std::map<std::string, BCLog::LogFlags, std::less<>>
132 {"0", BCLog::NONE},
133 {"", BCLog::NONE},
134 {"net", BCLog::NET},
135 {"tor", BCLog::TOR},
136 {"mempool", BCLog::MEMPOOL},
137 {"http", BCLog::HTTP},
138 {"bench", BCLog::BENCH},
139 {"zmq", BCLog::ZMQ},
140 {"walletdb", BCLog::WALLETDB},
141 {"rpc", BCLog::RPC},
142 {"estimatefee", BCLog::ESTIMATEFEE},
143 {"addrman", BCLog::ADDRMAN},
144 {"selectcoins", BCLog::SELECTCOINS},
145 {"reindex", BCLog::REINDEX},
146 {"cmpctblock", BCLog::CMPCTBLOCK},
147 {"rand", BCLog::RAND},
148 {"prune", BCLog::PRUNE},
149 {"proxy", BCLog::PROXY},
150 {"mempoolrej", BCLog::MEMPOOLREJ},
151 {"libevent", BCLog::LIBEVENT},
152 {"coindb", BCLog::COINDB},
153 {"qt", BCLog::QT},
154 {"leveldb", BCLog::LEVELDB},
155 {"validation", BCLog::VALIDATION},
156 {"avalanche", BCLog::AVALANCHE},
157 {"i2p", BCLog::I2P},
158 {"chronik", BCLog::CHRONIK},
159#ifdef DEBUG_LOCKCONTENTION
160 {"lock", BCLog::LOCK},
161#endif
162 {"blockstorage", BCLog::BLOCKSTORE},
163 {"netdebug", BCLog::NETDEBUG},
164 {"txpackages", BCLog::TXPACKAGES},
165 {"1", BCLog::ALL},
166 {"all", BCLog::ALL},
167 };
168
169static const std::unordered_map<BCLog::LogFlags, std::string>
171 // Swap keys and values from LOG_CATEGORIES_BY_STR.
172 [](const auto &in) {
173 std::unordered_map<BCLog::LogFlags, std::string> out;
174 for (const auto &[k, v] : in) {
175 switch (v) {
176 case BCLog::NONE:
177 out.emplace(BCLog::NONE, "");
178 break;
179 case BCLog::ALL:
180 out.emplace(BCLog::ALL, "all");
181 break;
182 default:
183 out.emplace(v, k);
184 }
185 }
186 return out;
188
189bool GetLogCategory(BCLog::LogFlags &flag, std::string_view str) {
190 if (str.empty()) {
191 flag = BCLog::ALL;
192 return true;
193 }
194 auto it = LOG_CATEGORIES_BY_STR.find(str);
195 if (it != LOG_CATEGORIES_BY_STR.end()) {
196 flag = it->second;
197 return true;
198 }
199 return false;
200}
201
203 switch (level) {
205 return "trace";
207 return "debug";
209 return "info";
211 return "warning";
213 return "error";
214 }
215 assert(false);
216}
217
218std::string LogCategoryToStr(BCLog::LogFlags category) {
219 auto it = LOG_CATEGORIES_BY_FLAG.find(category);
220 assert(it != LOG_CATEGORIES_BY_FLAG.end());
221 return it->second;
222}
223
224static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str) {
225 if (level_str == "trace") {
226 return BCLog::Level::Trace;
227 } else if (level_str == "debug") {
228 return BCLog::Level::Debug;
229 } else if (level_str == "info") {
230 return BCLog::Level::Info;
231 } else if (level_str == "warning") {
233 } else if (level_str == "error") {
234 return BCLog::Level::Error;
235 } else {
236 return std::nullopt;
237 }
238}
239
240std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const {
241 std::vector<LogCategory> ret;
242 for (const auto &[category, flag] : LOG_CATEGORIES_BY_STR) {
243 if (flag != BCLog::NONE && flag != BCLog::ALL) {
244 ret.push_back(LogCategory{.category = category,
245 .active = WillLogCategory(flag)});
246 }
247 }
248 return ret;
249}
250
252 if (m_fileout) {
253 fclose(m_fileout);
254 }
255}
256
258static constexpr std::array<BCLog::Level, 3> LogLevelsList() {
260}
261
263 const auto &levels = LogLevelsList();
264 return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ",
265 [](BCLog::Level level) { return LogLevelToStr(level); });
266}
267
268std::string
269BCLog::Logger::LogTimestampStr(SystemClock::time_point now,
270 std::chrono::seconds mocktime) const {
271 std::string strStamped;
272
273 if (!m_log_timestamps) {
274 return strStamped;
275 }
276
277 const auto now_seconds{
278 std::chrono::time_point_cast<std::chrono::seconds>(now)};
279 strStamped = FormatISO8601DateTime(
280 TicksSinceEpoch<std::chrono::seconds>(now_seconds));
281 if (m_log_time_micros && !strStamped.empty()) {
282 strStamped.pop_back();
283 strStamped += strprintf(
284 ".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
285 }
286 if (mocktime > 0s) {
287 strStamped +=
288 " (mocktime: " + FormatISO8601DateTime(count_seconds(mocktime)) +
289 ")";
290 }
291 strStamped += ' ';
292
293 return strStamped;
294}
295
296namespace BCLog {
304std::string LogEscapeMessage(std::string_view str) {
305 std::string ret;
306 for (char ch_in : str) {
307 uint8_t ch = (uint8_t)ch_in;
308 if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
309 ret += ch_in;
310 } else {
311 ret += strprintf("\\x%02x", ch);
312 }
313 }
314 return ret;
315}
316} // namespace BCLog
317
319 BCLog::Level level) const {
320 if (category == LogFlags::NONE) {
321 category = LogFlags::ALL;
322 }
323
324 const bool has_category{m_always_print_category_level ||
325 category != LogFlags::ALL};
326
327 // If there is no category, Info is implied
328 if (!has_category && level == Level::Info) {
329 return {};
330 }
331
332 std::string s{"["};
333 if (has_category) {
334 s += LogCategoryToStr(category);
335 }
336
337 if (m_always_print_category_level || !has_category ||
338 level != Level::Debug) {
339 // If there is a category, Debug is implied, so don't add the level
340
341 // Only add separator if we have a category
342 if (has_category) {
343 s += ":";
344 }
345 s += Logger::LogLevelToStr(level);
346 }
347
348 s += "] ";
349 return s;
350}
351
352static size_t MemUsage(const BCLog::Logger::BufferedLog &buflog) {
353 // TODO: use memusage::DynamicUsage for string after PR #31164
354 return buflog.str.size() + buflog.threadname.size() +
357}
358
360 std::chrono::seconds reset_window)
361 : m_max_bytes{max_bytes}, m_reset_window{reset_window} {}
362
363std::shared_ptr<BCLog::LogRateLimiter>
365 uint64_t max_bytes,
366 std::chrono::seconds reset_window) {
367 auto limiter{std::shared_ptr<LogRateLimiter>(
368 new LogRateLimiter(max_bytes, reset_window))};
369 std::weak_ptr<LogRateLimiter> weak_limiter{limiter};
370 auto reset = [weak_limiter] {
371 if (auto shared_limiter{weak_limiter.lock()}) {
372 shared_limiter->Reset();
373 }
374 return true;
375 };
376 scheduler_func(reset, limiter->m_reset_window);
377 return limiter;
378}
379
381BCLog::LogRateLimiter::Consume(const std::source_location &source_loc,
382 const std::string &str) {
383 StdLockGuard scoped_lock(m_mutex);
384 auto &stats{
385 m_source_locations.try_emplace(source_loc, m_max_bytes).first->second};
386 Status status{stats.m_dropped_bytes > 0 ? Status::STILL_SUPPRESSED
387 : Status::UNSUPPRESSED};
388
389 if (!stats.Consume(str.size()) && status == Status::UNSUPPRESSED) {
390 status = Status::NEWLY_SUPPRESSED;
391 m_suppression_active = true;
392 }
393
394 return status;
395}
396
398 std::string &str, BCLog::LogFlags category, BCLog::Level level,
399 const std::source_location &source_loc, std::string_view threadname,
400 SystemClock::time_point now, std::chrono::seconds mocktime) const {
401 str.insert(0, GetLogPrefix(category, level));
402
403 if (m_log_sourcelocations) {
404 str.insert(0, strprintf("[%s:%d] [%s] ",
405 RemovePrefixView(source_loc.file_name(), "./"),
406 source_loc.line(), source_loc.function_name()));
407 }
408
409 if (m_log_threadnames) {
410 str.insert(0, strprintf("[%s] ",
411 (threadname.empty() ? "unknown" : threadname)));
412 }
413
414 str.insert(0, LogTimestampStr(now, mocktime));
415}
416
417void BCLog::Logger::LogPrintStr(std::string_view str,
418 std::source_location &&source_loc,
419 BCLog::LogFlags category, BCLog::Level level,
420 bool should_ratelimit) {
421 StdLockGuard scoped_lock(m_cs);
422 return LogPrintStr_(str, std::move(source_loc), category, level,
423 should_ratelimit);
424}
425
426void BCLog::Logger::LogPrintStr_(std::string_view str,
427 std::source_location &&source_loc,
428 BCLog::LogFlags category, BCLog::Level level,
429 bool should_ratelimit) {
430 std::string str_prefixed = LogEscapeMessage(str);
431
432 const bool starts_new_line = m_started_new_line;
433 m_started_new_line = !str.empty() && str[str.size() - 1] == '\n';
434
435 if (m_buffering) {
436 if (!starts_new_line) {
437 if (!m_msgs_before_open.empty()) {
438 m_msgs_before_open.back().str += str_prefixed;
439 m_cur_buffer_memusage += str_prefixed.size();
440 return;
441 } else {
442 // unlikely edge case; add a marker that something was trimmed
443 str_prefixed.insert(0, "[...] ");
444 }
445 }
446
447 {
448 BufferedLog buf{
449 .now = SystemClock::now(),
450 .mocktime = GetMockTime(),
451 .str = str_prefixed,
452 .threadname = util::ThreadGetInternalName(),
453 .source_loc = std::move(source_loc),
454 .category = category,
455 .level = level,
456 };
457 m_cur_buffer_memusage += MemUsage(buf);
458 m_msgs_before_open.push_back(std::move(buf));
459 }
460
461 while (m_cur_buffer_memusage > m_max_buffer_memusage) {
462 if (m_msgs_before_open.empty()) {
463 m_cur_buffer_memusage = 0;
464 break;
465 }
466 m_cur_buffer_memusage -= MemUsage(m_msgs_before_open.front());
467 m_msgs_before_open.pop_front();
468 ++m_buffer_lines_discarded;
469 }
470
471 return;
472 }
473
474 FormatLogStrInPlace(str_prefixed, category, level, source_loc,
475 util::ThreadGetInternalName(), SystemClock::now(),
476 GetMockTime());
477 bool ratelimit{false};
478 if (should_ratelimit && m_limiter) {
479 auto status{m_limiter->Consume(source_loc, str_prefixed)};
481 // with should_ratelimit=false, this cannot lead to infinit
482 // recursion
483 // NOLINTNEXTLINE(misc-no-recursion)
484 LogPrintStr_(
485 strprintf(
486 "Excessive logging detected from %s:%d (%s): >%d bytes "
487 "logged during "
488 "the last time window of %is. Suppressing logging to disk "
489 "from this "
490 "source location until time window resets. Console logging "
491 "unaffected. Last log entry.\n",
492 source_loc.file_name(), source_loc.line(),
493 source_loc.function_name(), m_limiter->m_max_bytes,
494 Ticks<std::chrono::seconds>(m_limiter->m_reset_window)),
495 std::source_location::current(), LogFlags::ALL, Level::Warning,
496 /*should_ratelimit=*/false);
497 } else if (status == LogRateLimiter::Status::STILL_SUPPRESSED) {
498 ratelimit = true;
499 }
500 }
501
502 // To avoid confusion caused by dropped log messages when debugging an
503 // issue, we prefix log lines with "[*]" when there are any suppressed
504 // source locations.
505 if (m_limiter && m_limiter->SuppressionsActive()) {
506 str_prefixed.insert(0, "[*] ");
507 }
508
509 if (m_print_to_console) {
510 // Print to console.
511 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
512 fflush(stdout);
513 }
514 for (const auto &cb : m_print_callbacks) {
515 cb(str_prefixed);
516 }
517 if (m_print_to_file && !ratelimit) {
518 assert(m_fileout != nullptr);
519
520 // Reopen the log file, if requested.
521 if (m_reopen_file) {
522 m_reopen_file = false;
523 FILE *new_fileout = fsbridge::fopen(m_file_path, "a");
524 if (new_fileout) {
525 // unbuffered.
526 setbuf(m_fileout, nullptr);
527 fclose(m_fileout);
528 m_fileout = new_fileout;
529 }
530 }
531 FileWriteStr(str_prefixed, m_fileout);
532 }
533}
534
536 // Amount of debug.log to save at end when shrinking (must fit in memory)
537 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
538
539 assert(!m_file_path.empty());
540
541 // Scroll debug.log if it's getting too big.
542 FILE *file = fsbridge::fopen(m_file_path, "r");
543
544 // Special files (e.g. device nodes) may not have a size.
545 size_t log_size = 0;
546 try {
547 log_size = fs::file_size(m_file_path);
548 } catch (const fs::filesystem_error &) {
549 }
550
551 // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
552 // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes.
553 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) {
554 // Restart the file with some of the end.
555 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
556 if (fseek(file, -((long)vch.size()), SEEK_END)) {
557 LogPrintf("Failed to shrink debug log file: fseek(...) failed\n");
558 fclose(file);
559 return;
560 }
561 int nBytes = fread(vch.data(), 1, vch.size(), file);
562 fclose(file);
563
564 file = fsbridge::fopen(m_file_path, "w");
565 if (file) {
566 fwrite(vch.data(), 1, nBytes, file);
567 fclose(file);
568 }
569 } else if (file != nullptr) {
570 fclose(file);
571 }
572}
573
575 m_categories |= category;
576}
577
578bool BCLog::Logger::EnableCategory(std::string_view str) {
579 BCLog::LogFlags flag;
580 if (!GetLogCategory(flag, str)) {
581 return false;
582 }
583 EnableCategory(flag);
584 return true;
585}
586
588 m_categories &= ~category;
589}
590
591bool BCLog::Logger::DisableCategory(std::string_view str) {
592 BCLog::LogFlags flag;
593 if (!GetLogCategory(flag, str)) {
594 return false;
595 }
596 DisableCategory(flag);
597 return true;
598}
599
601 // ALL is not meant to be used as a logging category, but only as a mask
602 // representing all categories.
603 if (category == BCLog::NONE || category == BCLog::ALL) {
604 LogPrintf("Error trying to log using a category mask instead of an "
605 "explicit category.\n");
606 return true;
607 }
608
609 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
610}
611
613 BCLog::Level level) const {
614 // Log messages at Info, Warning and Error level unconditionally, so that
615 // important troubleshooting information doesn't get lost.
616 if (level >= BCLog::Level::Info) {
617 return true;
618 }
619
620 if (!WillLogCategory(category)) {
621 return false;
622 }
623
624 StdLockGuard scoped_lock(m_cs);
625 const auto it{m_category_log_levels.find(category)};
626 return level >=
627 (it == m_category_log_levels.end() ? LogLevel() : it->second);
628}
629
631 return m_categories != BCLog::NONE;
632}
633
635 decltype(m_source_locations) source_locations;
636 {
637 StdLockGuard scoped_lock(m_mutex);
638 source_locations.swap(m_source_locations);
639 m_suppression_active = false;
640 }
641 for (const auto &[source_loc, stats] : source_locations) {
642 if (stats.m_dropped_bytes == 0) {
643 continue;
644 }
645 LogPrintLevel_(LogFlags::ALL, Level::Warning,
646 /*should_ratelimit=*/false,
647 "Restarting logging from %s:%d (%s): %d bytes were "
648 "dropped during the last %ss.\n",
649 source_loc.file_name(), source_loc.line(),
650 source_loc.function_name(), stats.m_dropped_bytes,
651 Ticks<std::chrono::seconds>(m_reset_window));
652 }
653}
654
656 if (bytes > m_available_bytes) {
657 m_dropped_bytes += bytes;
658 m_available_bytes = 0;
659 return false;
660 }
661
662 m_available_bytes -= bytes;
663 return true;
664}
665
666bool BCLog::Logger::SetLogLevel(std::string_view level_str) {
667 const auto level = GetLogLevel(level_str);
668 if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) {
669 return false;
670 }
671 m_log_level = level.value();
672 return true;
673}
674
675bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str,
676 std::string_view level_str) {
677 BCLog::LogFlags flag;
678 if (!GetLogCategory(flag, category_str)) {
679 return false;
680 }
681
682 const auto level = GetLogLevel(level_str);
683 if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) {
684 return false;
685 }
686
687 StdLockGuard scoped_lock(m_cs);
688 m_category_log_levels[flag] = level.value();
689 return true;
690}
Fixed window rate limiter for logging.
Definition: logging.h:124
static std::shared_ptr< LogRateLimiter > Create(SchedulerFunction &&scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window)
Definition: logging.cpp:364
std::function< void(std::function< bool()>, std::chrono::milliseconds)> SchedulerFunction
Definition: logging.h:155
LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window)
Definition: logging.cpp:359
Status Consume(const std::source_location &source_loc, const std::string &str) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Consumes source_loc's available bytes corresponding to the size of the (formatted) str and returns it...
Definition: logging.cpp:381
Status
Suppression status of a source log location.
Definition: logging.h:173
void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Resets all usage to zero. Called periodically by the scheduler.
Definition: logging.cpp:634
static std::string LogLevelToStr(BCLog::Level level)
Returns the string representation of a log level.
Definition: logging.cpp:202
void LogPrintStr(std::string_view str, std::source_location &&source_loc, BCLog::LogFlags category, BCLog::Level level, bool should_ratelimit)
Send a string to the log output.
Definition: logging.cpp:417
bool WillLogCategory(LogFlags category) const
Return true if log accepts specified category.
Definition: logging.cpp:600
std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
Definition: logging.cpp:269
bool DefaultShrinkDebugFile() const
Default for whether ShrinkDebugFile should be run.
Definition: logging.cpp:630
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition: logging.h:327
void SetLogLevel(Level level)
Definition: logging.h:337
bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition: logging.cpp:612
fs::path m_file_path
Definition: logging.h:265
void DisableLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
This offers a slight speedup and slightly smaller memory usage compared to leaving the logging system...
Definition: logging.cpp:119
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
Definition: logging.cpp:240
bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Start logging (and flush all buffered messages)
Definition: logging.cpp:49
void DisableCategory(LogFlags category)
Definition: logging.cpp:587
void EnableCategory(LogFlags category)
Definition: logging.cpp:574
std::string GetLogPrefix(LogFlags category, Level level) const
Definition: logging.cpp:318
void LogPrintStr_(std::string_view str, std::source_location &&source_loc, BCLog::LogFlags category, BCLog::Level level, bool should_ratelimit) EXCLUSIVE_LOCKS_REQUIRED(m_cs)
Send a string to the log output (internal)
Definition: logging.cpp:426
void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Only for testing.
Definition: logging.cpp:105
std::string LogLevelsString() const
Returns a string with all user-selectable log levels.
Definition: logging.cpp:262
void ShrinkDebugFile()
Definition: logging.cpp:535
bool m_print_to_file
Definition: logging.h:257
void FormatLogStrInPlace(std::string &str, LogFlags category, Level level, const std::source_location &source_loc, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
Definition: logging.cpp:397
bool m_print_to_console
Definition: logging.h:256
StdMutex m_cs
Definition: logging.h:206
static constexpr std::array< BCLog::Level, 3 > LogLevelsList()
Log severity levels that can be selected by the user.
Definition: logging.cpp:258
static int FileWriteStr(std::string_view str, FILE *fp)
Definition: logging.cpp:45
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:189
static size_t MemUsage(const BCLog::Logger::BufferedLog &buflog)
Definition: logging.cpp:352
std::string LogCategoryToStr(BCLog::LogFlags category)
Definition: logging.cpp:218
static const std::map< std::string, BCLog::LogFlags, std::less<> > LOG_CATEGORIES_BY_STR
Definition: logging.cpp:131
BCLog::Logger & LogInstance()
Definition: logging.cpp:25
bool fLogIPs
Definition: logging.cpp:21
static const std::unordered_map< BCLog::LogFlags, std::string > LOG_CATEGORIES_BY_FLAG
Definition: logging.cpp:170
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:22
static std::optional< BCLog::Level > GetLogLevel(std::string_view level_str)
Definition: logging.cpp:224
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL
Definition: logging.cpp:23
#define LogPrintLevel_(category, level, should_ratelimit,...)
Definition: logging.h:407
static const bool DEFAULT_LOGIPS
Definition: logging.h:32
#define LogPrintf(...)
Definition: logging.h:424
Level
Definition: logging.h:103
std::string LogEscapeMessage(std::string_view str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
Definition: logging.cpp:304
constexpr size_t DEFAULT_MAX_LOG_BUFFER
Definition: logging.h:115
LogFlags
Definition: logging.h:67
@ ESTIMATEFEE
Definition: logging.h:77
@ AVALANCHE
Definition: logging.h:91
@ RAND
Definition: logging.h:82
@ COINDB
Definition: logging.h:87
@ REINDEX
Definition: logging.h:80
@ TXPACKAGES
Definition: logging.h:99
@ WALLETDB
Definition: logging.h:75
@ ADDRMAN
Definition: logging.h:78
@ ALL
Definition: logging.h:100
@ NETDEBUG
Definition: logging.h:98
@ RPC
Definition: logging.h:76
@ HTTP
Definition: logging.h:72
@ LEVELDB
Definition: logging.h:89
@ NONE
Definition: logging.h:68
@ VALIDATION
Definition: logging.h:90
@ MEMPOOLREJ
Definition: logging.h:85
@ PRUNE
Definition: logging.h:83
@ TOR
Definition: logging.h:70
@ LIBEVENT
Definition: logging.h:86
@ CMPCTBLOCK
Definition: logging.h:81
@ PROXY
Definition: logging.h:84
@ CHRONIK
Definition: logging.h:93
@ ZMQ
Definition: logging.h:74
@ MEMPOOL
Definition: logging.h:71
@ SELECTCOINS
Definition: logging.h:79
@ I2P
Definition: logging.h:92
@ BENCH
Definition: logging.h:73
@ NET
Definition: logging.h:69
@ QT
Definition: logging.h:88
@ BLOCKSTORE
Definition: logging.h:97
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
bool StartLogging(const ArgsManager &args)
Definition: common.cpp:205
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:74
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:39
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
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:43
bool Consume(uint64_t bytes)
Updates internal accounting and returns true if enough available_bytes were remaining.
Definition: logging.cpp:655
std::string threadname
Definition: logging.h:197
SystemClock::time_point now
Definition: logging.h:195
std::string category
Definition: logging.h:61
#define LOCK(cs)
Definition: sync.h:306
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:97
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:109
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:57
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
assert(!tx.IsCoinBase())