Skip to content

Commit cf5f432

Browse files
committed
Add -debuglogfile option
This patch adds an option to configure the name and/or directory of the debug log. The user can specify either a relative path, in which case the path is relative to the data directory. They can also specify an absolute path to put the log anywhere else in the file system.
1 parent 16fff80 commit cf5f432

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

src/init.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ std::string HelpMessage(HelpMessageMode mode)
342342
if (showDebug)
343343
strUsage += HelpMessageOpt("-feefilter", strprintf("Tell other nodes to filter invs to us by our mempool min fee (default: %u)", DEFAULT_FEEFILTER));
344344
strUsage += HelpMessageOpt("-loadblock=<file>", _("Imports blocks from external blk000??.dat file on startup"));
345+
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));
345346
strUsage += HelpMessageOpt("-maxorphantx=<n>", strprintf(_("Keep at most <n> unconnectable transactions in memory (default: %u)"), DEFAULT_MAX_ORPHAN_TRANSACTIONS));
346347
strUsage += HelpMessageOpt("-maxmempool=<n>", strprintf(_("Keep the transaction memory pool below <n> megabytes (default: %u)"), DEFAULT_MAX_MEMPOOL_SIZE));
347348
strUsage += HelpMessageOpt("-mempoolexpiry=<n>", strprintf(_("Do not keep transactions in the mempool longer than <n> hours (default: %u)"), DEFAULT_MEMPOOL_EXPIRY));
@@ -1209,8 +1210,11 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12091210
ShrinkDebugFile();
12101211
}
12111212

1212-
if (fPrintToDebugLog)
1213-
OpenDebugLog();
1213+
if (fPrintToDebugLog) {
1214+
if (!OpenDebugLog()) {
1215+
return InitError(strprintf("Could not open debug log file %s", GetDebugLogPath().string()));
1216+
}
1217+
}
12141218

12151219
if (!fLogTimestamps)
12161220
LogPrintf("Startup time: %s\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()));

src/util.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const int64_t nStartupTime = GetTime();
8989

9090
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
9191
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
92+
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
9293

9394
ArgsManager gArgs;
9495
bool fPrintToConsole = false;
@@ -189,26 +190,40 @@ static void DebugPrintInit()
189190
vMsgsBeforeOpenLog = new std::list<std::string>;
190191
}
191192

192-
void OpenDebugLog()
193+
fs::path GetDebugLogPath()
194+
{
195+
fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
196+
if (logfile.is_absolute()) {
197+
return logfile;
198+
} else {
199+
return GetDataDir() / logfile;
200+
}
201+
}
202+
203+
bool OpenDebugLog()
193204
{
194205
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
195206
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
196207

197208
assert(fileout == nullptr);
198209
assert(vMsgsBeforeOpenLog);
199-
fs::path pathDebug = GetDataDir() / "debug.log";
210+
fs::path pathDebug = GetDebugLogPath();
211+
200212
fileout = fsbridge::fopen(pathDebug, "a");
201-
if (fileout) {
202-
setbuf(fileout, nullptr); // unbuffered
203-
// dump buffered messages from before we opened the log
204-
while (!vMsgsBeforeOpenLog->empty()) {
205-
FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
206-
vMsgsBeforeOpenLog->pop_front();
207-
}
213+
if (!fileout) {
214+
return false;
215+
}
216+
217+
setbuf(fileout, nullptr); // unbuffered
218+
// dump buffered messages from before we opened the log
219+
while (!vMsgsBeforeOpenLog->empty()) {
220+
FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
221+
vMsgsBeforeOpenLog->pop_front();
208222
}
209223

210224
delete vMsgsBeforeOpenLog;
211225
vMsgsBeforeOpenLog = nullptr;
226+
return true;
212227
}
213228

214229
struct CLogCategoryDesc
@@ -355,7 +370,7 @@ int LogPrintStr(const std::string &str)
355370
// reopen the log file, if requested
356371
if (fReopenDebugLog) {
357372
fReopenDebugLog = false;
358-
fs::path pathDebug = GetDataDir() / "debug.log";
373+
fs::path pathDebug = GetDebugLogPath();
359374
if (fsbridge::freopen(pathDebug,"a",fileout) != nullptr)
360375
setbuf(fileout, nullptr); // unbuffered
361376
}
@@ -774,7 +789,7 @@ void ShrinkDebugFile()
774789
// Amount of debug.log to save at end when shrinking (must fit in memory)
775790
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
776791
// Scroll debug.log if it's getting too big
777-
fs::path pathLog = GetDataDir() / "debug.log";
792+
fs::path pathLog = GetDebugLogPath();
778793
FILE* file = fsbridge::fopen(pathLog, "r");
779794
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
780795
// trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes

src/util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ int64_t GetStartupTime();
3636
static const bool DEFAULT_LOGTIMEMICROS = false;
3737
static const bool DEFAULT_LOGIPS = false;
3838
static const bool DEFAULT_LOGTIMESTAMPS = true;
39+
extern const char * const DEFAULT_DEBUGLOGFILE;
3940

4041
/** Signals for translation. */
4142
class CTranslationInterface
@@ -180,7 +181,8 @@ void CreatePidFile(const fs::path &path, pid_t pid);
180181
#ifdef WIN32
181182
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
182183
#endif
183-
void OpenDebugLog();
184+
fs::path GetDebugLogPath();
185+
bool OpenDebugLog();
184186
void ShrinkDebugFile();
185187
void runCommand(const std::string& strCommand);
186188

0 commit comments

Comments
 (0)