Skip to content

Commit

Permalink
OverrideLogCleanerFilePathPrefix to override the files the log cleane…
Browse files Browse the repository at this point in the history
…r removes
  • Loading branch information
mturnock committed Mar 11, 2024
1 parent 31429d8 commit 28398b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/glog/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ InstallFailureFunction(logging_fail_func_t fail_func);
// Enable/Disable old log cleaner.
GLOG_EXPORT void EnableLogCleaner(const std::chrono::minutes& overdue);
GLOG_EXPORT void DisableLogCleaner();
GLOG_EXPORT void OverrideLogCleanerFilePathPrefix(const std::string& prefix);
GLOG_EXPORT void SetApplicationFingerprint(const std::string& fingerprint);

class LogSink; // defined below
Expand Down
33 changes: 27 additions & 6 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ class LogCleaner {
void Enable(const std::chrono::minutes& overdue);
void Disable();

void OverrideFilePathPrefix(const std::string& prefix);

void Run(const std::chrono::system_clock::time_point& current_time,
bool base_filename_selected, const string& base_filename,
const string& filename_extension);
Expand All @@ -464,6 +466,8 @@ class LogCleaner {
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
bool override_required_file_path_prefix_{false};
std::string required_file_path_prefix_;
};

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

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

void LogCleaner::OverrideFilePathPrefix(const std::string& prefix) {
override_required_file_path_prefix_ = true;
required_file_path_prefix_ = prefix;
}

void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time,
bool base_filename_selected, const string& base_filename,
const string& filename_extension) {
Expand All @@ -1309,17 +1318,20 @@ void LogCleaner::Run(const std::chrono::system_clock::time_point& current_time,

vector<string> dirs;

if (!base_filename_selected) {
dirs = GetLoggingDirectories();
} else {
size_t pos = base_filename.find_last_of(possible_dir_delim, string::npos,
sizeof(possible_dir_delim));
if (base_filename_selected || override_required_file_path_prefix_) {
const string base_path = override_required_file_path_prefix_
? required_file_path_prefix_
: base_filename;
size_t pos = base_path.find_last_of(possible_dir_delim, string::npos,
sizeof(possible_dir_delim));
if (pos != string::npos) {
string dir = base_filename.substr(0, pos + 1);
string dir = base_path.substr(0, pos + 1);
dirs.push_back(dir);
} else {
dirs.emplace_back(".");
}
} else {
dirs = GetLoggingDirectories();
}

for (const std::string& dir : dirs) {
Expand Down Expand Up @@ -1397,6 +1409,11 @@ bool LogCleaner::IsLogFromCurrentProject(
}
}

// If overriding log file path prefix, we only need to check this.
if (override_required_file_path_prefix_) {
return filepath.find(required_file_path_prefix_) == 0;
}

// Return early if the filename doesn't start with `cleaned_base_filename`.
if (filepath.find(cleaned_base_filename) != 0) {
return false;
Expand Down Expand Up @@ -2629,6 +2646,10 @@ void EnableLogCleaner(const std::chrono::minutes& overdue) {

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

void OverrideLogCleanerFilePathPrefix(const std::string& prefix) {
log_cleaner.OverrideFilePathPrefix(prefix);
}

LogMessageTime::LogMessageTime() = default;

namespace {
Expand Down

0 comments on commit 28398b7

Please sign in to comment.