MCPcopy Create free account
hub / github.com/ElementsProject/elements / ShrinkDebugFile

Method ShrinkDebugFile

src/logging.cpp:449–488  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

447}
448
449void BCLog::Logger::ShrinkDebugFile()
450{
451 // Amount of debug.log to save at end when shrinking (must fit in memory)
452 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
453
454 assert(!m_file_path.empty());
455
456 // Scroll debug.log if it's getting too big
457 FILE* file = fsbridge::fopen(m_file_path, "r");
458
459 // Special files (e.g. device nodes) may not have a size.
460 size_t log_size = 0;
461 try {
462 log_size = fs::file_size(m_file_path);
463 } catch (const fs::filesystem_error&) {}
464
465 // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
466 // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
467 if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
468 {
469 // Restart the file with some of the end
470 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
471 if (fseek(file, -((long)vch.size()), SEEK_END)) {
472 LogPrintf("Failed to shrink debug log file: fseek(...) failed\n");
473 fclose(file);
474 return;
475 }
476 int nBytes = fread(vch.data(), 1, vch.size(), file);
477 fclose(file);
478
479 file = fsbridge::fopen(m_file_path, "w");
480 if (file)
481 {
482 fwrite(vch.data(), 1, nBytes, file);
483 fclose(file);
484 }
485 }
486 else if (file != nullptr)
487 fclose(file);
488}
489
490void BCLog::LogRateLimiter::MaybeReset()
491{

Callers 1

StartLoggingFunction · 0.80

Calls 4

fopenFunction · 0.85
emptyMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected