| 43 | } |
| 44 | |
| 45 | bool BCLog::Logger::StartLogging() |
| 46 | { |
| 47 | std::lock_guard<std::mutex> scoped_lock(m_cs); |
| 48 | |
| 49 | assert(m_buffering); |
| 50 | assert(m_fileout == nullptr); |
| 51 | |
| 52 | if (m_print_to_file) { |
| 53 | assert(!m_file_path.empty()); |
| 54 | m_fileout = fsbridge::fopen(m_file_path, "a"); |
| 55 | if (!m_fileout) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | setbuf(m_fileout, nullptr); // unbuffered |
| 60 | |
| 61 | // Add newlines to the logfile to distinguish this execution from the |
| 62 | // last one. |
| 63 | FileWriteStr("\n\n\n\n\n", m_fileout); |
| 64 | } |
| 65 | |
| 66 | // dump buffered messages from before we opened the log |
| 67 | m_buffering = false; |
| 68 | while (!m_msgs_before_open.empty()) { |
| 69 | const std::string& s = m_msgs_before_open.front(); |
| 70 | |
| 71 | if (m_print_to_file) FileWriteStr(s, m_fileout); |
| 72 | if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout); |
| 73 | for (const auto& cb : m_print_callbacks) { |
| 74 | cb(s); |
| 75 | } |
| 76 | |
| 77 | m_msgs_before_open.pop_front(); |
| 78 | } |
| 79 | if (m_print_to_console) fflush(stdout); |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | void BCLog::Logger::Flush() { |
| 85 | if (Enabled()) { |
no test coverage detected