From a4ed799a8c4828ac98e1a79642ba8340467d6dd3 Mon Sep 17 00:00:00 2001 From: fxliang Date: Wed, 6 Mar 2024 17:25:07 +0800 Subject: [PATCH] set log filename extension and avoid deleting incorrect files (#838) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * set log filename extension ".log" * avoid deleting non-log file or not log files of current app --------- Co-authored-by: 居戎氏 --- src/rime/deployer.h | 1 + src/rime/lever/deployment_tasks.cc | 4 +++- src/rime/setup.cc | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/rime/deployer.h b/src/rime/deployer.h index ea8f836cd1..9d2427dd51 100644 --- a/src/rime/deployer.h +++ b/src/rime/deployer.h @@ -41,6 +41,7 @@ class Deployer : public Messenger { string distribution_name; string distribution_code_name; string distribution_version; + string app_name; // } RIME_API Deployer(); diff --git a/src/rime/lever/deployment_tasks.cc b/src/rime/lever/deployment_tasks.cc index fd33027cd3..c141f08e29 100644 --- a/src/rime/lever/deployment_tasks.cc +++ b/src/rime/lever/deployment_tasks.cc @@ -633,6 +633,7 @@ bool CleanOldLogFiles::Run(Deployer* deployer) { DLOG(INFO) << "scanning " << dirs.size() << " temp directory for log files."; int removed = 0; + const string& app_name = deployer->app_name; for (const auto& dir : dirs) { // avoid iteration on non-existing directory, which may cause error if (!fs::exists(fs::path(dir))) @@ -644,7 +645,8 @@ bool CleanOldLogFiles::Run(Deployer* deployer) { for (const auto& entry : fs::directory_iterator(dir)) { const string& file_name(entry.path().filename().string()); if (entry.is_regular_file() && !entry.is_symlink() && - boost::starts_with(file_name, "rime.") && + boost::starts_with(file_name, app_name) && + boost::ends_with(file_name, ".log") && !boost::contains(file_name, today)) { files.push_back(entry.path()); } diff --git a/src/rime/setup.cc b/src/rime/setup.cc index 760088cd63..d188beccf5 100644 --- a/src/rime/setup.cc +++ b/src/rime/setup.cc @@ -57,6 +57,8 @@ RIME_API void SetupDeployer(RimeTraits* traits) { deployer.distribution_code_name = traits->distribution_code_name; if (PROVIDED(traits, distribution_version)) deployer.distribution_version = traits->distribution_version; + if (PROVIDED(traits, app_name)) + deployer.app_name = traits->app_name; if (PROVIDED(traits, prebuilt_data_dir)) deployer.prebuilt_data_dir = path(traits->prebuilt_data_dir); else @@ -82,6 +84,7 @@ RIME_API void SetupLogging(const char* app_name, FLAGS_log_dir = log_dir; } } + google::SetLogFilenameExtension(".log"); // Do not allow other users to read/write log files created by current // process. FLAGS_logfile_mode = 0600;