| 52 | } |
| 53 | |
| 54 | bool BCLog::Logger::StartLogging() |
| 55 | { |
| 56 | STDLOCK(m_cs); |
| 57 | |
| 58 | assert(m_buffering); |
| 59 | assert(m_fileout == nullptr); |
| 60 | |
| 61 | if (m_print_to_file) { |
| 62 | assert(!m_file_path.empty()); |
| 63 | m_fileout = fsbridge::fopen(m_file_path, "a"); |
| 64 | if (!m_fileout) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | setbuf(m_fileout, nullptr); // unbuffered |
| 69 | |
| 70 | // Add newlines to the logfile to distinguish this execution from the |
| 71 | // last one. |
| 72 | FileWriteStr("\n\n\n\n\n", m_fileout); |
| 73 | } |
| 74 | |
| 75 | // dump buffered messages from before we opened the log |
| 76 | m_buffering = false; |
| 77 | if (m_buffer_lines_discarded > 0) { |
| 78 | LogPrint_({ |
| 79 | .category = BCLog::ALL, |
| 80 | .level = Level::Info, |
| 81 | .should_ratelimit = false, |
| 82 | .source_loc = SourceLocation{__func__}, |
| 83 | .message = strprintf("Early logging buffer overflowed, %d log lines discarded.", m_buffer_lines_discarded), |
| 84 | }); |
| 85 | } |
| 86 | while (!m_msgs_before_open.empty()) { |
| 87 | const auto& buflog = m_msgs_before_open.front(); |
| 88 | std::string s{Format(buflog)}; |
| 89 | m_msgs_before_open.pop_front(); |
| 90 | |
| 91 | if (m_print_to_file) FileWriteStr(s, m_fileout); |
| 92 | if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout); |
| 93 | for (const auto& cb : m_print_callbacks) { |
| 94 | cb(s); |
| 95 | } |
| 96 | } |
| 97 | m_cur_buffer_memusage = 0; |
| 98 | if (m_print_to_console) fflush(stdout); |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | void BCLog::Logger::DisconnectTestLogger() |
| 104 | { |