Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for automatic removal of old logs #432

Merged
merged 14 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ GOOGLE_GLOG_DLL_DECL void ShutdownGoogleLogging();
// Install a function which will be called after LOG(FATAL).
GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(void (*fail_func)());

// Enable/Disable old log cleaner.
GOOGLE_GLOG_DLL_DECL void EnableLogCleaner(int overdue_days);
GOOGLE_GLOG_DLL_DECL void DisableLogCleaner();


class LogSink; // defined below

// If a non-NULL sink pointer is given, we push this message to that sink.
Expand Down
27 changes: 24 additions & 3 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,11 @@ vector<string> GetOverdueLogNames(string log_directory, int days) {
return overdue_log_names;
}

// Is log_cleaner enabled?
// This option can be enabled by calling google::EnableLogCleaner(days)
bool log_cleaner_enabled_;
int log_cleaner_overdue_days_ = 7;

} // namespace


Expand Down Expand Up @@ -1222,9 +1227,11 @@ void LogFileObject::Write(bool force_flush,
}
#endif
// Perform clean up for old logs
for (const auto& dir : GetLoggingDirectories()) {
for (const auto& name : GetOverdueLogNames(dir, 3)) {
static_cast<void>(unlink(name.c_str()));
if (log_cleaner_enabled_) {
for (const auto& dir : GetLoggingDirectories()) {
for (const auto& name : GetOverdueLogNames(dir, 3)) {
aesophor marked this conversation as resolved.
Show resolved Hide resolved
static_cast<void>(unlink(name.c_str()));
}
}
aesophor marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down Expand Up @@ -2270,4 +2277,18 @@ void ShutdownGoogleLogging() {
logging_directories_list = NULL;
}

void EnableLogCleaner(int overdue_days) {
log_cleaner_enabled_ = true;

// Setting overdue_days to 0 day should not be allowed!
// Since all logs will be deleted immediately, which will cause troubles.
if (overdue_days > 0) {
log_cleaner_overdue_days_ = overdue_days;
}
}

void DisableLogCleaner() {
log_cleaner_overdue_days_ = false;
}

_END_GOOGLE_NAMESPACE_