Skip to content

Commit

Permalink
Increase minimum debug.log size to 10MB after shrink. (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 authored May 31, 2017
1 parent fc406f2 commit 5988e1e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,8 +1210,11 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
#ifndef WIN32
CreatePidFile(GetPidFile(), getpid());
#endif
if (GetBoolArg("-shrinkdebugfile", !fDebug))
if (GetBoolArg("-shrinkdebugfile", !fDebug)) {
// Do this first since it both loads a bunch of debug.log into memory,
// and because this needs to happen before any other debug.log printing
ShrinkDebugFile();
}

if (fPrintToDebugLog)
OpenDebugLog();
Expand Down
8 changes: 6 additions & 2 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,13 +796,17 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {

void ShrinkDebugFile()
{
// Amount of debug.log to save at end when shrinking (must fit in memory)
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
// Scroll debug.log if it's getting too big
boost::filesystem::path pathLog = GetDataDir() / "debug.log";
FILE* file = fopen(pathLog.string().c_str(), "r");
if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
// trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
if (file && boost::filesystem::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
{
// Restart the file with some of the end
std::vector <char> vch(200000,0);
std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
fseek(file, -((long)vch.size()), SEEK_END);
int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
fclose(file);
Expand Down

0 comments on commit 5988e1e

Please sign in to comment.