Skip to content

Commit

Permalink
Add -debuglogfile option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
random-zebra committed Apr 6, 2020
1 parent 1fa5156 commit b44a324
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ std::string HelpMessage(HelpMessageMode mode)
#endif
}
strUsage += HelpMessageOpt("-datadir=<dir>", _("Specify data directory"));
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));
strUsage += HelpMessageOpt("-dbcache=<n>", strprintf(_("Set database cache size in megabytes (%d to %d, default: %d)"), nMinDbCache, nMaxDbCache, nDefaultDbCache));
strUsage += HelpMessageOpt("-loadblock=<file>", _("Imports blocks from external blk000??.dat file") + " " + _("on startup"));
strUsage += HelpMessageOpt("-maxreorg=<n>", strprintf(_("Set the Maximum reorg depth (default: %u)"), DEFAULT_MAX_REORG_DEPTH));
Expand Down Expand Up @@ -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
Expand Down
37 changes: 24 additions & 13 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ bool fSucessfullyLoaded = false;
std::vector<int64_t> obfuScationDenominations;
std::string strBudgetMode = "";

const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
namespace fs = boost::filesystem;

std::map<std::string, std::string> mapArgs;
std::map<std::string, std::vector<std::string> > mapMultiArgs;
bool fPrintToConsole = false;
Expand Down Expand Up @@ -203,17 +206,29 @@ static void DebugPrintInit()
vMsgsBeforeOpenLog = new std::list<std::string>;
}

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);
Expand All @@ -222,6 +237,7 @@ void OpenDebugLog()

delete vMsgsBeforeOpenLog;
vMsgsBeforeOpenLog = nullptr;
return true;
}

struct CLogCategoryDesc
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand Down
5 changes: 4 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <boost/thread/exceptions.hpp>
#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted

extern const char * const DEFAULT_DEBUGLOGFILE;

//PIVX only features

extern bool fMasterNode;
Expand Down Expand Up @@ -161,7 +163,8 @@ void ReadConfigFile(std::map<std::string, std::string>& 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);

Expand Down

0 comments on commit b44a324

Please sign in to comment.