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 all commits
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Andy Ying <andy@trailofbits.com>
Brian Silverman <bsilver16384@gmail.com>
Google Inc.
Guillaume Dumont <dumont.guillaume@gmail.com>
Marco Wang <m.aesophor@gmail.com>
Michael Tanner <michael@tannertaxpro.com>
MiniLight <MiniLightAR@Gmail.com>
romange <romange@users.noreply.github.com>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Guillaume Dumont <dumont.guillaume@gmail.com>
Håkan L. S. Younes <hyounes@google.com>
Ivan Penkov <ivanpe@google.com>
Jim Ray <jimray@google.com>
Marco Wang <m.aesophor@gmail.com>
Michael Tanner <michael@tannertaxpro.com>
MiniLight <MiniLightAR@Gmail.com>
Peter Collingbourne <pcc@google.com>
Expand Down
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
113 changes: 111 additions & 2 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h> // For uname.
#endif
#include <time.h>
#include <fcntl.h>
#include <cstdio>
#include <iostream>
Expand All @@ -58,6 +59,11 @@
#include <vector>
#include <errno.h> // for errno
#include <sstream>
#ifdef OS_WINDOWS
#include "windows/dirent.h"
#else
#include <dirent.h> // for automatic removal of old logs
#endif
#include "base/commandlineflags.h" // to get the program name
#include "glog/logging.h"
#include "glog/raw_logging.h"
Expand Down Expand Up @@ -827,6 +833,86 @@ void LogDestination::DeleteLogDestinations() {
sinks_ = NULL;
}

namespace {

bool IsGlogLog(const string& filename) {
// Check if filename matches the pattern of a glog file:
// "<program name>.<hostname>.<user name>.log...".
const int kKeywordCount = 4;
std::string keywords[kKeywordCount] = {
glog_internal_namespace_::ProgramInvocationShortName(),
LogDestination::hostname(),
MyUserName(),
"log"
};

int start_pos = 0;
for (int i = 0; i < kKeywordCount; i++) {
if (filename.find(keywords[i], start_pos) == filename.npos) {
return false;
}
start_pos += keywords[i].size() + 1;
}
return true;
}

bool LastModifiedOver(const string& filepath, int days) {
// Try to get the last modified time of this file.
struct stat file_stat;

if (stat(filepath.c_str(), &file_stat) == 0) {
// A day is 86400 seconds, so 7 days is 86400 * 7 = 604800 seconds.
time_t last_modified_time = file_stat.st_mtime;
time_t current_time = time(NULL);
return difftime(current_time, last_modified_time) > days * 86400;
}

// If failed to get file stat, don't return true!
return false;
}

vector<string> GetOverdueLogNames(string log_directory, int days) {
// The names of overdue logs.
vector<string> overdue_log_names;

// Try to get all files within log_directory.
DIR *dir;
struct dirent *ent;

char dir_delim = '/';
#ifdef OS_WINDOWS
dir_delim = '\\';
#endif

// If log_directory doesn't end with a slash, append a slash to it.
if (log_directory.at(log_directory.size() - 1) != dir_delim) {
log_directory += dir_delim;
}

if ((dir=opendir(log_directory.c_str()))) {
while ((ent=readdir(dir))) {
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
continue;
}
string filepath = log_directory + ent->d_name;
if (IsGlogLog(ent->d_name) && LastModifiedOver(filepath, days)) {
overdue_log_names.push_back(filepath);
}
}
closedir(dir);
}

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


namespace {

LogFileObject::LogFileObject(LogSeverity severity,
Expand Down Expand Up @@ -1085,7 +1171,7 @@ void LogFileObject::Write(bool force_flush,
file_length_ += header_len;
bytes_since_flush_ += header_len;
}

// Write to LOG file
if ( !stop_writing ) {
// fwrite() doesn't return an error when the disk is full, for
Expand Down Expand Up @@ -1130,12 +1216,21 @@ void LogFileObject::Write(bool force_flush,
}
}
#endif
// Perform clean up for old logs
if (log_cleaner_enabled_) {
const vector<string>& dirs = GetLoggingDirectories();
for (size_t i = 0; i < dirs.size(); i++) {
vector<string> logs = GetOverdueLogNames(dirs[i], log_cleaner_overdue_days_);
for (size_t j = 0; j < logs.size(); j++) {
static_cast<void>(unlink(logs[j].c_str()));
}
}
aesophor marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

} // namespace


// Static log data space to avoid alloc failures in a LOG(FATAL)
//
// Since multiple threads may call LOG(FATAL), and we want to preserve
Expand Down Expand Up @@ -2174,4 +2269,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_
Loading