39 return fwrite(str.data(), 1, str.size(), fp);
47 assert(m_fileout ==
nullptr);
56 setbuf(m_fileout,
nullptr);
65 while (!m_msgs_before_open.empty()) {
66 const std::string& s = m_msgs_before_open.front();
70 for (
const auto& cb : m_print_callbacks) {
74 m_msgs_before_open.pop_front();
85 if (m_fileout !=
nullptr) fclose(m_fileout);
87 m_print_callbacks.clear();
105 m_categories &= ~flag;
112 DisableCategory(flag);
118 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
169 if (category_desc.category == str) {
170 flag = category_desc.flag;
179 std::vector<LogCategory> ret;
184 catActive.
category = category_desc.category;
185 catActive.
active = WillLogCategory(category_desc.flag);
186 ret.push_back(catActive);
194 std::string strStamped;
196 if (!m_log_timestamps)
199 if (m_started_new_line) {
202 if (m_log_time_micros) {
203 strStamped.pop_back();
204 strStamped +=
strprintf(
".%06dZ", nTimeMicros%1000000);
210 strStamped +=
' ' + str;
227 for (
char ch_in : str) {
228 uint8_t ch = (uint8_t)ch_in;
229 if ((ch >= 32 || ch ==
'\n') && ch !=
'\x7f') {
244 if (m_log_threadnames && m_started_new_line) {
248 str_prefixed = LogTimestampStr(str_prefixed);
250 m_started_new_line = !str.empty() && str[str.size()-1] ==
'\n';
254 m_msgs_before_open.push_back(str_prefixed);
258 if (m_print_to_console) {
260 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
263 for (
const auto& cb : m_print_callbacks) {
266 if (m_print_to_file) {
267 assert(m_fileout !=
nullptr);
271 m_reopen_file =
false;
274 setbuf(new_fileout,
nullptr);
276 m_fileout = new_fileout;
286 constexpr
size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
288 assert(!m_file_path.empty());
296 log_size = fs::file_size(m_file_path);
297 }
catch (
const fs::filesystem_error&) {}
301 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
304 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
305 if (fseek(file, -((
long)vch.size()), SEEK_END)) {
306 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
310 int nBytes = fread(vch.data(), 1, vch.size(), file);
316 fwrite(vch.data(), 1, nBytes, file);
320 else if (file !=
nullptr)
void EnableCategory(LogFlags flag)
BCLog::Logger & LogInstance()
FILE * fopen(const fs::path &p, const char *mode)
std::vector< LogCategory > LogCategoriesList()
Returns a vector of the log categories.
static void LogPrintf(const char *fmt, const Args &...args)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
void LogPrintStr(const std::string &str)
Send a string to the log output.
int64_t GetMockTime()
For testing.
void DisableCategory(LogFlags flag)
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.
int64_t GetTimeMicros()
Returns the system time (not mockable)
bool WillLogCategory(LogFlags category) const
bool DefaultShrinkDebugFile() const
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
static int FileWriteStr(const std::string &str, FILE *fp)
const CLogCategoryDesc LogCategories[]
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)