From 5179b745e14c29f155b23fb809ca43469a58caf9 Mon Sep 17 00:00:00 2001 From: "tao.jin" Date: Tue, 27 Sep 2022 12:38:06 +0800 Subject: [PATCH] NEW: limit log files within 30 by deleting old files Change-Id: I1bb5c827c7fb01d97b84a4a1bf3f905bb8e90448 (cherry picked from commit 065ef224357352851cbba2c0dcac131ea9acae82) --- src/slic3r/GUI/GUI_App.cpp | 25 +++++++++++++++++++++++++ src/slic3r/GUI/GUI_App.hpp | 1 + 2 files changed, 26 insertions(+) diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 6529bf7f1..6f25048f6 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -1096,6 +1096,31 @@ void GUI_App::post_init() std::string functional_config_file = Slic3r::resources_dir() + "/config.json"; DeviceManager::load_functional_config(encode_path(functional_config_file.c_str())); + // remove old log files over LOG_FILES_MAX_NUM + std::string log_addr = data_dir(); + if (!log_addr.empty()) { + auto log_folder = boost::filesystem::path(log_addr) / "log"; + if (boost::filesystem::exists(log_folder)) { + std::vector> files_vec; + for (auto& it : boost::filesystem::directory_iterator(log_folder)) { + auto temp_path = it.path(); + std::time_t lw_t = boost::filesystem::last_write_time(temp_path) ; + files_vec.push_back({ lw_t, temp_path.filename().string() }); + } + std::sort(files_vec.begin(), files_vec.end(), []( + std::pair &a, std::pair &b) { + return a.first > b.first; + }); + + while (files_vec.size() > LOG_FILES_MAX_NUM) { + auto full_path = log_folder / boost::filesystem::path(files_vec[files_vec.size() - 1].second); + BOOST_LOG_TRIVIAL(info) << "delete log file over " << LOG_FILES_MAX_NUM << ", filename: "<< files_vec[files_vec.size() - 1].second; + boost::filesystem::remove(full_path); + files_vec.pop_back(); + } + } + } + BOOST_LOG_TRIVIAL(info) << "finished post_init"; //BBS: remove the single instance currently /*#ifdef _WIN32 diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index 8f189f095..209c1a75f 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -26,6 +26,7 @@ #define BBL_HAS_FIRST_PAGE 1 #define STUDIO_INACTIVE_TIMEOUT 15*60*1000 +#define LOG_FILES_MAX_NUM 30 class wxMenuItem; class wxMenuBar;