Skip to content

Commit 58bbc55

Browse files
committed
Merge #13004: Print to console by default when not run with -daemon
6a3b0d3 Print to console by default when not run with -daemon (Evan Klitzke) Pull request description: Cherry-picked ef6fa1c38e1bd115d1cce155907023d79da379d8 from the "up for grabs" PR: "Smarter default behavior for -printtoconsole" (#12689). See previous review in #12689. Tree-SHA512: 8923a89b9c8973286d53e960d3c464b1cd026cd5a5911ba62f9f972c83684417dc4004101815dfe987fc1e1baaec1fdd90748a0866bb5548e974d77b3135d43b
2 parents 39e0c65 + 6a3b0d3 commit 58bbc55

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

src/init.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,25 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
814814
return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
815815
}
816816

817+
/**
818+
* Initialize global loggers.
819+
*
820+
* Note that this is called very early in the process lifetime, so you should be
821+
* careful about what global state you rely on here.
822+
*/
817823
void InitLogging()
818824
{
819-
fPrintToConsole = gArgs.GetBoolArg("-printtoconsole", false);
825+
// Add newlines to the logfile to distinguish this execution from the last
826+
// one; called before console logging is set up, so this is only sent to
827+
// debug.log.
828+
LogPrintf("\n\n\n\n\n");
829+
830+
fPrintToConsole = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
831+
fPrintToDebugLog = !gArgs.IsArgNegated("-debuglogfile");
820832
fLogTimestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
821833
fLogTimeMicros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
822834
fLogIPs = gArgs.GetBoolArg("-logips", DEFAULT_LOGIPS);
823835

824-
LogPrintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
825836
std::string version_string = FormatFullVersion();
826837
#ifdef DEBUG
827838
version_string += " (debug build)";
@@ -1215,13 +1226,12 @@ bool AppInitMain()
12151226
#ifndef WIN32
12161227
CreatePidFile(GetPidFile(), getpid());
12171228
#endif
1218-
if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) {
1219-
// Do this first since it both loads a bunch of debug.log into memory,
1220-
// and because this needs to happen before any other debug.log printing
1221-
ShrinkDebugFile();
1222-
}
1223-
12241229
if (fPrintToDebugLog) {
1230+
if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) {
1231+
// Do this first since it both loads a bunch of debug.log into memory,
1232+
// and because this needs to happen before any other debug.log printing
1233+
ShrinkDebugFile();
1234+
}
12251235
if (!OpenDebugLog()) {
12261236
return InitError(strprintf("Could not open debug log file %s", GetDebugLogPath().string()));
12271237
}

src/util.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,12 @@ int LogPrintStr(const std::string &str)
341341

342342
std::string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
343343

344-
if (fPrintToConsole)
345-
{
344+
if (fPrintToConsole) {
346345
// print to console
347346
ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
348347
fflush(stdout);
349348
}
350-
else if (fPrintToDebugLog)
351-
{
349+
if (fPrintToDebugLog) {
352350
std::call_once(debugPrintInitFlag, &DebugPrintInit);
353351
std::lock_guard<std::mutex> scoped_lock(*mutexDebugLog);
354352

@@ -1126,9 +1124,16 @@ void ShrinkDebugFile()
11261124
// Scroll debug.log if it's getting too big
11271125
fs::path pathLog = GetDebugLogPath();
11281126
FILE* file = fsbridge::fopen(pathLog, "r");
1127+
1128+
// Special files (e.g. device nodes) may not have a size.
1129+
size_t log_size = 0;
1130+
try {
1131+
log_size = fs::file_size(pathLog);
1132+
} catch (boost::filesystem::filesystem_error &) {}
1133+
11291134
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
11301135
// trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
1131-
if (file && fs::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
1136+
if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
11321137
{
11331138
// Restart the file with some of the end
11341139
std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);

test/functional/feature_logging.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@ def set_test_params(self):
1515
self.num_nodes = 1
1616
self.setup_clean_chain = True
1717

18+
def relative_log_path(self, name):
19+
return os.path.join(self.nodes[0].datadir, "regtest", name)
20+
1821
def run_test(self):
1922
# test default log file name
20-
assert os.path.isfile(os.path.join(self.nodes[0].datadir, "regtest", "debug.log"))
23+
default_log_path = self.relative_log_path("debug.log")
24+
assert os.path.isfile(default_log_path)
2125

2226
# test alternative log file name in datadir
2327
self.restart_node(0, ["-debuglogfile=foo.log"])
24-
assert os.path.isfile(os.path.join(self.nodes[0].datadir, "regtest", "foo.log"))
28+
assert os.path.isfile(self.relative_log_path("foo.log"))
2529

2630
# test alternative log file name outside datadir
2731
tempname = os.path.join(self.options.tmpdir, "foo.log")
2832
self.restart_node(0, ["-debuglogfile=%s" % tempname])
2933
assert os.path.isfile(tempname)
3034

3135
# check that invalid log (relative) will cause error
32-
invdir = os.path.join(self.nodes[0].datadir, "regtest", "foo")
36+
invdir = self.relative_log_path("foo")
3337
invalidname = os.path.join("foo", "foo.log")
3438
self.stop_node(0)
3539
exp_stderr = "Error: Could not open debug log file \S+$"
@@ -55,6 +59,17 @@ def run_test(self):
5559
self.start_node(0, ["-debuglogfile=%s" % (invalidname)])
5660
assert os.path.isfile(os.path.join(invdir, "foo.log"))
5761

62+
# check that -nodebuglogfile disables logging
63+
self.stop_node(0)
64+
os.unlink(default_log_path)
65+
assert not os.path.isfile(default_log_path)
66+
self.start_node(0, ["-nodebuglogfile"])
67+
assert not os.path.isfile(default_log_path)
68+
69+
# just sanity check no crash here
70+
self.stop_node(0)
71+
self.start_node(0, ["-debuglogfile=%s" % os.devnull])
72+
5873

5974
if __name__ == '__main__':
6075
LoggingTest().main()

test/functional/test_framework/test_node.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ def __init__(self, i, datadir, rpchost, timewait, binary, stderr, mocktime, cove
7878
# For those callers that need more flexibility, they can just set the args property directly.
7979
# Note that common args are set in the config file (see initialize_datadir)
8080
self.extra_args = extra_args
81-
self.args = [self.binary, "-datadir=" + self.datadir, "-logtimemicros", "-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=testnode%d" % i]
81+
self.args = [
82+
self.binary,
83+
"-datadir=" + self.datadir,
84+
"-logtimemicros",
85+
"-debug",
86+
"-debugexclude=libevent",
87+
"-debugexclude=leveldb",
88+
"-mocktime=" + str(mocktime),
89+
"-uacomment=testnode%d" % i,
90+
"-noprinttoconsole"
91+
]
8292

8393
self.cli = TestNodeCLI(os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir)
8494
self.use_cli = use_cli

0 commit comments

Comments
 (0)