Skip to content

Commit

Permalink
LogCleaner: avoid scanning logs too frequently
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyWu-fdgkhdkgh committed Dec 31, 2021
1 parent a8cfbe0 commit e7ccdf9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/cleanup_with_absolute_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ TEST(CleanImmediatelyWithAbsolutePrefix, logging) {
LOG(INFO) << "cleanup test";
}

for (unsigned i = 0; i < 10; ++i) {
LOG(ERROR) << "cleanup test";
}

google::DisableLogCleaner();
}

Expand Down
26 changes: 23 additions & 3 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ GLOG_DEFINE_int32(logbuflevel, 0,
" ...)");
GLOG_DEFINE_int32(logbufsecs, 30,
"Buffer log messages for at most this many seconds");

GLOG_DEFINE_int32(logcleansecs, 300,
"Clean overdue logs every this many seconds");

GLOG_DEFINE_int32(logemaillevel, 999,
"Email log messages logged at this level or higher"
" (0 means email all; 3 means email FATAL only;"
Expand Down Expand Up @@ -484,9 +488,12 @@ class LogCleaner {
void Enable(unsigned int overdue_days);
void Disable();

// update next_cleanup_time_
void UpdateCleanUpTime();

void Run(bool base_filename_selected,
const string& base_filename,
const string& filename_extension) const;
const string& filename_extension);

bool enabled() const { return enabled_; }

Expand All @@ -503,6 +510,7 @@ class LogCleaner {

bool enabled_;
unsigned int overdue_days_;
int64 next_cleanup_time_; // cycle count at which to clean overdue log
};

LogCleaner log_cleaner;
Expand Down Expand Up @@ -1303,7 +1311,7 @@ void LogFileObject::Write(bool force_flush,
}
}

LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7) {}
LogCleaner::LogCleaner() : enabled_(false), overdue_days_(7), next_cleanup_time_(0) {}

void LogCleaner::Enable(unsigned int overdue_days) {
enabled_ = true;
Expand All @@ -1314,12 +1322,24 @@ void LogCleaner::Disable() {
enabled_ = false;
}

void LogCleaner::UpdateCleanUpTime() {
const int64 next = (FLAGS_logcleansecs
* static_cast<int64>(1000000)); // in usec
next_cleanup_time_ = CycleClock_Now() + UsecToCycles(next);
}

void LogCleaner::Run(bool base_filename_selected,
const string& base_filename,
const string& filename_extension) const {
const string& filename_extension) {
assert(enabled_);
assert(!base_filename_selected || !base_filename.empty());

// avoid scanning logs too frequently
if (CycleClock_Now() < next_cleanup_time_) {
return;
}
UpdateCleanUpTime();

vector<string> dirs;

if (!base_filename_selected) {
Expand Down

0 comments on commit e7ccdf9

Please sign in to comment.