42 return fwrite(str.data(), 1, str.size(), fp);
50 assert(m_fileout ==
nullptr);
59 setbuf(m_fileout,
nullptr);
68 while (!m_msgs_before_open.empty()) {
69 const std::string& s = m_msgs_before_open.front();
73 for (
const auto& cb : m_print_callbacks) {
77 m_msgs_before_open.pop_front();
88 if (m_fileout !=
nullptr) fclose(m_fileout);
90 m_print_callbacks.clear();
102 EnableCategory(flag);
108 m_categories &= ~flag;
115 DisableCategory(flag);
121 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
176 if (category_desc.category == str) {
177 flag = category_desc.flag;
189 std::sort(categories.begin(), categories.end(), [](
auto a,
auto b) { return a.category < b.category; });
191 std::vector<LogCategory> ret;
195 catActive.
category = category_desc.category;
196 catActive.
active = WillLogCategory(category_desc.flag);
197 ret.push_back(catActive);
204 std::string strStamped;
206 if (!m_log_timestamps)
209 if (m_started_new_line) {
212 if (m_log_time_micros) {
213 strStamped.pop_back();
214 strStamped +=
strprintf(
".%06dZ", nTimeMicros%1000000);
220 strStamped +=
' ' + str;
237 for (
char ch_in : str) {
238 uint8_t ch = (uint8_t)ch_in;
239 if ((ch >= 32 || ch ==
'\n') && ch !=
'\x7f') {
249void BCLog::Logger::LogPrintStr(
const std::string& str,
const std::string& logging_function,
const std::string& source_file,
const int source_line)
254 if (m_log_sourcelocations && m_started_new_line) {
255 str_prefixed.insert(0,
"[" +
RemovePrefix(source_file,
"./") +
":" +
ToString(source_line) +
"] [" + logging_function +
"] ");
258 if (m_log_threadnames && m_started_new_line) {
262 str_prefixed = LogTimestampStr(str_prefixed);
264 m_started_new_line = !str.empty() && str[str.size()-1] ==
'\n';
268 m_msgs_before_open.push_back(str_prefixed);
272 if (m_print_to_console) {
274 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
277 for (
const auto& cb : m_print_callbacks) {
280 if (m_print_to_file) {
281 assert(m_fileout !=
nullptr);
285 m_reopen_file =
false;
288 setbuf(new_fileout,
nullptr);
290 m_fileout = new_fileout;
300 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
302 assert(!m_file_path.empty());
310 log_size = fs::file_size(m_file_path);
311 }
catch (
const fs::filesystem_error&) {}
315 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
318 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
319 if (fseek(file, -((
long)vch.size()), SEEK_END)) {
320 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
324 int nBytes = fread(vch.data(), 1, vch.size(), file);
330 fwrite(vch.data(), 1, nBytes, file);
334 else if (file !=
nullptr)
bool WillLogCategory(LogFlags category) const
std::string LogTimestampStr(const std::string &str)
void DisconnectTestLogger()
Only for testing.
bool DefaultShrinkDebugFile() const
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)
void LogPrintStr(const std::string &str, const std::string &logging_function, const std::string &source_file, const int source_line)
Send a string to the log output.
void DisableCategory(LogFlags flag)
const CLogCategoryDesc LogCategories[]
static int FileWriteStr(const std::string &str, FILE *fp)
BCLog::Logger & LogInstance()
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 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(const std::string &str, const std::string &prefix)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
int64_t GetTimeMicros()
Returns the system time (not mockable)
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)
Helper to count the seconds of a duration.