feat: Keep only 10 latest logs on exit (#1079)

Co-authored-by: Nik <werwolv98@gmail.com>
This commit is contained in:
iTrooz 2023-05-16 14:45:24 +02:00 committed by GitHub
parent a5b0a8614a
commit e1620966e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 0 deletions

View File

@ -442,6 +442,27 @@ namespace hex::init {
return true;
}
bool clearOldLogs() {
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs, true)) {
std::vector<std::filesystem::directory_entry> files;
for (const auto& file : std::filesystem::directory_iterator(path))
files.push_back(file);
if (files.size() <= 10)
return true;
std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) {
return std::filesystem::last_write_time(a) > std::filesystem::last_write_time(b);
});
for (auto it = files.begin() + 10; it != files.end(); it++)
std::filesystem::remove(it->path());
}
return true;
}
bool unloadPlugins() {
PluginManager::unload();
@ -497,6 +518,7 @@ namespace hex::init {
{ "Saving settings...", storeSettings, false },
{ "Cleaning up shared data...", deleteSharedData, false },
{ "Unloading plugins...", unloadPlugins, false },
{ "Clearing old logs...", clearOldLogs, false },
};
}