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
196 if (category_desc.category == str) {
197 flag = category_desc.flag;
229 case BCLog::LogFlags::NET:
239 case BCLog::LogFlags::ZMQ:
243 case BCLog::LogFlags::RPC:
246 return "estimatefee";
250 return "selectcoins";
277 #ifdef DEBUG_LOCKCONTENTION
284 return "blockstorage";
286 return "txreconciliation";
295 static std::optional<BCLog::Level>
GetLogLevel(
const std::string& level_str)
297 if (level_str ==
"trace") {
299 }
else if (level_str ==
"debug") {
301 }
else if (level_str ==
"info") {
303 }
else if (level_str ==
"warning") {
305 }
else if (level_str ==
"error") {
307 }
else if (level_str ==
"none") {
319 std::sort(categories.begin(), categories.end(), [](
auto a,
auto b) { return a.category < b.category; });
321 std::vector<LogCategory>
ret;
325 catActive.
category = category_desc.category;
326 catActive.
active = WillLogCategory(category_desc.flag);
327 ret.push_back(catActive);
341 return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()},
", ", [
this](
BCLog::Level level) {
return LogLevelToStr(level); });
346 std::string strStamped;
348 if (!m_log_timestamps)
351 if (m_started_new_line) {
352 const auto now{SystemClock::now()};
353 const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
355 if (m_log_time_micros) {
356 strStamped.pop_back();
357 strStamped +=
strprintf(
".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
363 strStamped +=
' ' + str;
380 for (
char ch_in : str) {
381 uint8_t ch = (uint8_t)ch_in;
382 if ((ch >= 32 || ch ==
'\n') && ch !=
'\x7f') {
410 s += LogLevelToStr(level);
414 str_prefixed.insert(0, s);
417 if (m_log_sourcelocations && m_started_new_line) {
418 str_prefixed.insert(0,
"[" +
RemovePrefix(source_file,
"./") +
":" +
ToString(source_line) +
"] [" + logging_function +
"] ");
421 if (m_log_threadnames && m_started_new_line) {
423 str_prefixed.insert(0,
"[" + (threadname.empty() ?
"unknown" : threadname) +
"] ");
426 str_prefixed = LogTimestampStr(str_prefixed);
428 m_started_new_line = !str.empty() && str[str.size()-1] ==
'\n';
432 m_msgs_before_open.push_back(str_prefixed);
436 if (m_print_to_console) {
438 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
441 for (
const auto& cb : m_print_callbacks) {
444 if (m_print_to_file) {
445 assert(m_fileout !=
nullptr);
449 m_reopen_file =
false;
452 setbuf(new_fileout,
nullptr);
454 m_fileout = new_fileout;
464 constexpr
size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
466 assert(!m_file_path.empty());
474 log_size = fs::file_size(m_file_path);
475 }
catch (
const fs::filesystem_error&) {}
479 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
482 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
483 if (fseek(file, -((
long)vch.size()), SEEK_END)) {
484 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
488 int nBytes = fread(vch.data(), 1, vch.size(), file);
494 fwrite(vch.data(), 1, nBytes, file);
498 else if (file !=
nullptr)
506 m_log_level = level.value();
519 m_category_log_levels[flag] = level.value();
bool WillLogCategory(LogFlags category) const
std::string LogTimestampStr(const std::string &str)
void DisconnectTestLogger()
Only for testing.
bool DefaultShrinkDebugFile() const
void SetLogLevel(Level level)
bool WillLogCategoryLevel(LogFlags category, Level level) const
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.
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
void EnableCategory(LogFlags flag)
bool StartLogging()
Start logging (and flush all buffered messages)
std::string LogLevelToStr(BCLog::Level level) const
Returns the string representation of a log level.
std::string LogLevelsString() const
Returns a string with all user-selectable log levels.
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels)
void DisableCategory(LogFlags flag)
const CLogCategoryDesc LogCategories[]
static constexpr std::array< BCLog::Level, 3 > LogLevelsList()
Log severity levels that can be selected by the user.
std::string LogCategoryToStr(BCLog::LogFlags category)
static int FileWriteStr(const std::string &str, FILE *fp)
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
const char *const DEFAULT_DEBUGLOGFILE
static std::optional< BCLog::Level > GetLogLevel(const std::string &level_str)
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL
BCLog::Logger & LogInstance()
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...
FILE * fopen(const fs::path &p, const char *mode)
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
std::string RemovePrefix(std::string_view str, std::string_view prefix)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
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)