From cb1b4aa8911cc84c66484ef785a91686e242f2f1 Mon Sep 17 00:00:00 2001 From: HNOONa-0 Date: Mon, 16 Dec 2024 18:48:01 +0200 Subject: [PATCH] add map between severity and overdue minutes --- src/logging.cc | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/logging.cc b/src/logging.cc index 08b2ab387..5cbdeac9f 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -46,6 +46,7 @@ #include #include #include +#include #include "config.h" #include "glog/platform.h" @@ -337,6 +338,16 @@ const char* GetLogSeverityName(LogSeverity severity) { return LogSeverityNames[severity]; } +LogSeverity FindFilepathLogSeverity(const std::string& filepath) { + for (int i = GLOG_INFO; i < NUM_SEVERITIES; i++) { + if (filepath.find(GetLogSeverityName(static_cast(i))) != std::string::npos) { + return static_cast(i); + } + } + // assume that the file has the INFO severity + return GLOG_INFO; +} + static bool SendEmailInternal(const char* dest, const char* subject, const char* body, bool use_logging); @@ -437,6 +448,9 @@ class LogCleaner { // Setting overdue to 0 days will delete all logs. void Enable(const std::chrono::minutes& overdue); + + void Enable(std::unordered_map& overdue_per_severity_); + void Disable(); void Run(const std::chrono::system_clock::time_point& current_time, @@ -460,8 +474,7 @@ class LogCleaner { const std::chrono::system_clock::time_point& current_time) const; bool enabled_{false}; - std::chrono::minutes overdue_{ - std::chrono::duration>{1}}; + std::unordered_map overdue_per_severity_; std::chrono::system_clock::time_point next_cleanup_time_; // cycle count at which to clean overdue log }; @@ -1286,7 +1299,14 @@ LogCleaner::LogCleaner() = default; void LogCleaner::Enable(const std::chrono::minutes& overdue) { enabled_ = true; - overdue_ = overdue; + for (int i = GLOG_INFO; i < NUM_SEVERITIES; i++) { + overdue_per_severity_[static_cast(i)] = overdue; + } +} + +void LogCleaner::Enable(std::unordered_map& overdue_per_severity) { + enabled_ = true; + overdue_per_severity_ = overdue_per_severity; } void LogCleaner::Disable() { enabled_ = false; } @@ -1471,7 +1491,14 @@ bool LogCleaner::IsLogLastModifiedOver( const auto last_modified_time = std::chrono::system_clock::from_time_t(file_stat.st_mtime); const auto diff = current_time - last_modified_time; - return diff >= overdue_; + + LogSeverity severity = FindFilepathLogSeverity(filepath); + auto overdue_it = overdue_per_severity_.find(severity); + // if there is no overdue for this severity, do not delete it + if(overdue_it == overdue_per_severity_.end()){ + return false; + } + return diff >= overdue_it->second; } // If failed to get file stat, don't return true! @@ -2627,6 +2654,10 @@ void EnableLogCleaner(const std::chrono::minutes& overdue) { log_cleaner.Enable(overdue); } +void EnableLogCleaner(std::unordered_map& overdue_per_severity) { + log_cleaner.Enable(overdue_per_severity); +} + void DisableLogCleaner() { log_cleaner.Disable(); } LogMessageTime::LogMessageTime() = default;