| 233 | } |
| 234 | |
| 235 | void BCLog::Logger::ShrinkDebugFile() |
| 236 | { |
| 237 | // Amount of debug.log to save at end when shrinking (must fit in memory) |
| 238 | constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; |
| 239 | |
| 240 | assert(!m_file_path.empty()); |
| 241 | |
| 242 | // Scroll debug.log if it's getting too big |
| 243 | FILE* file = fsbridge::fopen(m_file_path, "r"); |
| 244 | |
| 245 | // Special files (e.g. device nodes) may not have a size. |
| 246 | size_t log_size = 0; |
| 247 | try { |
| 248 | log_size = fs::file_size(m_file_path); |
| 249 | } catch (boost::filesystem::filesystem_error &) {} |
| 250 | |
| 251 | // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE |
| 252 | // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes |
| 253 | if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) |
| 254 | { |
| 255 | // Restart the file with some of the end |
| 256 | std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0); |
| 257 | if (fseek(file, -((long)vch.size()), SEEK_END)) { |
| 258 | LogPrintf("Failed to shrink debug log file: fseek(...) failed\n"); |
| 259 | fclose(file); |
| 260 | return; |
| 261 | } |
| 262 | int nBytes = fread(vch.data(), 1, vch.size(), file); |
| 263 | fclose(file); |
| 264 | |
| 265 | file = fsbridge::fopen(m_file_path, "w"); |
| 266 | if (file) |
| 267 | { |
| 268 | fwrite(vch.data(), 1, nBytes, file); |
| 269 | fclose(file); |
| 270 | } |
| 271 | } |
| 272 | else if (file != nullptr) |
| 273 | fclose(file); |
| 274 | } |
no test coverage detected