45 return fwrite(str.data(), 1, str.size(), fp);
53 assert(m_fileout ==
nullptr);
62 setbuf(m_fileout,
nullptr);
71 while (!m_msgs_before_open.empty()) {
72 const std::string& s = m_msgs_before_open.front();
76 for (
const auto& cb : m_print_callbacks) {
80 m_msgs_before_open.pop_front();
91 if (m_fileout !=
nullptr) fclose(m_fileout);
93 m_print_callbacks.clear();
105 EnableCategory(flag);
111 m_categories &= ~flag;
118 DisableCategory(flag);
124 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
133 if (!WillLogCategory(category))
return false;
136 const auto it{m_category_log_levels.find(category)};
137 return level >= (it == m_category_log_levels.end() ? LogLevel() : it->second);
178 #ifdef DEBUG_LOCKCONTENTION 197 if (category_desc.category == str) {
198 flag = category_desc.flag;
240 case BCLog::LogFlags::ZMQ:
244 case BCLog::LogFlags::RPC:
247 return "estimatefee";
251 return "selectcoins";
278 #ifdef DEBUG_LOCKCONTENTION 285 return "blockstorage";
287 return "txreconciliation";
298 static std::optional<BCLog::Level>
GetLogLevel(
const std::string& level_str)
300 if (level_str ==
"trace") {
302 }
else if (level_str ==
"debug") {
304 }
else if (level_str ==
"info") {
306 }
else if (level_str ==
"warning") {
308 }
else if (level_str ==
"error") {
310 }
else if (level_str ==
"none") {
320 std::array<CLogCategoryDesc, std::size(LogCategories)> categories;
322 std::sort(categories.begin(), categories.end(), [](
auto a,
auto b) {
return a.category < b.category; });
324 std::vector<LogCategory>
ret;
328 catActive.
category = category_desc.category;
329 catActive.
active = WillLogCategory(category_desc.flag);
330 ret.push_back(catActive);
344 return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()},
", ", [
this](
BCLog::Level level) {
return LogLevelToStr(level); });
349 std::string strStamped;
351 if (!m_log_timestamps)
354 if (m_started_new_line) {
355 const auto now{SystemClock::now()};
356 const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
358 if (m_log_time_micros && !strStamped.empty()) {
359 strStamped.pop_back();
360 strStamped +=
strprintf(
".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
366 strStamped +=
' ' + str;
383 for (
char ch_in : str) {
384 uint8_t ch = (uint8_t)ch_in;
385 if ((ch >= 32 || ch ==
'\n') && ch !=
'\x7f') {
413 s += LogLevelToStr(level);
417 str_prefixed.insert(0, s);
420 if (m_log_sourcelocations && m_started_new_line) {
421 str_prefixed.insert(0,
"[" +
RemovePrefix(source_file,
"./") +
":" +
ToString(source_line) +
"] [" + logging_function +
"] ");
424 if (m_log_threadnames && m_started_new_line) {
426 str_prefixed.insert(0,
"[" + (threadname.empty() ?
"unknown" : threadname) +
"] ");
429 str_prefixed = LogTimestampStr(str_prefixed);
431 m_started_new_line = !str.empty() && str[str.size()-1] ==
'\n';
435 m_msgs_before_open.push_back(str_prefixed);
439 if (m_print_to_console) {
441 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
444 for (
const auto& cb : m_print_callbacks) {
447 if (m_print_to_file) {
448 assert(m_fileout !=
nullptr);
452 m_reopen_file =
false;
455 setbuf(new_fileout,
nullptr);
457 m_fileout = new_fileout;
467 constexpr
size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
469 assert(!m_file_path.empty());
477 log_size = fs::file_size(m_file_path);
478 }
catch (
const fs::filesystem_error&) {}
482 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
485 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
486 if (fseek(file, -((
long)vch.size()), SEEK_END)) {
487 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
491 int nBytes = fread(vch.data(), 1, vch.size(), file);
497 fwrite(vch.data(), 1, nBytes, file);
501 else if (file !=
nullptr)
509 m_log_level = level.value();
522 m_category_log_levels[flag] = level.value();
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)
Send a string to the log output.
void EnableCategory(LogFlags flag)
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL
BCLog::Logger & LogInstance()
std::chrono::seconds GetMockTime()
For testing.
FILE * fopen(const fs::path &p, const char *mode)
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels)
std::string LogLevelToStr(BCLog::Level level) const
Returns the string representation of a log level.
static constexpr std::array< BCLog::Level, 3 > LogLevelsList()
Log severity levels that can be selected by the user.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
std::string LogCategoryToStr(BCLog::LogFlags category)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
static std::optional< BCLog::Level > GetLogLevel(const std::string &level_str)
void DisableCategory(LogFlags flag)
void SetLogLevel(Level level)
bool WillLogCategory(LogFlags category) const
constexpr int64_t count_seconds(std::chrono::seconds t)
void DisconnectTestLogger()
Only for testing.
bool StartLogging()
Start logging (and flush all buffered messages)
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
static int FileWriteStr(const std::string &str, FILE *fp)
const CLogCategoryDesc LogCategories[]
std::string LogLevelsString() const
Returns a string with all user-selectable log levels.
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
std::string RemovePrefix(std::string_view str, std::string_view prefix)
static const bool DEFAULT_LOGIPS
std::string LogEscapeMessage(const std::string &str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
const char *const DEFAULT_DEBUGLOGFILE
std::string LogTimestampStr(const std::string &str)
bool WillLogCategoryLevel(LogFlags category, Level level) const
bool DefaultShrinkDebugFile() const