LogCleaner: make overdue_days_ unsigned int

Since the value of `LogCleaner::overdue_days_` should be >= 0,
we can simply make it an unsigned int which also avoids
unnecessary assertions.

Signed-off-by: Marco Wang <m.aesophor@gmail.com>
This commit is contained in:
Marco Wang 2021-12-14 00:19:19 +08:00 committed by Sergiu Deitsch
parent baa7006e63
commit d09b999f34
2 changed files with 14 additions and 13 deletions

View File

@ -627,7 +627,7 @@ typedef void (*logging_fail_func_t)();
GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(logging_fail_func_t fail_func); GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(logging_fail_func_t fail_func);
// Enable/Disable old log cleaner. // Enable/Disable old log cleaner.
GOOGLE_GLOG_DLL_DECL void EnableLogCleaner(int overdue_days); GOOGLE_GLOG_DLL_DECL void EnableLogCleaner(unsigned int overdue_days);
GOOGLE_GLOG_DLL_DECL void DisableLogCleaner(); GOOGLE_GLOG_DLL_DECL void DisableLogCleaner();
GOOGLE_GLOG_DLL_DECL void SetApplicationFingerprint(const std::string& fingerprint); GOOGLE_GLOG_DLL_DECL void SetApplicationFingerprint(const std::string& fingerprint);

View File

@ -476,8 +476,10 @@ class LogCleaner {
public: public:
LogCleaner(); LogCleaner();
void Enable(int overdue_days); // Setting overdue_days to 0 days will delete all logs.
void Enable(unsigned int overdue_days);
void Disable(); void Disable();
void Run(bool base_filename_selected, void Run(bool base_filename_selected,
const string& base_filename, const string& base_filename,
const string& filename_extension) const; const string& filename_extension) const;
@ -486,7 +488,7 @@ class LogCleaner {
private: private:
vector<string> GetOverdueLogNames(string log_directory, vector<string> GetOverdueLogNames(string log_directory,
int days, unsigned int days,
const string& base_filename, const string& base_filename,
const string& filename_extension) const; const string& filename_extension) const;
@ -494,10 +496,11 @@ class LogCleaner {
const string& base_filename, const string& base_filename,
const string& filename_extension) const; const string& filename_extension) const;
bool IsLogLastModifiedOver(const string& filepath, int days) const; bool IsLogLastModifiedOver(const string& filepath,
unsigned int days) const;
bool enabled_; bool enabled_;
int overdue_days_; unsigned int overdue_days_;
}; };
LogCleaner log_cleaner; LogCleaner log_cleaner;
@ -1299,10 +1302,7 @@ void LogFileObject::Write(bool force_flush,
LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7) {} LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7) {}
void LogCleaner::Enable(int overdue_days) { void LogCleaner::Enable(unsigned int overdue_days) {
// Setting overdue_days to 0 days will delete all logs.
assert(overdue_days >= 0);
enabled_ = true; enabled_ = true;
overdue_days_ = overdue_days; overdue_days_ = overdue_days;
} }
@ -1314,7 +1314,7 @@ void LogCleaner::Disable() {
void LogCleaner::Run(bool base_filename_selected, void LogCleaner::Run(bool base_filename_selected,
const string& base_filename, const string& base_filename,
const string& filename_extension) const { const string& filename_extension) const {
assert(enabled_ && overdue_days_ >= 0); assert(enabled_);
assert(!base_filename_selected || !base_filename.empty()); assert(!base_filename_selected || !base_filename.empty());
vector<string> dirs; vector<string> dirs;
@ -1344,7 +1344,7 @@ void LogCleaner::Run(bool base_filename_selected,
} }
vector<string> LogCleaner::GetOverdueLogNames(string log_directory, vector<string> LogCleaner::GetOverdueLogNames(string log_directory,
int days, unsigned int days,
const string& base_filename, const string& base_filename,
const string& filename_extension) const { const string& filename_extension) const {
// The names of overdue logs. // The names of overdue logs.
@ -1460,7 +1460,8 @@ bool LogCleaner::IsLogFromCurrentProject(const string& filepath,
return true; return true;
} }
bool LogCleaner::IsLogLastModifiedOver(const string& filepath, int days) const { bool LogCleaner::IsLogLastModifiedOver(const string& filepath,
unsigned int days) const {
// Try to get the last modified time of this file. // Try to get the last modified time of this file.
struct stat file_stat; struct stat file_stat;
@ -2550,7 +2551,7 @@ void ShutdownGoogleLogging() {
logging_directories_list = NULL; logging_directories_list = NULL;
} }
void EnableLogCleaner(int overdue_days) { void EnableLogCleaner(unsigned int overdue_days) {
log_cleaner.Enable(overdue_days); log_cleaner.Enable(overdue_days);
} }