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

Commit 5a2cb2a

Browse files
committed
Enable log cleaner to clean files of all verbosities
Add OverrideLogCleanerFilePathPrefix to override the files the log cleaner removes
1 parent 31429d8 commit 5a2cb2a

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/glog/logging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ InstallFailureFunction(logging_fail_func_t fail_func);
484484
// Enable/Disable old log cleaner.
485485
GLOG_EXPORT void EnableLogCleaner(const std::chrono::minutes& overdue);
486486
GLOG_EXPORT void DisableLogCleaner();
487+
GLOG_EXPORT void OverrideLogCleanerFilePathPrefix(const std::string& prefix);
487488
GLOG_EXPORT void SetApplicationFingerprint(const std::string& fingerprint);
488489

489490
class LogSink; // defined below

src/logging.cc

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ class LogCleaner {
439439
void Enable(const std::chrono::minutes& overdue);
440440
void Disable();
441441

442+
void OverrideFilePathPrefix(const std::string& prefix);
443+
442444
void Run(const std::chrono::system_clock::time_point& current_time,
443445
bool base_filename_selected, const string& base_filename,
444446
const string& filename_extension);
@@ -464,6 +466,8 @@ class LogCleaner {
464466
std::chrono::duration<int, std::ratio<kSecondsInWeek>>{1}};
465467
std::chrono::system_clock::time_point
466468
next_cleanup_time_; // cycle count at which to clean overdue log
469+
bool override_required_file_path_prefix_{false};
470+
std::string required_file_path_prefix_;
467471
};
468472

469473
LogCleaner log_cleaner;
@@ -1291,6 +1295,11 @@ void LogCleaner::Enable(const std::chrono::minutes& overdue) {
12911295

12921296
void LogCleaner::Disable() { enabled_ = false; }
12931297

1298+
void LogCleaner::OverrideFilePathPrefix(const std::string& prefix) {
1299+
override_required_file_path_prefix_ = true;
1300+
required_file_path_prefix_ = prefix;
1301+
}
1302+
12941303
void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time,
12951304
bool base_filename_selected, const string& base_filename,
12961305
const string& filename_extension) {
@@ -1309,17 +1318,20 @@ void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time,
13091318

13101319
vector<string> dirs;
13111320

1312-
if (!base_filename_selected) {
1313-
dirs = GetLoggingDirectories();
1314-
} else {
1315-
size_t pos = base_filename.find_last_of(possible_dir_delim, string::npos,
1316-
sizeof(possible_dir_delim));
1321+
if (base_filename_selected || override_required_file_path_prefix_) {
1322+
const string base_path = override_required_file_path_prefix_
1323+
? required_file_path_prefix_
1324+
: base_filename;
1325+
size_t pos = base_path.find_last_of(possible_dir_delim, string::npos,
1326+
sizeof(possible_dir_delim));
13171327
if (pos != string::npos) {
1318-
string dir = base_filename.substr(0, pos + 1);
1328+
string dir = base_path.substr(0, pos + 1);
13191329
dirs.push_back(dir);
13201330
} else {
13211331
dirs.emplace_back(".");
13221332
}
1333+
} else {
1334+
dirs = GetLoggingDirectories();
13231335
}
13241336

13251337
for (const std::string& dir : dirs) {
@@ -1377,6 +1389,22 @@ vector<string> LogCleaner::GetOverdueLogNames(
13771389
bool LogCleaner::IsLogFromCurrentProject(
13781390
const string& filepath, const string& base_filename,
13791391
const string& filename_extension) const {
1392+
// If overriding log file path prefix, check if the file path starts with
1393+
// 'required_file_path_prefix_' and ends with `filename_extension`.
1394+
if (override_required_file_path_prefix_) {
1395+
if (filepath.find(required_file_path_prefix_) != 0) {
1396+
return false;
1397+
}
1398+
if (filename_extension.empty()) {
1399+
return true;
1400+
}
1401+
if (filepath.size() < filename_extension.size()) {
1402+
return false;
1403+
}
1404+
return filename_extension ==
1405+
filepath.substr(filepath.size() - filename_extension.size());
1406+
}
1407+
13801408
// We should remove duplicated delimiters from `base_filename`, e.g.,
13811409
// before: "/tmp//<base_filename>.<create_time>.<pid>"
13821410
// after: "/tmp/<base_filename>.<create_time>.<pid>"
@@ -2629,6 +2657,10 @@ void EnableLogCleaner(const std::chrono::minutes& overdue) {
26292657

26302658
void DisableLogCleaner() { log_cleaner.Disable(); }
26312659

2660+
void OverrideLogCleanerFilePathPrefix(const std::string& prefix) {
2661+
log_cleaner.OverrideFilePathPrefix(prefix);
2662+
}
2663+
26322664
LogMessageTime::LogMessageTime() = default;
26332665

26342666
namespace {

0 commit comments

Comments
 (0)