19#include <unordered_map>
49 return fwrite(str.data(), 1, str.size(), fp);
56 assert(m_fileout ==
nullptr);
66 setbuf(m_fileout,
nullptr);
75 if (m_buffer_lines_discarded > 0) {
78 "Early logging buffer overflowed, %d log lines discarded.\n",
79 m_buffer_lines_discarded),
83 while (!m_msgs_before_open.empty()) {
84 const auto &buflog = m_msgs_before_open.front();
85 std::string s{buflog.str};
87 buflog.threadname, buflog.now, buflog.mocktime);
88 m_msgs_before_open.pop_front();
94 fwrite(s.data(), 1, s.size(), stdout);
96 for (
const auto &cb : m_print_callbacks) {
100 m_cur_buffer_memusage = 0;
111 if (m_fileout !=
nullptr) {
115 m_print_callbacks.clear();
117 m_cur_buffer_memusage = 0;
118 m_buffer_lines_discarded = 0;
119 m_msgs_before_open.clear();
126 assert(m_print_callbacks.empty());
128 m_print_to_file =
false;
129 m_print_to_console =
false;
133static const std::map<std::string, BCLog::LogFlags, std::less<>>
162#ifdef DEBUG_LOCKCONTENTION
172static const std::unordered_map<BCLog::LogFlags, std::string>
176 std::unordered_map<BCLog::LogFlags, std::string> out;
177 for (
const auto &[k, v] : in) {
227static std::optional<BCLog::Level>
GetLogLevel(std::string_view level_str) {
228 if (level_str ==
"trace") {
230 }
else if (level_str ==
"debug") {
232 }
else if (level_str ==
"info") {
234 }
else if (level_str ==
"warning") {
236 }
else if (level_str ==
"error") {
244 std::vector<LogCategory> ret;
248 .active = WillLogCategory(flag)});
267 return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()},
", ",
268 [](
BCLog::Level level) {
return LogLevelToStr(level); });
273 std::chrono::seconds mocktime)
const {
274 std::string strStamped;
276 if (!m_log_timestamps) {
280 const auto now_seconds{
281 std::chrono::time_point_cast<std::chrono::seconds>(now)};
283 TicksSinceEpoch<std::chrono::seconds>(now_seconds));
284 if (m_log_time_micros && !strStamped.empty()) {
285 strStamped.pop_back();
287 ".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
309 for (
char ch_in : str) {
310 uint8_t ch = (uint8_t)ch_in;
311 if ((ch >= 32 || ch ==
'\n') && ch !=
'\x7f') {
324 category = LogFlags::ALL;
327 const bool has_category{m_always_print_category_level ||
328 category != LogFlags::ALL};
340 if (m_always_print_category_level || !has_category ||
363 std::chrono::seconds reset_window)
364 : m_max_bytes{max_bytes}, m_reset_window{reset_window} {}
366std::shared_ptr<BCLog::LogRateLimiter>
369 std::chrono::seconds reset_window) {
370 auto limiter{std::shared_ptr<LogRateLimiter>(
372 std::weak_ptr<LogRateLimiter> weak_limiter{limiter};
373 auto reset = [weak_limiter] {
374 if (
auto shared_limiter{weak_limiter.lock()}) {
375 shared_limiter->Reset();
379 scheduler_func(reset, limiter->m_reset_window);
385 const std::string &str) {
388 m_source_locations.try_emplace(source_loc, m_max_bytes).first->second};
389 Status status{stats.m_dropped_bytes > 0 ? Status::STILL_SUPPRESSED
390 : Status::UNSUPPRESSED};
392 if (!stats.Consume(str.size()) && status == Status::UNSUPPRESSED) {
393 status = Status::NEWLY_SUPPRESSED;
394 m_suppression_active =
true;
402 const std::source_location &source_loc, std::string_view threadname,
403 SystemClock::time_point now, std::chrono::seconds mocktime)
const {
404 str.insert(0, GetLogPrefix(category, level));
406 if (m_log_sourcelocations) {
409 source_loc.line(), source_loc.function_name()));
412 if (m_log_threadnames) {
414 (threadname.empty() ?
"unknown" : threadname)));
417 str.insert(0, LogTimestampStr(now, mocktime));
421 std::source_location &&source_loc,
423 bool should_ratelimit) {
425 return LogPrintStr_(str, std::move(source_loc), category, level,
430 std::source_location &&source_loc,
432 bool should_ratelimit) {
435 const bool starts_new_line = m_started_new_line;
436 m_started_new_line = !str.empty() && str[str.size() - 1] ==
'\n';
439 if (!starts_new_line) {
440 if (!m_msgs_before_open.empty()) {
441 m_msgs_before_open.back().str += str_prefixed;
442 m_cur_buffer_memusage += str_prefixed.size();
446 str_prefixed.insert(0,
"[...] ");
452 .
now = SystemClock::now(),
456 .source_loc = std::move(source_loc),
457 .category = category,
460 m_cur_buffer_memusage +=
MemUsage(buf);
461 m_msgs_before_open.push_back(std::move(buf));
464 while (m_cur_buffer_memusage > m_max_buffer_memusage) {
465 if (m_msgs_before_open.empty()) {
466 m_cur_buffer_memusage = 0;
469 m_cur_buffer_memusage -=
MemUsage(m_msgs_before_open.front());
470 m_msgs_before_open.pop_front();
471 ++m_buffer_lines_discarded;
477 FormatLogStrInPlace(str_prefixed, category, level, source_loc,
480 bool ratelimit{
false};
481 if (should_ratelimit && m_limiter) {
482 auto status{m_limiter->Consume(source_loc, str_prefixed)};
489 "Excessive logging detected from %s:%d (%s): >%d bytes "
491 "the last time window of %is. Suppressing logging to disk "
493 "source location until time window resets. Console logging "
494 "unaffected. Last log entry.\n",
495 source_loc.file_name(), source_loc.line(),
496 source_loc.function_name(), m_limiter->m_max_bytes,
497 Ticks<std::chrono::seconds>(m_limiter->m_reset_window)),
508 if (m_limiter && m_limiter->SuppressionsActive()) {
509 str_prefixed.insert(0,
"[*] ");
512 if (m_print_to_console) {
514 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
517 for (
const auto &cb : m_print_callbacks) {
520 if (m_print_to_file && !ratelimit) {
521 assert(m_fileout !=
nullptr);
525 m_reopen_file =
false;
529 setbuf(m_fileout,
nullptr);
531 m_fileout = new_fileout;
540 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
542 assert(!m_file_path.empty());
550 log_size = fs::file_size(m_file_path);
551 }
catch (
const fs::filesystem_error &) {
556 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) {
558 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
559 if (fseek(file, -((
long)vch.size()), SEEK_END)) {
560 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
564 int nBytes = fread(vch.data(), 1, vch.size(), file);
569 fwrite(vch.data(), 1, nBytes, file);
572 }
else if (file !=
nullptr) {
578 m_categories |= category;
586 EnableCategory(flag);
591 m_categories &= ~category;
599 DisableCategory(flag);
607 LogPrintf(
"Error trying to log using a category mask instead of an "
608 "explicit category.\n");
612 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
623 if (!WillLogCategory(category)) {
628 const auto it{m_category_log_levels.find(category)};
630 (it == m_category_log_levels.end() ? LogLevel() : it->second);
638 decltype(m_source_locations) source_locations;
641 source_locations.swap(m_source_locations);
642 m_suppression_active =
false;
644 for (
const auto &[source_loc, stats] : source_locations) {
645 if (stats.m_dropped_bytes == 0) {
650 "Restarting logging from %s:%d (%s): %d bytes were "
651 "dropped during the last %ss.\n",
652 source_loc.file_name(), source_loc.line(),
653 source_loc.function_name(), stats.m_dropped_bytes,
654 Ticks<std::chrono::seconds>(m_reset_window));
659 if (bytes > m_available_bytes) {
660 m_dropped_bytes += bytes;
661 m_available_bytes = 0;
665 m_available_bytes -= bytes;
674 m_log_level = level.value();
679 std::string_view level_str) {
691 m_category_log_levels[flag] = level.value();
Fixed window rate limiter for logging.
static std::shared_ptr< LogRateLimiter > Create(SchedulerFunction &&scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window)
std::function< void(std::function< bool()>, std::chrono::milliseconds)> SchedulerFunction
LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window)
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...
Status
Suppression status of a source log location.
void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Resets all usage to zero. Called periodically by the scheduler.
static std::string LogLevelToStr(BCLog::Level level)
Returns the string representation of a log level.
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.
bool WillLogCategory(LogFlags category) const
Return true if log accepts specified category.
std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
bool DefaultShrinkDebugFile() const
Default for whether ShrinkDebugFile should be run.
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
void SetLogLevel(Level level)
bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
void DisableLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
This offers a slight speedup and slightly smaller memory usage compared to leaving the logging system...
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Start logging (and flush all buffered messages)
void DisableCategory(LogFlags category)
void EnableCategory(LogFlags category)
std::string GetLogPrefix(LogFlags category, Level level) const
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)
void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Only for testing.
std::string LogLevelsString() const
Returns a string with all user-selectable log levels.
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
static constexpr std::array< BCLog::Level, 3 > LogLevelsList()
Log severity levels that can be selected by the user.
static int FileWriteStr(std::string_view str, FILE *fp)
bool GetLogCategory(BCLog::LogFlags &flag, std::string_view str)
Return true if str parses as a log category and set the flag.
static size_t MemUsage(const BCLog::Logger::BufferedLog &buflog)
std::string LogCategoryToStr(BCLog::LogFlags category)
static const std::map< std::string, BCLog::LogFlags, std::less<> > LOG_CATEGORIES_BY_STR
BCLog::Logger & LogInstance()
static const std::unordered_map< BCLog::LogFlags, std::string > LOG_CATEGORIES_BY_FLAG
const char *const DEFAULT_DEBUGLOGFILE
static std::optional< BCLog::Level > GetLogLevel(std::string_view level_str)
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL
#define LogPrintLevel_(category, level, should_ratelimit,...)
static const bool DEFAULT_LOGIPS
std::string LogEscapeMessage(std::string_view str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
constexpr size_t DEFAULT_MAX_LOG_BUFFER
FILE * fopen(const fs::path &p, const char *mode)
bool StartLogging(const ArgsManager &args)
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
bool Consume(uint64_t bytes)
Updates internal accounting and returns true if enough available_bytes were remaining.
SystemClock::time_point now
std::chrono::seconds GetMockTime()
For testing.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t count_seconds(std::chrono::seconds t)