From 28398b79333db816b0a12013a0eecd77764945ee Mon Sep 17 00:00:00 2001 From: mturnock Date: Mon, 11 Mar 2024 20:37:46 +0000 Subject: [PATCH] OverrideLogCleanerFilePathPrefix to override the files the log cleaner removes --- src/glog/logging.h | 1 + src/logging.cc | 33 +++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/glog/logging.h b/src/glog/logging.h index 6e36eaadf..f6acb3169 100644 --- a/src/glog/logging.h +++ b/src/glog/logging.h @@ -484,6 +484,7 @@ InstallFailureFunction(logging_fail_func_t fail_func); // Enable/Disable old log cleaner. GLOG_EXPORT void EnableLogCleaner(const std::chrono::minutes& overdue); GLOG_EXPORT void DisableLogCleaner(); +GLOG_EXPORT void OverrideLogCleanerFilePathPrefix(const std::string& prefix); GLOG_EXPORT void SetApplicationFingerprint(const std::string& fingerprint); class LogSink; // defined below diff --git a/src/logging.cc b/src/logging.cc index 08b2ab387..e6a641eb5 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -439,6 +439,8 @@ class LogCleaner { void Enable(const std::chrono::minutes& overdue); void Disable(); + void OverrideFilePathPrefix(const std::string& prefix); + void Run(const std::chrono::system_clock::time_point& current_time, bool base_filename_selected, const string& base_filename, const string& filename_extension); @@ -464,6 +466,8 @@ class LogCleaner { std::chrono::duration>{1}}; std::chrono::system_clock::time_point next_cleanup_time_; // cycle count at which to clean overdue log + bool override_required_file_path_prefix_{false}; + std::string required_file_path_prefix_; }; LogCleaner log_cleaner; @@ -1291,6 +1295,11 @@ void LogCleaner::Enable(const std::chrono::minutes& overdue) { void LogCleaner::Disable() { enabled_ = false; } +void LogCleaner::OverrideFilePathPrefix(const std::string& prefix) { + override_required_file_path_prefix_ = true; + required_file_path_prefix_ = prefix; +} + void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time, bool base_filename_selected, const string& base_filename, const string& filename_extension) { @@ -1309,17 +1318,20 @@ void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time, vector dirs; - if (!base_filename_selected) { - dirs = GetLoggingDirectories(); - } else { - size_t pos = base_filename.find_last_of(possible_dir_delim, string::npos, - sizeof(possible_dir_delim)); + if (base_filename_selected || override_required_file_path_prefix_) { + const string base_path = override_required_file_path_prefix_ + ? required_file_path_prefix_ + : base_filename; + size_t pos = base_path.find_last_of(possible_dir_delim, string::npos, + sizeof(possible_dir_delim)); if (pos != string::npos) { - string dir = base_filename.substr(0, pos + 1); + string dir = base_path.substr(0, pos + 1); dirs.push_back(dir); } else { dirs.emplace_back("."); } + } else { + dirs = GetLoggingDirectories(); } for (const std::string& dir : dirs) { @@ -1397,6 +1409,11 @@ bool LogCleaner::IsLogFromCurrentProject( } } + // If overriding log file path prefix, we only need to check this. + if (override_required_file_path_prefix_) { + return filepath.find(required_file_path_prefix_) == 0; + } + // Return early if the filename doesn't start with `cleaned_base_filename`. if (filepath.find(cleaned_base_filename) != 0) { return false; @@ -2629,6 +2646,10 @@ void EnableLogCleaner(const std::chrono::minutes& overdue) { void DisableLogCleaner() { log_cleaner.Disable(); } +void OverrideLogCleanerFilePathPrefix(const std::string& prefix) { + log_cleaner.OverrideFilePathPrefix(prefix); +} + LogMessageTime::LogMessageTime() = default; namespace {