Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 55e6f47

Browse files
committed
support per-severity overdue settings
1 parent 6c5c692 commit 55e6f47

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

src/logging.cc

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ const char* GetLogSeverityName(LogSeverity severity) {
337337
return LogSeverityNames[severity];
338338
}
339339

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+
340350
static bool SendEmailInternal(const char* dest, const char* subject,
341351
const char* body, bool use_logging);
342352

@@ -437,13 +447,15 @@ class LogCleaner {
437447

438448
// Setting overdue to 0 days will delete all logs.
439449
void Enable(const std::chrono::minutes& overdue);
450+
void Enable(const LogSeverity severity, const std::chrono::minutes& overdue);
440451
void Disable();
452+
void Disable(const LogSeverity severity);
441453

442454
void Run(const std::chrono::system_clock::time_point& current_time,
443455
bool base_filename_selected, const string& base_filename,
444456
const string& filename_extension);
445457

446-
bool enabled() const { return enabled_; }
458+
bool enabled() const;
447459

448460
private:
449461
vector<string> GetOverdueLogNames(
@@ -459,9 +471,7 @@ class LogCleaner {
459471
const string& filepath,
460472
const std::chrono::system_clock::time_point& current_time) const;
461473

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_;
465475
std::chrono::system_clock::time_point
466476
next_cleanup_time_; // cycle count at which to clean overdue log
467477
};
@@ -1282,19 +1292,38 @@ void LogFileObject::Write(
12821292
}
12831293
}
12841294

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+
}
12861307

12871308
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;
12901315
}
12911316

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+
}
12931322

12941323
void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time,
12951324
bool base_filename_selected, const string& base_filename,
12961325
const string& filename_extension) {
1297-
assert(enabled_);
1326+
assert(enabled());
12981327
assert(!base_filename_selected || !base_filename.empty());
12991328

13001329
// avoid scanning logs too frequently
@@ -1471,7 +1500,15 @@ bool LogCleaner::IsLogLastModifiedOver(
14711500
const auto last_modified_time =
14721501
std::chrono::system_clock::from_time_t(file_stat.st_mtime);
14731502
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];
14751512
}
14761513

14771514
// If failed to get file stat, don't return true!
@@ -2627,6 +2664,16 @@ void EnableLogCleaner(const std::chrono::minutes& overdue) {
26272664
log_cleaner.Enable(overdue);
26282665
}
26292666

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+
26302677
void DisableLogCleaner() { log_cleaner.Disable(); }
26312678

26322679
LogMessageTime::LogMessageTime() = default;

0 commit comments

Comments
 (0)