37 return fwrite(str.data(), 1, str.size(), fp);
44 assert(m_fileout ==
nullptr);
54 setbuf(m_fileout,
nullptr);
63 while (!m_msgs_before_open.empty()) {
64 const std::string &s = m_msgs_before_open.front();
70 fwrite(s.data(), 1, s.size(), stdout);
72 for (
const auto &cb : m_print_callbacks) {
76 m_msgs_before_open.pop_front();
88 if (m_fileout !=
nullptr) {
92 m_print_callbacks.clear();
137 if (category_desc.category == str) {
138 flag = category_desc.flag;
146 std::vector<LogCategory> ret;
152 catActive.
category = category_desc.category;
153 catActive.
active = WillLogCategory(category_desc.flag);
154 ret.push_back(catActive);
167 std::string strStamped;
169 if (!m_log_timestamps) {
173 if (m_started_new_line) {
176 if (m_log_time_micros) {
177 strStamped.pop_back();
178 strStamped +=
strprintf(
".%06dZ", nTimeMicros % 1000000);
185 strStamped +=
' ' + str;
203 for (
char ch_in : str) {
204 uint8_t ch = (uint8_t)ch_in;
205 if ((ch >= 32 || ch ==
'\n') && ch !=
'\x7f') {
216 const std::string &logging_function,
217 const std::string &source_file,
218 const int source_line) {
222 if (m_log_sourcelocations && m_started_new_line) {
223 str_prefixed.insert(0,
"[" +
RemovePrefix(source_file,
"./") +
":" +
225 logging_function +
"] ");
228 if (m_log_threadnames && m_started_new_line) {
232 str_prefixed = LogTimestampStr(str_prefixed);
234 m_started_new_line = !str.empty() && str[str.size() - 1] ==
'\n';
238 m_msgs_before_open.push_back(str_prefixed);
242 if (m_print_to_console) {
244 fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
247 for (
const auto &cb : m_print_callbacks) {
250 if (m_print_to_file) {
251 assert(m_fileout !=
nullptr);
255 m_reopen_file =
false;
259 setbuf(m_fileout,
nullptr);
261 m_fileout = new_fileout;
270 constexpr
size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
272 assert(!m_file_path.empty());
280 log_size = fs::file_size(m_file_path);
281 }
catch (
const fs::filesystem_error &) {
286 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) {
288 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
289 if (fseek(file, -((
long)vch.size()), SEEK_END)) {
290 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
294 int nBytes = fread(vch.data(), 1, vch.size(), file);
299 fwrite(vch.data(), 1, nBytes, file);
302 }
else if (file !=
nullptr) {
308 m_categories |= category;
316 EnableCategory(flag);
321 m_categories &= ~category;
329 DisableCategory(flag);
337 LogPrintf(
"Error trying to log using a category mask instead of an "
338 "explicit category.\n");
342 return (m_categories.load(std::memory_order_relaxed) & category) != 0;