Skip to content

Commit 6a3b0d3

Browse files
eklitzkepracticalswift
authored andcommitted
Print to console by default when not run with -daemon
Printing to the debug log file can be disabled with -nodebulogfile
1 parent 0782508 commit 6a3b0d3

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
@@ -815,14 +815,25 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
815815
return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
816816
}
817817

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

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

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)