diff --git a/src/init.cpp b/src/init.cpp index 68a8992e8f2d8..e99fee0a376c5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -383,6 +383,7 @@ std::string HelpMessage(HelpMessageMode mode) #endif } strUsage += HelpMessageOpt("-datadir=", _("Specify data directory")); + strUsage += HelpMessageOpt("-debuglogfile=", 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)); strUsage += HelpMessageOpt("-dbcache=", strprintf(_("Set database cache size in megabytes (%d to %d, default: %d)"), nMinDbCache, nMaxDbCache, nDefaultDbCache)); strUsage += HelpMessageOpt("-loadblock=", _("Imports blocks from external blk000??.dat file") + " " + _("on startup")); strUsage += HelpMessageOpt("-maxreorg=", strprintf(_("Set the Maximum reorg depth (default: %u)"), DEFAULT_MAX_REORG_DEPTH)); @@ -1063,8 +1064,9 @@ bool AppInit2() #endif if (GetBoolArg("-shrinkdebugfile", logCategories != BCLog::NONE)) ShrinkDebugFile(); - if (fPrintToDebugLog) - OpenDebugLog(); + if (fPrintToDebugLog && !OpenDebugLog()) { + return UIError(strprintf("Could not open debug log file %s", GetDebugLogPath().string())); + } #ifdef ENABLE_WALLET LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0)); #endif diff --git a/src/util.cpp b/src/util.cpp index 958bf072a951e..452ef33ca154a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -102,6 +102,9 @@ bool fSucessfullyLoaded = false; std::vector obfuScationDenominations; std::string strBudgetMode = ""; +const char * const DEFAULT_DEBUGLOGFILE = "debug.log"; +namespace fs = boost::filesystem; + std::map mapArgs; std::map > mapMultiArgs; bool fPrintToConsole = false; @@ -203,17 +206,29 @@ static void DebugPrintInit() vMsgsBeforeOpenLog = new std::list; } -void OpenDebugLog() +fs::path GetDebugLogPath() +{ + fs::path logfile(GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)); + if (logfile.is_absolute()) { + return logfile; + } else { + return GetDataDir() / logfile; + } +} + +bool OpenDebugLog() { boost::call_once(&DebugPrintInit, debugPrintInitFlag); boost::mutex::scoped_lock scoped_lock(*mutexDebugLog); assert(fileout == nullptr); assert(vMsgsBeforeOpenLog); - boost::filesystem::path pathDebug = GetDataDir() / "debug.log"; + boost::filesystem::path pathDebug = GetDebugLogPath(); + fileout = fopen(pathDebug.string().c_str(), "a"); - if (fileout) setbuf(fileout, nullptr); // unbuffered + if (!fileout) return false; + setbuf(fileout, nullptr); // unbuffered // dump buffered messages from before we opened the log while (!vMsgsBeforeOpenLog->empty()) { FileWriteStr(vMsgsBeforeOpenLog->front(), fileout); @@ -222,6 +237,7 @@ void OpenDebugLog() delete vMsgsBeforeOpenLog; vMsgsBeforeOpenLog = nullptr; + return true; } struct CLogCategoryDesc @@ -357,7 +373,7 @@ int LogPrintStr(const std::string& str) // reopen the log file, if requested if (fReopenDebugLog) { fReopenDebugLog = false; - boost::filesystem::path pathDebug = GetDataDir() / "debug.log"; + boost::filesystem::path pathDebug = GetDebugLogPath(); if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL) setbuf(fileout, NULL); // unbuffered } @@ -497,7 +513,6 @@ void PrintExceptionContinue(const std::exception* pex, const char* pszThread) boost::filesystem::path GetDefaultDataDir() { - namespace fs = boost::filesystem; // Windows < Vista: C:\Documents and Settings\Username\Application Data\PIVX // Windows >= Vista: C:\Users\Username\AppData\Roaming\PIVX // Mac: ~/Library/Application Support/PIVX @@ -524,14 +539,12 @@ boost::filesystem::path GetDefaultDataDir() #endif } -static boost::filesystem::path pathCached; -static boost::filesystem::path pathCachedNetSpecific; +static fs::path pathCached; +static fs::path pathCachedNetSpecific; static RecursiveMutex csPathCached; -const boost::filesystem::path& GetDataDir(bool fNetSpecific) +const fs::path& GetDataDir(bool fNetSpecific) { - namespace fs = boost::filesystem; - LOCK(csPathCached); fs::path& path = fNetSpecific ? pathCachedNetSpecific : pathCached; @@ -755,7 +768,7 @@ void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length) void ShrinkDebugFile() { // Scroll debug.log if it's getting too big - boost::filesystem::path pathLog = GetDataDir() / "debug.log"; + boost::filesystem::path pathLog = GetDebugLogPath(); FILE* file = fopen(pathLog.string().c_str(), "r"); if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000) { // Restart the file with some of the end @@ -776,8 +789,6 @@ void ShrinkDebugFile() #ifdef WIN32 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate) { - namespace fs = boost::filesystem; - char pszPath[MAX_PATH] = ""; if (SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) { diff --git a/src/util.h b/src/util.h index 6733d634595a1..e5bb1d30b5cbb 100644 --- a/src/util.h +++ b/src/util.h @@ -32,6 +32,8 @@ #include #include // for boost::thread_interrupted +extern const char * const DEFAULT_DEBUGLOGFILE; + //PIVX only features extern bool fMasterNode; @@ -161,7 +163,8 @@ void ReadConfigFile(std::map& mapSettingsRet, std::map boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true); #endif boost::filesystem::path GetTempPath(); -void OpenDebugLog(); +boost::filesystem::path GetDebugLogPath(); +bool OpenDebugLog(); void ShrinkDebugFile(); void runCommand(std::string strCommand);