@@ -337,6 +337,16 @@ const char* GetLogSeverityName(LogSeverity severity) {
337
337
return LogSeverityNames[severity];
338
338
}
339
339
340
+ int FindFilepathLogSeverity (const std::string& filepath) {
341
+ for (int i = GLOG_INFO; i < NUM_SEVERITIES; i++) {
342
+ if (filepath.rfind (GetLogSeverityName (static_cast <LogSeverity>(i))) != std::string::npos) {
343
+ return i;
344
+ }
345
+ }
346
+
347
+ return -1 ;
348
+ }
349
+
340
350
static bool SendEmailInternal (const char * dest, const char * subject,
341
351
const char * body, bool use_logging);
342
352
@@ -437,13 +447,15 @@ class LogCleaner {
437
447
438
448
// Setting overdue to 0 days will delete all logs.
439
449
void Enable (const std::chrono::minutes& overdue);
450
+ void Enable (const LogSeverity severity, const std::chrono::minutes& overdue);
440
451
void Disable ();
452
+ void Disable (const LogSeverity severity);
441
453
442
454
void Run (const std::chrono::system_clock::time_point& current_time,
443
455
bool base_filename_selected, const string& base_filename,
444
456
const string& filename_extension);
445
457
446
- bool enabled () const { return enabled_; }
458
+ bool enabled () const ;
447
459
448
460
private:
449
461
vector<string> GetOverdueLogNames (
@@ -459,9 +471,7 @@ class LogCleaner {
459
471
const string& filepath,
460
472
const std::chrono::system_clock::time_point& current_time) const ;
461
473
462
- bool enabled_{false };
463
- std::chrono::minutes overdue_{
464
- std::chrono::duration<int , std::ratio<kSecondsInWeek >>{1 }};
474
+ vector<std::chrono::minutes> overdue_per_severity_;
465
475
std::chrono::system_clock::time_point
466
476
next_cleanup_time_; // cycle count at which to clean overdue log
467
477
};
@@ -1282,19 +1292,38 @@ void LogFileObject::Write(
1282
1292
}
1283
1293
}
1284
1294
1285
- LogCleaner::LogCleaner () = default ;
1295
+ LogCleaner::LogCleaner () {
1296
+ overdue_per_severity_.resize (NUM_SEVERITIES, std::chrono::minutes::max ());
1297
+ }
1298
+
1299
+ bool LogCleaner::enabled () const {
1300
+ // return true if any severity is enabled by iterating over the array
1301
+ for (int i = 0 ; i < NUM_SEVERITIES; i++) {
1302
+ if (overdue_per_severity_[i] != std::chrono::minutes::max ()) return true ;
1303
+ }
1304
+
1305
+ return false ;
1306
+ }
1286
1307
1287
1308
void LogCleaner::Enable (const std::chrono::minutes& overdue) {
1288
- enabled_ = true ;
1289
- overdue_ = overdue;
1309
+ // for backward compatability, set all severities to the same value
1310
+ fill (overdue_per_severity_.begin (), overdue_per_severity_.end (), overdue);
1311
+ }
1312
+
1313
+ void LogCleaner::Enable (const LogSeverity severity, const std::chrono::minutes& overdue) {
1314
+ overdue_per_severity_[severity] = overdue;
1290
1315
}
1291
1316
1292
- void LogCleaner::Disable () { enabled_ = false ; }
1317
+ void LogCleaner::Disable () { fill (overdue_per_severity_.begin (), overdue_per_severity_.end (), std::chrono::minutes::max ()); }
1318
+
1319
+ void LogCleaner::Disable (const LogSeverity severity) {
1320
+ overdue_per_severity_[severity] = std::chrono::minutes::max ();
1321
+ }
1293
1322
1294
1323
void LogCleaner::Run (const std::chrono::system_clock::time_point& current_time,
1295
1324
bool base_filename_selected, const string& base_filename,
1296
1325
const string& filename_extension) {
1297
- assert (enabled_ );
1326
+ assert (enabled () );
1298
1327
assert (!base_filename_selected || !base_filename.empty ());
1299
1328
1300
1329
// avoid scanning logs too frequently
@@ -1471,7 +1500,15 @@ bool LogCleaner::IsLogLastModifiedOver(
1471
1500
const auto last_modified_time =
1472
1501
std::chrono::system_clock::from_time_t (file_stat.st_mtime );
1473
1502
const auto diff = current_time - last_modified_time;
1474
- return diff >= overdue_;
1503
+
1504
+ int severity = FindFilepathLogSeverity (filepath);
1505
+ // if the filepath does not have a severity, cant clean it
1506
+ if (severity < 0 ) {
1507
+ perror ((" Cannot clean the file. No severity found for: " + filepath).c_str ());
1508
+ return false ;
1509
+ }
1510
+
1511
+ return diff >= overdue_per_severity_[severity];
1475
1512
}
1476
1513
1477
1514
// If failed to get file stat, don't return true!
@@ -2627,6 +2664,16 @@ void EnableLogCleaner(const std::chrono::minutes& overdue) {
2627
2664
log_cleaner.Enable (overdue);
2628
2665
}
2629
2666
2667
+ void EnableLogCleaner (LogSeverity severity, unsigned int overdue_days) {
2668
+ log_cleaner.Enable (severity, std::chrono::duration_cast<std::chrono::minutes>(
2669
+ std::chrono::duration<unsigned , std::ratio<kSecondsInDay >>{
2670
+ overdue_days}));
2671
+ }
2672
+
2673
+ void EnableLogCleaner (LogSeverity severity, const std::chrono::minutes& overdue) {
2674
+ log_cleaner.Enable (severity, overdue);
2675
+ }
2676
+
2630
2677
void DisableLogCleaner () { log_cleaner.Disable (); }
2631
2678
2632
2679
LogMessageTime::LogMessageTime () = default ;
0 commit comments