Skip to content

Commit

Permalink
fixed-islogfromcurrentproject func
Browse files Browse the repository at this point in the history
  • Loading branch information
Mivekk committed Jan 31, 2024
1 parent 489d35e commit 71a04a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/cleanup_immediately_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ TEST(CleanImmediately, logging) {
google::EnableLogCleaner(0h);

for (unsigned i = 0; i < 1000; ++i) {
LOG(INFO) << "cleanup test";
LOG(WARNING) << "cleanup test";
}

google::DisableLogCleaner();
Expand Down
2 changes: 1 addition & 1 deletion src/cleanup_with_relative_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST(CleanImmediatelyWithRelativePrefix, logging) {
google::SetLogDestination(GLOG_INFO, "test_subdir/test_cleanup_");

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

google::DisableLogCleaner();
Expand Down
19 changes: 11 additions & 8 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ class LogCleaner {

bool enabled() const { return enabled_; }

std::chrono::system_clock::time_point
next_cleanup_time; // cycle count at which to clean overdue log

private:
vector<string> GetOverdueLogNames(
string log_directory,
Expand All @@ -463,8 +466,6 @@ class LogCleaner {
bool enabled_{false};
std::chrono::minutes overdue_{
std::chrono::duration<int, std::ratio<kSecondsInWeek>>{1}};
std::chrono::system_clock::time_point
next_cleanup_time_; // cycle count at which to clean overdue log
};

LogCleaner log_cleaner;
Expand Down Expand Up @@ -855,6 +856,13 @@ inline void LogDestination::LogToAllLogfiles(
LogDestination::MaybeLogToLogfile(static_cast<LogSeverity>(i), timestamp,
message, len);
}

// avoid scanning logs too frequently
if (timestamp >= log_cleaner.next_cleanup_time) {
log_cleaner.next_cleanup_time = timestamp +
std::chrono::duration_cast<std::chrono::system_clock::duration>(
std::chrono::duration<int32>{FLAGS_logcleansecs}); // set next cleanup time
}
}
}

Expand Down Expand Up @@ -1317,15 +1325,10 @@ void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time,
assert(!base_filename_selected || !base_filename.empty());

// avoid scanning logs too frequently
if (current_time < next_cleanup_time_) {
if (current_time < next_cleanup_time) {
return;
}

next_cleanup_time_ =
current_time +
std::chrono::duration_cast<std::chrono::system_clock::duration>(
std::chrono::duration<int32>{FLAGS_logcleansecs});

vector<string> dirs;

if (!base_filename_selected) {
Expand Down

0 comments on commit 71a04a1

Please sign in to comment.