Skip to content

Commit 77bd8c4

Browse files
committed
Merge #9625: Increase minimum debug.log size to 10MB after shrink.
29fb311 Increase minimum debug.log size to 10MB after shrink. (Alex Morcos)
2 parents 7bfb770 + 29fb311 commit 77bd8c4

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/init.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,11 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
11461146
#ifndef WIN32
11471147
CreatePidFile(GetPidFile(), getpid());
11481148
#endif
1149-
if (GetBoolArg("-shrinkdebugfile", !fDebug))
1149+
if (GetBoolArg("-shrinkdebugfile", !fDebug)) {
1150+
// Do this first since it both loads a bunch of debug.log into memory,
1151+
// and because this needs to happen before any other debug.log printing
11501152
ShrinkDebugFile();
1153+
}
11511154

11521155
if (fPrintToDebugLog)
11531156
OpenDebugLog();

src/util.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,17 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
723723

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

0 commit comments

Comments
 (0)