@@ -110,6 +110,7 @@ static void TestLogSinkWaitTillSent();
110
110
static void TestCHECK ();
111
111
static void TestDCHECK ();
112
112
static void TestSTREQ ();
113
+ static void TestMaxLogSizeWhenNoTimestamp ();
113
114
static void TestBasename ();
114
115
static void TestBasenameAppendWhenNoTimestamp ();
115
116
static void TestTwoProcessesWrite ();
@@ -288,6 +289,7 @@ int main(int argc, char** argv) {
288
289
MungeAndDiffTestStdout (FLAGS_test_srcdir + " /src/logging_unittest.out" ));
289
290
FLAGS_logtostdout = false ;
290
291
292
+ TestMaxLogSizeWhenNoTimestamp ();
291
293
TestBasename ();
292
294
TestBasenameAppendWhenNoTimestamp ();
293
295
TestTwoProcessesWrite ();
@@ -806,6 +808,47 @@ static void CheckFile(const string& name, const string& expected_string,
806
808
<< expected_string << " in " << files[0 ];
807
809
}
808
810
811
+ static void TestMaxLogSizeWhenNoTimestamp () {
812
+ fprintf (stderr, " ==== Test setting max log size without timestamp\n " );
813
+ const string dest = FLAGS_test_tmpdir + " /logging_test_max_log_size" ;
814
+ DeleteFiles (dest + " *" );
815
+
816
+ auto original_max_log_size = FLAGS_max_log_size;
817
+ auto original_timestamp_in_logfile_name = FLAGS_timestamp_in_logfile_name;
818
+
819
+ FLAGS_max_log_size = 1 ; // Set max log size to 1MB
820
+ FLAGS_timestamp_in_logfile_name = false ;
821
+
822
+ // Set log destination
823
+ SetLogDestination (GLOG_INFO, dest.c_str ());
824
+
825
+ // 1e4 info logs -> is about 772 KB in size
826
+ // 2e4 info logs -> is around 1500 KB in size -> 1.5MB
827
+ // If our max_log_size constraint is respected, it will truncate earlier logs
828
+ // and the file size will be lesser than 1MB (around 0.5MB)
829
+ const int num_logs = 2e4 ;
830
+ for (int i = 0 ; i < num_logs; i++) {
831
+ LOG (INFO) << " Hello world" ;
832
+ }
833
+ FlushLogFiles (GLOG_INFO);
834
+
835
+ // Check log file size
836
+ struct stat statbuf;
837
+ stat (dest.c_str (), &statbuf);
838
+
839
+ // Verify file size is less than the max log size limit
840
+ CHECK_LT (static_cast <unsigned int >(statbuf.st_size ),
841
+ FLAGS_max_log_size << 20U );
842
+
843
+ // Reset flag values to their original values
844
+ FLAGS_max_log_size = original_max_log_size;
845
+ FLAGS_timestamp_in_logfile_name = original_timestamp_in_logfile_name;
846
+
847
+ // Release file handle for the destination file to unlock the file in Windows.
848
+ LogToStderr ();
849
+ DeleteFiles (dest + " *" );
850
+ }
851
+
809
852
static void TestBasename () {
810
853
fprintf (stderr, " ==== Test setting log file basename\n " );
811
854
const string dest = FLAGS_test_tmpdir + " /logging_test_basename" ;
0 commit comments