Skip to content

Commit 0b282f9

Browse files
ajtownsMarcoFalke
authored andcommitted
Log early messages with -printtoconsole
This ensures log messages prior to StartLogging() are replayed to the console as well as to the debug log file.
1 parent 4129874 commit 0b282f9

File tree

4 files changed

+49
-32
lines changed

4 files changed

+49
-32
lines changed

src/init.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -855,12 +855,6 @@ void InitLogging()
855855
{
856856
LogInstance().m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
857857
LogInstance().m_file_path = AbsPathForConfigVal(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
858-
859-
// Add newlines to the logfile to distinguish this execution from the last
860-
// one; called before console logging is set up, so this is only sent to
861-
// debug.log.
862-
LogPrintf("\n\n\n\n\n");
863-
864858
LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
865859
LogInstance().m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
866860
LogInstance().m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);

src/logging.cpp

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,35 @@ static int FileWriteStr(const std::string &str, FILE *fp)
4242
bool BCLog::Logger::StartLogging()
4343
{
4444
std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
45-
if (!m_print_to_file) return true;
4645

46+
assert(m_buffering);
4747
assert(m_fileout == nullptr);
48-
assert(!m_file_path.empty());
4948

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);
5361
}
5462

55-
setbuf(m_fileout, nullptr); // unbuffered
5663
// dump buffered messages from before we opened the log
64+
m_buffering = false;
5765
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+
5971
m_msgs_before_open.pop_front();
6072
}
73+
if (m_print_to_console) fflush(stdout);
6174

6275
return true;
6376
}
@@ -205,6 +218,7 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
205218

206219
void BCLog::Logger::LogPrintStr(const std::string &str)
207220
{
221+
std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
208222
std::string str_prefixed = str;
209223

210224
if (m_log_threadnames && m_started_new_line) {
@@ -215,32 +229,31 @@ void BCLog::Logger::LogPrintStr(const std::string &str)
215229

216230
m_started_new_line = !str.empty() && str[str.size()-1] == '\n';
217231

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+
218238
if (m_print_to_console) {
219239
// print to console
220240
fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
221241
fflush(stdout);
222242
}
223243
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;
241254
}
242-
FileWriteStr(str_prefixed, m_fileout);
243255
}
256+
FileWriteStr(str_prefixed, m_fileout);
244257
}
245258
}
246259

src/logging.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ namespace BCLog {
6363
FILE* m_fileout = nullptr;
6464
std::mutex m_file_mutex;
6565
std::list<std::string> m_msgs_before_open;
66+
bool m_buffering = true; //!< Buffer messages before logging can be started
6667

6768
/**
6869
* m_started_new_line is a state variable that will suppress printing of
@@ -91,9 +92,11 @@ namespace BCLog {
9192
void LogPrintStr(const std::string &str);
9293

9394
/** Returns whether logs will be written to any output */
94-
bool Enabled() const { return m_print_to_console || m_print_to_file; }
95+
bool Enabled() const { return m_buffering || m_print_to_console || m_print_to_file; }
9596

97+
/** Start logging (and flush all buffered messages) */
9698
bool StartLogging();
99+
97100
void ShrinkDebugFile();
98101

99102
uint32_t GetCategoryMask() const { return m_categories.load(); }

test/functional/feature_config_args.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,16 @@ def test_config_file_parser(self):
6161
with open(inc_conf_file2_path, 'w', encoding='utf-8') as conf:
6262
conf.write('') # clear
6363

64+
def test_log_buffer(self):
65+
with self.nodes[0].assert_debug_log(expected_msgs=['Warning: parsed potentially confusing double-negative -connect=0']):
66+
self.start_node(0, extra_args=['-noconnect=0'])
67+
self.stop_node(0)
68+
6469
def run_test(self):
6570
self.stop_node(0)
6671

72+
self.test_log_buffer()
73+
6774
self.test_config_file_parser()
6875

6976
# Remove the -datadir argument so it doesn't override the config file

0 commit comments

Comments
 (0)