Skip to content

Commit b44a324

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. backports bitcoin/bitcoin@cf5f432
1 parent 1fa5156 commit b44a324

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

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

0 commit comments

Comments
 (0)