src/logging.cc: Make LogCleaner aware of filename extension (#589)

Previously, LogCleaner::IsLogFromCurrentProject() did not consider
the custom file extension set with SetLogFilenameExtension().

This PR fixes it.

Signed-off-by: Marco Wang <m.aesophor@gmail.com>
This commit is contained in:
Marco Wang 2020-10-09 22:40:12 +08:00
parent c8f8135a57
commit 431d74c802

View File

@ -473,14 +473,22 @@ class LogCleaner {
void Enable(int overdue_days); void Enable(int overdue_days);
void Disable(); void Disable();
void Run(bool base_filename_selected, const string& base_filename) const; void Run(bool base_filename_selected,
const string& base_filename,
const string& filename_extension) const;
inline bool enabled() const { return enabled_; } inline bool enabled() const { return enabled_; }
private: private:
vector<string> GetOverdueLogNames(string log_directory, int days, vector<string> GetOverdueLogNames(string log_directory,
const string& base_filename) const; int days,
bool IsLogFromCurrentProject(const string& filepath, const string& base_filename) const; const string& base_filename,
const string& filename_extension) const;
bool IsLogFromCurrentProject(const string& filepath,
const string& base_filename,
const string& filename_extension) const;
bool IsLogLastModifiedOver(const string& filepath, int days) const; bool IsLogLastModifiedOver(const string& filepath, int days) const;
bool enabled_; bool enabled_;
@ -1272,12 +1280,15 @@ void LogFileObject::Write(bool force_flush,
} }
} }
#endif #endif
// Perform clean up for old logs // Perform clean up for old logs
if (log_cleaner.enabled()) { if (log_cleaner.enabled()) {
if (base_filename_selected_ && base_filename_.empty()) { if (base_filename_selected_ && base_filename_.empty()) {
return; return;
} }
log_cleaner.Run(base_filename_selected_, base_filename_); log_cleaner.Run(base_filename_selected_,
base_filename_,
filename_extension_);
} }
} }
} }
@ -1302,7 +1313,9 @@ void LogCleaner::Disable() {
enabled_ = false; enabled_ = false;
} }
void LogCleaner::Run(bool base_filename_selected, const string& base_filename) const { void LogCleaner::Run(bool base_filename_selected,
const string& base_filename,
const string& filename_extension) const {
assert(enabled_ && overdue_days_ > 0); assert(enabled_ && overdue_days_ > 0);
vector<string> dirs; vector<string> dirs;
@ -1315,15 +1328,20 @@ void LogCleaner::Run(bool base_filename_selected, const string& base_filename) c
} }
for (size_t i = 0; i < dirs.size(); i++) { for (size_t i = 0; i < dirs.size(); i++) {
vector<string> logs = GetOverdueLogNames(dirs[i], overdue_days_, base_filename); vector<string> logs = GetOverdueLogNames(dirs[i],
overdue_days_,
base_filename,
filename_extension);
for (size_t j = 0; j < logs.size(); j++) { for (size_t j = 0; j < logs.size(); j++) {
static_cast<void>(unlink(logs[j].c_str())); static_cast<void>(unlink(logs[j].c_str()));
} }
} }
} }
vector<string> LogCleaner::GetOverdueLogNames(string log_directory, int days, vector<string> LogCleaner::GetOverdueLogNames(string log_directory,
const string& base_filename) const { int days,
const string& base_filename,
const string& filename_extension) const {
// The names of overdue logs. // The names of overdue logs.
vector<string> overdue_log_names; vector<string> overdue_log_names;
@ -1342,7 +1360,7 @@ vector<string> LogCleaner::GetOverdueLogNames(string log_directory, int days,
continue; continue;
} }
string filepath = log_directory + ent->d_name; string filepath = log_directory + ent->d_name;
if (IsLogFromCurrentProject(filepath, base_filename) && if (IsLogFromCurrentProject(filepath, base_filename, filename_extension) &&
IsLogLastModifiedOver(filepath, days)) { IsLogLastModifiedOver(filepath, days)) {
overdue_log_names.push_back(filepath); overdue_log_names.push_back(filepath);
} }
@ -1354,7 +1372,8 @@ vector<string> LogCleaner::GetOverdueLogNames(string log_directory, int days,
} }
bool LogCleaner::IsLogFromCurrentProject(const string& filepath, bool LogCleaner::IsLogFromCurrentProject(const string& filepath,
const string& base_filename) const { const string& base_filename,
const string& filename_extension) const {
// We should remove duplicated delimiters from `base_filename`, e.g., // We should remove duplicated delimiters from `base_filename`, e.g.,
// before: "/tmp//<base_filename>.<create_time>.<pid>" // before: "/tmp//<base_filename>.<create_time>.<pid>"
// after: "/tmp/<base_filename>.<create_time>.<pid>" // after: "/tmp/<base_filename>.<create_time>.<pid>"
@ -1376,6 +1395,16 @@ bool LogCleaner::IsLogFromCurrentProject(const string& filepath,
return false; return false;
} }
// Check if in the string `filename_extension` is right next to
// `cleaned_base_filename` in `filepath` if the user
// has set a custom filename extension.
if (!filename_extension.empty()) {
if (filepath.find(filename_extension) != cleaned_base_filename.size()) {
return false;
}
cleaned_base_filename += filename_extension;
}
// The characters after `cleaned_base_filename` should match the format: // The characters after `cleaned_base_filename` should match the format:
// YYYYMMDD-HHMMSS.pid // YYYYMMDD-HHMMSS.pid
for (size_t i = cleaned_base_filename.size(); i < filepath.size(); i++) { for (size_t i = cleaned_base_filename.size(); i < filepath.size(); i++) {