Changeset View
Changeset View
Standalone View
Standalone View
src/logging.cpp
Show First 20 Lines • Show All 158 Lines • ▼ Show 20 Lines | if (!str.empty() && str[str.size() - 1] == '\n') { | ||||
m_started_new_line = true; | m_started_new_line = true; | ||||
} else { | } else { | ||||
m_started_new_line = false; | m_started_new_line = false; | ||||
} | } | ||||
return strStamped; | return strStamped; | ||||
} | } | ||||
int BCLog::Logger::LogPrintStr(const std::string &str) { | void BCLog::Logger::LogPrintStr(const std::string &str) { | ||||
// Returns total number of characters written. | |||||
int ret = 0; | |||||
std::string strTimestamped = LogTimestampStr(str); | std::string strTimestamped = LogTimestampStr(str); | ||||
if (m_print_to_console) { | if (m_print_to_console) { | ||||
// Print to console. | // Print to console. | ||||
ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); | fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); | ||||
fflush(stdout); | fflush(stdout); | ||||
} else if (m_print_to_file) { | } else if (m_print_to_file) { | ||||
std::lock_guard<std::mutex> scoped_lock(m_file_mutex); | std::lock_guard<std::mutex> scoped_lock(m_file_mutex); | ||||
// Buffer if we haven't opened the log yet. | // Buffer if we haven't opened the log yet. | ||||
if (m_fileout == nullptr) { | if (m_fileout == nullptr) { | ||||
ret = strTimestamped.length(); | |||||
m_msgs_before_open.push_back(strTimestamped); | m_msgs_before_open.push_back(strTimestamped); | ||||
} else { | } else { | ||||
// Reopen the log file, if requested. | // Reopen the log file, if requested. | ||||
if (m_reopen_file) { | if (m_reopen_file) { | ||||
m_reopen_file = false; | m_reopen_file = false; | ||||
fs::path pathDebug = GetDebugLogPath(); | fs::path pathDebug = GetDebugLogPath(); | ||||
if (fsbridge::freopen(pathDebug, "a", m_fileout) != nullptr) { | m_fileout = fsbridge::freopen(pathDebug, "a", m_fileout); | ||||
if (!m_fileout) { | |||||
return; | |||||
} | |||||
// unbuffered. | // unbuffered. | ||||
setbuf(m_fileout, nullptr); | setbuf(m_fileout, nullptr); | ||||
} | } | ||||
} | |||||
ret = FileWriteStr(strTimestamped, m_fileout); | FileWriteStr(strTimestamped, m_fileout); | ||||
} | } | ||||
} | } | ||||
return ret; | |||||
} | } | ||||
void BCLog::Logger::ShrinkDebugFile() { | void BCLog::Logger::ShrinkDebugFile() { | ||||
// Amount of debug.log to save at end when shrinking (must fit in memory) | // Amount of debug.log to save at end when shrinking (must fit in memory) | ||||
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; | constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; | ||||
// Scroll debug.log if it's getting too big. | // Scroll debug.log if it's getting too big. | ||||
fs::path pathLog = GetDebugLogPath(); | fs::path pathLog = GetDebugLogPath(); | ||||
FILE *file = fsbridge::fopen(pathLog, "r"); | FILE *file = fsbridge::fopen(pathLog, "r"); | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |