From e1620966e5b185ba243f914725661cf1fbb68ed7 Mon Sep 17 00:00:00 2001 From: iTrooz Date: Tue, 16 May 2023 14:45:24 +0200 Subject: [PATCH] feat: Keep only 10 latest logs on exit (#1079) Co-authored-by: Nik --- main/source/init/tasks.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index f3f413e21..b99f1f698 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -442,6 +442,27 @@ namespace hex::init { return true; } + bool clearOldLogs() { + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs, true)) { + std::vector 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 }, }; }