36 return fwrite(str.data(), 1, str.size(), fp);
43 assert(m_fileout ==
nullptr);
53 setbuf(m_fileout,
nullptr);
62 while (!m_msgs_before_open.empty()) {
63 const std::string &s = m_msgs_before_open.front();
69 fwrite(s.data(), 1, s.size(), stdout);
71 for (
const auto &cb : m_print_callbacks) {
75 m_msgs_before_open.pop_front();
87 if (m_fileout !=
nullptr) {
91 m_print_callbacks.clear();
134 if (category_desc.category == str) {
135 flag = category_desc.flag;
143 std::vector<LogCategory> ret;
149 catActive.
category = category_desc.category;
151 ret.push_back(catActive);
164 std::string strStamped;
174 strStamped.pop_back();
175 strStamped +=
strprintf(
".%06dZ", nTimeMicros % 1000000);
182 strStamped +=
' ' + str;
200 for (
char ch_in : str) {
201 uint8_t ch = (uint8_t)ch_in;
202 if ((ch >= 32 || ch ==
'\n') && ch !=
'\x7f') {
226 m_msgs_before_open.push_back(str_prefixed);
232 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
235 for (
const auto &cb : m_print_callbacks) {
239 assert(m_fileout !=
nullptr);
247 setbuf(m_fileout,
nullptr);
249 m_fileout = new_fileout;
258 constexpr
size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
269 }
catch (
const fs::filesystem_error &) {
274 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) {
276 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
277 if (fseek(file, -((
long)vch.size()), SEEK_END)) {
278 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
282 int nBytes = fread(vch.data(), 1, vch.size(), file);
287 fwrite(vch.data(), 1, nBytes, file);
290 }
else if (file !=
nullptr) {
325 LogPrintf(
"Error trying to log using a category mask instead of an " 326 "explicit category.\n");
330 return (
m_categories.load(std::memory_order_relaxed) & category) != 0;
BCLog::Logger & LogInstance()
FILE * fopen(const fs::path &p, const char *mode)
void EnableCategory(LogFlags category)
std::vector< LogCategory > LogCategoriesList()
Returns a vector of the log categories.
std::atomic< bool > m_reopen_file
static void LogPrintf(const char *fmt, const Args &... args)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
void DisableCategory(LogFlags category)
void LogPrintStr(const std::string &str)
Send a string to the log output.
int64_t GetMockTime()
For testing.
bool WillLogCategory(LogFlags category) const
Return true if log accepts specified category.
std::atomic< uint32_t > m_categories
Log categories bitfield.
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)
std::atomic_bool m_started_new_line
m_started_new_line is a state variable that will suppress printing of the timestamp when multiple cal...
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)
bool DefaultShrinkDebugFile() const
Default for whether ShrinkDebugFile should be run.