Skip to content

Commit b26dbc4

Browse files
committed
Merge #1455: [Init] Add -debuglogfile option
882ef90 [Doc] Update release notes for `-debuglogfile` (random-zebra) 3d5ad7f test: Add tests for `-debuglogfile` with subdirs (random-zebra) 2c2e36d test: Add test for `-debuglogfile` (random-zebra) b44a324 Add `-debuglogfile` option (random-zebra) Pull request description: Implemented on top of - [x] #1449 - [x] #1437 - [x] #1439 - [x] #1450 - [x] #1451 Only last 4 commits are relevant to this PR. Backports bitcoin#11781 > This patch adds an option to configure the name and/or directory of the debug log file.</br></br> The user can specify either a relative path, in which case the path is relative to the (network specific) data directory. They can also specify an absolute path to put the log anywhere else in the file system. Functional test included. ACKs for top commit: Fuzzbawls: ACK 882ef90 furszy: utACK 882ef90 Tree-SHA512: 02a0e6487683e5111765af7296ef7ce035372febf037268d99d29b4c4a2f74bcc40f46a0f5b1bacddc2249c2a7e40255555e83ca9d51bf71d9e054c6e85765cc
2 parents 3dddd25 + 882ef90 commit b26dbc4

File tree

6 files changed

+105
-16
lines changed

6 files changed

+105
-16
lines changed

doc/release-notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ E.g. `logging "[\"all\"]" "[\"http\"]""`
150150
Details about each new command can be found below.
151151

152152

153+
Changed command-line options
154+
-----------------------------
155+
156+
- `-debuglogfile=<file>` can be used to specify an alternative debug logging file. This can be an absolute path or a path relative to the data directory
157+
158+
153159
*version* Change log
154160
==============
155161

src/init.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ std::string HelpMessage(HelpMessageMode mode)
383383
#endif
384384
}
385385
strUsage += HelpMessageOpt("-datadir=<dir>", _("Specify data directory"));
386+
strUsage += HelpMessageOpt("-debuglogfile=<file>", strprintf(_("Specify location of debug log file: this can be an absolute path or a path relative to the data directory (default: %s)"), DEFAULT_DEBUGLOGFILE));
386387
strUsage += HelpMessageOpt("-dbcache=<n>", strprintf(_("Set database cache size in megabytes (%d to %d, default: %d)"), nMinDbCache, nMaxDbCache, nDefaultDbCache));
387388
strUsage += HelpMessageOpt("-loadblock=<file>", _("Imports blocks from external blk000??.dat file") + " " + _("on startup"));
388389
strUsage += HelpMessageOpt("-maxreorg=<n>", strprintf(_("Set the Maximum reorg depth (default: %u)"), DEFAULT_MAX_REORG_DEPTH));
@@ -1063,8 +1064,9 @@ bool AppInit2()
10631064
#endif
10641065
if (GetBoolArg("-shrinkdebugfile", logCategories != BCLog::NONE))
10651066
ShrinkDebugFile();
1066-
if (fPrintToDebugLog)
1067-
OpenDebugLog();
1067+
if (fPrintToDebugLog && !OpenDebugLog()) {
1068+
return UIError(strprintf("Could not open debug log file %s", GetDebugLogPath().string()));
1069+
}
10681070
#ifdef ENABLE_WALLET
10691071
LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
10701072
#endif

src/util.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ bool fSucessfullyLoaded = false;
102102
std::vector<int64_t> obfuScationDenominations;
103103
std::string strBudgetMode = "";
104104

105+
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
106+
namespace fs = boost::filesystem;
107+
105108
std::map<std::string, std::string> mapArgs;
106109
std::map<std::string, std::vector<std::string> > mapMultiArgs;
107110
bool fPrintToConsole = false;
@@ -203,17 +206,29 @@ static void DebugPrintInit()
203206
vMsgsBeforeOpenLog = new std::list<std::string>;
204207
}
205208

206-
void OpenDebugLog()
209+
fs::path GetDebugLogPath()
210+
{
211+
fs::path logfile(GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
212+
if (logfile.is_absolute()) {
213+
return logfile;
214+
} else {
215+
return GetDataDir() / logfile;
216+
}
217+
}
218+
219+
bool OpenDebugLog()
207220
{
208221
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
209222
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
210223
assert(fileout == nullptr);
211224
assert(vMsgsBeforeOpenLog);
212225

213-
boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
226+
boost::filesystem::path pathDebug = GetDebugLogPath();
227+
214228
fileout = fopen(pathDebug.string().c_str(), "a");
215-
if (fileout) setbuf(fileout, nullptr); // unbuffered
229+
if (!fileout) return false;
216230

231+
setbuf(fileout, nullptr); // unbuffered
217232
// dump buffered messages from before we opened the log
218233
while (!vMsgsBeforeOpenLog->empty()) {
219234
FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
@@ -222,6 +237,7 @@ void OpenDebugLog()
222237

223238
delete vMsgsBeforeOpenLog;
224239
vMsgsBeforeOpenLog = nullptr;
240+
return true;
225241
}
226242

227243
struct CLogCategoryDesc
@@ -357,7 +373,7 @@ int LogPrintStr(const std::string& str)
357373
// reopen the log file, if requested
358374
if (fReopenDebugLog) {
359375
fReopenDebugLog = false;
360-
boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
376+
boost::filesystem::path pathDebug = GetDebugLogPath();
361377
if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
362378
setbuf(fileout, NULL); // unbuffered
363379
}
@@ -497,7 +513,6 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
497513

498514
boost::filesystem::path GetDefaultDataDir()
499515
{
500-
namespace fs = boost::filesystem;
501516
// Windows < Vista: C:\Documents and Settings\Username\Application Data\PIVX
502517
// Windows >= Vista: C:\Users\Username\AppData\Roaming\PIVX
503518
// Mac: ~/Library/Application Support/PIVX
@@ -524,14 +539,12 @@ boost::filesystem::path GetDefaultDataDir()
524539
#endif
525540
}
526541

527-
static boost::filesystem::path pathCached;
528-
static boost::filesystem::path pathCachedNetSpecific;
542+
static fs::path pathCached;
543+
static fs::path pathCachedNetSpecific;
529544
static RecursiveMutex csPathCached;
530545

531-
const boost::filesystem::path& GetDataDir(bool fNetSpecific)
546+
const fs::path& GetDataDir(bool fNetSpecific)
532547
{
533-
namespace fs = boost::filesystem;
534-
535548
LOCK(csPathCached);
536549

537550
fs::path& path = fNetSpecific ? pathCachedNetSpecific : pathCached;
@@ -755,7 +768,7 @@ void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
755768
void ShrinkDebugFile()
756769
{
757770
// Scroll debug.log if it's getting too big
758-
boost::filesystem::path pathLog = GetDataDir() / "debug.log";
771+
boost::filesystem::path pathLog = GetDebugLogPath();
759772
FILE* file = fopen(pathLog.string().c_str(), "r");
760773
if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000) {
761774
// Restart the file with some of the end
@@ -776,8 +789,6 @@ void ShrinkDebugFile()
776789
#ifdef WIN32
777790
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
778791
{
779-
namespace fs = boost::filesystem;
780-
781792
char pszPath[MAX_PATH] = "";
782793

783794
if (SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) {

src/util.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <boost/thread/exceptions.hpp>
3333
#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
3434

35+
extern const char * const DEFAULT_DEBUGLOGFILE;
36+
3537
//PIVX only features
3638

3739
extern bool fMasterNode;
@@ -161,7 +163,8 @@ void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map
161163
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
162164
#endif
163165
boost::filesystem::path GetTempPath();
164-
void OpenDebugLog();
166+
boost::filesystem::path GetDebugLogPath();
167+
bool OpenDebugLog();
165168
void ShrinkDebugFile();
166169
void runCommand(std::string strCommand);
167170

test/functional/feature_logging.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017 The Bitcoin Core developers
3+
# Copyright (c) 20200 The PIVX Core developers
4+
# Distributed under the MIT software license, see the accompanying
5+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
"""Test debug logging."""
7+
8+
import os
9+
10+
from test_framework.test_framework import PivxTestFramework
11+
12+
class LoggingTest(PivxTestFramework):
13+
def set_test_params(self):
14+
self.num_nodes = 1
15+
self.setup_clean_chain = True
16+
17+
def run_test(self):
18+
# test default log file name
19+
assert os.path.isfile(os.path.join(self.nodes[0].datadir, "regtest", "debug.log"))
20+
self.log.info("Default filename ok")
21+
22+
# test alternative log file name in datadir
23+
self.restart_node(0, ["-debuglogfile=foo.log"])
24+
assert os.path.isfile(os.path.join(self.nodes[0].datadir, "regtest", "foo.log"))
25+
self.log.info("Alternative filename ok")
26+
27+
# test alternative log file name outside datadir
28+
tempname = os.path.join(self.options.tmpdir, "foo.log")
29+
self.restart_node(0, ["-debuglogfile=%s" % tempname])
30+
assert os.path.isfile(tempname)
31+
self.log.info("Alternative filename outside datadir ok")
32+
33+
# check that invalid log (relative) will cause error
34+
invdir = os.path.join(self.nodes[0].datadir, "regtest", "foo")
35+
invalidname = os.path.join("foo", "foo.log")
36+
self.stop_node(0)
37+
self.assert_start_raises_init_error(0, ["-debuglogfile=%s" % (invalidname)],
38+
"Error: Could not open debug log file")
39+
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
40+
self.log.info("Invalid relative filename throws")
41+
42+
# check that a previously invalid log (relative) works after path exists
43+
os.mkdir(invdir)
44+
self.start_node(0, ["-debuglogfile=%s" % (invalidname)])
45+
assert os.path.isfile(os.path.join(invdir, "foo.log"))
46+
self.log.info("Relative filename ok when path exists")
47+
48+
# check that invalid log (absolute) will cause error
49+
self.stop_node(0)
50+
invdir = os.path.join(self.options.tmpdir, "foo")
51+
invalidname = os.path.join(invdir, "foo.log")
52+
self.assert_start_raises_init_error(0, ["-debuglogfile=%s" % invalidname],
53+
"Error: Could not open debug log file")
54+
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
55+
self.log.info("Invalid absolute filename throws")
56+
57+
# check that a previously invalid log (relative) works after path exists
58+
os.mkdir(invdir)
59+
self.start_node(0, ["-debuglogfile=%s" % (invalidname)])
60+
assert os.path.isfile(os.path.join(invdir, "foo.log"))
61+
self.log.info("Absolute filename ok when path exists")
62+
63+
64+
if __name__ == '__main__':
65+
LoggingTest().main()

test/functional/test_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'wallet_abandonconflict.py', # ~ 212 sec
6767
'wallet_hd.py', # ~ 210 sec
6868
'wallet_zerocoin_publicspends.py', # ~ 202 sec
69+
'feature_logging.py', # ~ 200 sec
6970
'rpc_rawtransaction.py', # ~ 193 sec
7071
'wallet_zapwallettxes.py', # ~ 180 sec
7172
'wallet_keypool_topup.py', # ~ 174 sec
@@ -153,6 +154,7 @@
153154
LEGACY_SKIP_TESTS = [
154155
# These tests are not run when the flag --legacywallet is used
155156
'feature_help.py',
157+
'feature_logging.py',
156158
'feature_reindex.py',
157159
'feature_proxy.py',
158160
'feature_uacomment.py',

0 commit comments

Comments
 (0)