From 71a04a14035a0618e32614ff76646d694fda84dd Mon Sep 17 00:00:00 2001
From: Lukasz Machutt <lukasz.machutt@gmail.com>
Date: Mon, 15 Jan 2024 13:02:33 +0100
Subject: [PATCH] fixed-islogfromcurrentproject func

---
 src/cleanup_immediately_unittest.cc          |  2 +-
 src/cleanup_with_relative_prefix_unittest.cc |  2 +-
 src/logging.cc                               | 19 +++++++++++--------
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/cleanup_immediately_unittest.cc b/src/cleanup_immediately_unittest.cc
index 5d970064d..84326a366 100644
--- a/src/cleanup_immediately_unittest.cc
+++ b/src/cleanup_immediately_unittest.cc
@@ -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();
diff --git a/src/cleanup_with_relative_prefix_unittest.cc b/src/cleanup_with_relative_prefix_unittest.cc
index 3d9ef33b5..74b9a4622 100644
--- a/src/cleanup_with_relative_prefix_unittest.cc
+++ b/src/cleanup_with_relative_prefix_unittest.cc
@@ -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();
diff --git a/src/logging.cc b/src/logging.cc
index 14385eb0a..52b76f895 100644
--- a/src/logging.cc
+++ b/src/logging.cc
@@ -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,
@@ -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;
@@ -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
+    }
   }
 }
 
@@ -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) {