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 5 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
100 changes: 98 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,91 @@ void LogDestination::DeleteLogDestinations() {
sinks_ = NULL;
}

namespace {

vector<string> SplitString(const string& s, const char delimiter) {
std::stringstream ss(s);
string t;
vector<string> tokens;

while (std::getline(ss, t, delimiter)) {
if (t.length() > 0) {
tokens.push_back(t);
}
}
return tokens;
}

bool IsGlogLog(const string& log_name) {
static const int kGlogFilenameTokenCount = 6;
vector<string> log_name_tokens = SplitString(log_name, '.');
aesophor marked this conversation as resolved.
Show resolved Hide resolved
aesophor marked this conversation as resolved.
Show resolved Hide resolved

if (log_name_tokens.size() < kGlogFilenameTokenCount) {
return false;
}

// e.g., /tmp/webserver.examplehost.root.log......
// here we should erase "/tmp/" from the first token,
// so that we can compare it with the program name later!
log_name_tokens[0].erase(0, log_name_tokens[0].find_last_of('/') + 1); // including last '/'

// Check if log_name matches the pattern
// "<program name>.<hostname>.<user name>.log.<severity level>.".
return log_name_tokens[0] == glog_internal_namespace_::ProgramInvocationShortName()
&& log_name_tokens[1] == LogDestination::hostname()
&& log_name_tokens[2] == MyUserName()
&& log_name_tokens[3] == "log"
&& (log_name_tokens[4] == "INFO"
|| log_name_tokens[4] == "ERROR"
|| log_name_tokens[4] == "WARNING");
}

bool LastModifiedOver(const string& log_name, int days) {
// Try to get the last modified time of log.
struct stat log_stat;

if(stat(log_name.c_str(), &log_stat) == 0) {
// A day is 86400 seconds, so 7 days is 86400 * 7 = 604800 seconds.
time_t last_modified_time = log_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;

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

if ((dir=opendir(log_directory.c_str()))) {
while ((ent=readdir(dir))) {
string filename = log_directory + ent->d_name;
if (IsGlogLog(filename) && LastModifiedOver(filename, days)) {
overdue_log_names.push_back(filename);
}
}
closedir(dir);
} else {
perror("Unable to open directory.");
}

return overdue_log_names;
}

} // namespace


namespace {

LogFileObject::LogFileObject(LogSeverity severity,
Expand Down Expand Up @@ -1085,7 +1176,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 +1221,17 @@ 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()));
}
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
Loading