@@ -42,22 +42,35 @@ static int FileWriteStr(const std::string &str, FILE *fp)
42
42
bool BCLog::Logger::StartLogging ()
43
43
{
44
44
std::lock_guard<std::mutex> scoped_lock (m_file_mutex);
45
- if (!m_print_to_file) return true ;
46
45
46
+ assert (m_buffering);
47
47
assert (m_fileout == nullptr );
48
- assert (!m_file_path.empty ());
49
48
50
- m_fileout = fsbridge::fopen (m_file_path, " a" );
51
- if (!m_fileout) {
52
- return false ;
49
+ if (m_print_to_file) {
50
+ assert (!m_file_path.empty ());
51
+ m_fileout = fsbridge::fopen (m_file_path, " a" );
52
+ if (!m_fileout) {
53
+ return false ;
54
+ }
55
+
56
+ setbuf (m_fileout, nullptr ); // unbuffered
57
+
58
+ // Add newlines to the logfile to distinguish this execution from the
59
+ // last one.
60
+ FileWriteStr (" \n\n\n\n\n " , m_fileout);
53
61
}
54
62
55
- setbuf (m_fileout, nullptr ); // unbuffered
56
63
// dump buffered messages from before we opened the log
64
+ m_buffering = false ;
57
65
while (!m_msgs_before_open.empty ()) {
58
- FileWriteStr (m_msgs_before_open.front (), m_fileout);
66
+ const std::string& s = m_msgs_before_open.front ();
67
+
68
+ if (m_print_to_file) FileWriteStr (s, m_fileout);
69
+ if (m_print_to_console) fwrite (s.data (), 1 , s.size (), stdout);
70
+
59
71
m_msgs_before_open.pop_front ();
60
72
}
73
+ if (m_print_to_console) fflush (stdout);
61
74
62
75
return true ;
63
76
}
@@ -205,6 +218,7 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
205
218
206
219
void BCLog::Logger::LogPrintStr (const std::string &str)
207
220
{
221
+ std::lock_guard<std::mutex> scoped_lock (m_file_mutex);
208
222
std::string str_prefixed = str;
209
223
210
224
if (m_log_threadnames && m_started_new_line) {
@@ -215,32 +229,31 @@ void BCLog::Logger::LogPrintStr(const std::string &str)
215
229
216
230
m_started_new_line = !str.empty () && str[str.size ()-1 ] == ' \n ' ;
217
231
232
+ if (m_buffering) {
233
+ // buffer if we haven't started logging yet
234
+ m_msgs_before_open.push_back (str_prefixed);
235
+ return ;
236
+ }
237
+
218
238
if (m_print_to_console) {
219
239
// print to console
220
240
fwrite (str_prefixed.data (), 1 , str_prefixed.size (), stdout);
221
241
fflush (stdout);
222
242
}
223
243
if (m_print_to_file) {
224
- std::lock_guard<std::mutex> scoped_lock (m_file_mutex);
225
-
226
- // buffer if we haven't opened the log yet
227
- if (m_fileout == nullptr ) {
228
- m_msgs_before_open.push_back (str_prefixed);
229
- }
230
- else
231
- {
232
- // reopen the log file, if requested
233
- if (m_reopen_file) {
234
- m_reopen_file = false ;
235
- FILE* new_fileout = fsbridge::fopen (m_file_path, " a" );
236
- if (new_fileout) {
237
- setbuf (new_fileout, nullptr ); // unbuffered
238
- fclose (m_fileout);
239
- m_fileout = new_fileout;
240
- }
244
+ assert (m_fileout != nullptr );
245
+
246
+ // reopen the log file, if requested
247
+ if (m_reopen_file) {
248
+ m_reopen_file = false ;
249
+ FILE* new_fileout = fsbridge::fopen (m_file_path, " a" );
250
+ if (new_fileout) {
251
+ setbuf (new_fileout, nullptr ); // unbuffered
252
+ fclose (m_fileout);
253
+ m_fileout = new_fileout;
241
254
}
242
- FileWriteStr (str_prefixed, m_fileout);
243
255
}
256
+ FileWriteStr (str_prefixed, m_fileout);
244
257
}
245
258
}
246
259
0 commit comments