Skip to content

Commit

Permalink
use uint32 for --max-log-size
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Dec 29, 2021
1 parent 43fc3bf commit b3abfaa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
14 changes: 14 additions & 0 deletions src/base/commandlineflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
#define DEFINE_int32(name, value, meaning) \
DEFINE_VARIABLE(GOOGLE_NAMESPACE::int32, I, name, value, meaning, int32)

// uint32 specialization
#ifndef DECLARE_uint32
#define DECLARE_uint32(name) \
DECLARE_VARIABLE(GOOGLE_NAMESPACE::uint32, U, name, uint32)
#endif // DECLARE_uint64
#define DEFINE_uint32(name, value, meaning) \
DEFINE_VARIABLE(GOOGLE_NAMESPACE::uint32, U, name, value, meaning, uint32)

// Special case for string, because we have to specify the namespace
// std::string, which doesn't play nicely with our FLAG__namespace hackery.
#define DECLARE_string(name) \
Expand Down Expand Up @@ -115,6 +123,9 @@
#define GLOG_DEFINE_int32(name, value, meaning) \
DEFINE_int32(name, EnvToInt("GLOG_" #name, value), meaning)

#define GLOG_DEFINE_uint32(name, value, meaning) \
DEFINE_uint32(name, EnvToUInt("GLOG_" #name, value), meaning)

#define GLOG_DEFINE_string(name, value, meaning) \
DEFINE_string(name, EnvToString("GLOG_" #name, value), meaning)

Expand All @@ -130,4 +141,7 @@
#define EnvToInt(envname, dflt) \
(!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10))

#define EnvToUInt(envname, dflt) \
(!getenv(envname) ? (dflt) : strtoul(getenv(envname), NULL, 10))

#endif // BASE_COMMANDLINEFLAGS_H__
11 changes: 9 additions & 2 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ typedef void(*CustomPrefixCallback)(std::ostream& s, const LogMessageInfo& l, vo
#define DECLARE_int32(name) \
DECLARE_VARIABLE(@ac_google_namespace@::int32, I, name, int32)

#if !defined(DECLARE_uint32) && !(@ac_cv_have_libgflags@)
// uint32 specialization
#define DECLARE_uint32(name) \
DECLARE_VARIABLE(@ac_google_namespace@::uint32, U, name, uint32)
#endif // !defined(DECLARE_uint32) && !(@ac_cv_have_libgflags@)

// Special case for string, because we have to specify the namespace
// std::string, which doesn't play nicely with our FLAG__namespace hackery.
#define DECLARE_string(name) \
Expand Down Expand Up @@ -460,7 +466,7 @@ DECLARE_int32(v); // in vlog_is_on.cc
DECLARE_string(vmodule); // also in vlog_is_on.cc

// Sets the maximum log file size (in MB).
DECLARE_int32(max_log_size);
DECLARE_uint32(max_log_size);

// Sets whether to avoid logging to the disk if the disk is full.
DECLARE_bool(stop_logging_if_full_disk);
Expand All @@ -473,6 +479,7 @@ DECLARE_bool(log_utc_time);
#undef DECLARE_VARIABLE
#undef DECLARE_bool
#undef DECLARE_int32
#undef DECLARE_uint32
#undef DECLARE_string
#endif

Expand Down Expand Up @@ -1831,7 +1838,7 @@ GOOGLE_GLOG_DLL_DECL void ReprintFatalMessage();
// lose very small amounts of data. For security, only follow symlinks
// if the path is /proc/self/fd/*
GOOGLE_GLOG_DLL_DECL void TruncateLogFile(const char *path,
int64 limit, int64 keep);
uint64 limit, uint64 keep);

// Truncate stdout and stderr if they are over the value specified by
// --max_log_size; keep the final 1MB. This function has the same
Expand Down
38 changes: 20 additions & 18 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ GLOG_DEFINE_string(log_dir, DefaultLogDir(),
GLOG_DEFINE_string(log_link, "", "Put additional links to the log "
"files in this directory");

GLOG_DEFINE_int32(max_log_size, 1800,
"approx. maximum log file size (in MB). A value of 0 will "
"be silently overridden to 1.");
GLOG_DEFINE_uint32(max_log_size, 1800,
"approx. maximum log file size (in MB). A value of 0 will "
"be silently overridden to 1.");

GLOG_DEFINE_bool(stop_logging_if_full_disk, false,
"Stop attempting to log to disk if the disk is full.");
Expand Down Expand Up @@ -342,8 +342,10 @@ static const char* GetAnsiColorCode(GLogColor color) {
#endif // GLOG_OS_WINDOWS

// Safely get max_log_size, overriding to 1 if it somehow gets defined as 0
static int32 MaxLogSize() {
return (FLAGS_max_log_size > 0 && FLAGS_max_log_size < 4096 ? FLAGS_max_log_size : 1);
static uint32 MaxLogSize() {
return (FLAGS_max_log_size > 0 && FLAGS_max_log_size < 4096
? FLAGS_max_log_size
: 1);
}

// An arbitrary limit on the length of a single log message. This
Expand Down Expand Up @@ -1114,12 +1116,11 @@ void LogFileObject::Write(bool force_flush,
return;
}

if (static_cast<int>(file_length_ >> 20) >= MaxLogSize() ||
PidHasChanged()) {
if (file_length_ >> 20U >= MaxLogSize() || PidHasChanged()) {
if (file_ != NULL) fclose(file_);
file_ = NULL;
file_length_ = bytes_since_flush_ = dropped_mem_length_ = 0;
rollover_attempt_ = kRolloverAttemptFrequency-1;
rollover_attempt_ = kRolloverAttemptFrequency - 1;
}

// If there's no destination file, make one before outputting
Expand Down Expand Up @@ -1273,12 +1274,13 @@ void LogFileObject::Write(bool force_flush,
FlushUnlocked();
#ifdef GLOG_OS_LINUX
// Only consider files >= 3MiB
if (FLAGS_drop_log_memory && file_length_ >= (3 << 20)) {
if (FLAGS_drop_log_memory && file_length_ >= (3U << 20U)) {
// Don't evict the most recent 1-2MiB so as not to impact a tailer
// of the log file and to avoid page rounding issue on linux < 4.7
uint32 total_drop_length = (file_length_ & ~((1 << 20U) - 1U)) - (1U << 20U);
uint32 total_drop_length =
(file_length_ & ~((1U << 20U) - 1U)) - (1U << 20U);
uint32 this_drop_length = total_drop_length - dropped_mem_length_;
if (this_drop_length >= (2 << 20)) {
if (this_drop_length >= (2U << 20U)) {
// Only advise when >= 2MiB to drop
# if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
// 'posix_fadvise' introduced in API 21:
Expand Down Expand Up @@ -2325,7 +2327,7 @@ void GetExistingTempDirectories(vector<string>* list) {
}
}

void TruncateLogFile(const char *path, int64 limit, int64 keep) {
void TruncateLogFile(const char *path, uint64 limit, uint64 keep) {
#ifdef HAVE_UNISTD_H
struct stat statbuf;
const int kCopyBlockSize = 8 << 10;
Expand Down Expand Up @@ -2366,14 +2368,14 @@ void TruncateLogFile(const char *path, int64 limit, int64 keep) {
// See if the path refers to a regular file bigger than the
// specified limit
if (!S_ISREG(statbuf.st_mode)) goto out_close_fd;
if (statbuf.st_size <= limit) goto out_close_fd;
if (statbuf.st_size <= keep) goto out_close_fd;
if (statbuf.st_size <= static_cast<off_t>(limit)) goto out_close_fd;
if (statbuf.st_size <= static_cast<off_t>(keep)) goto out_close_fd;

// This log file is too large - we need to truncate it
LOG(INFO) << "Truncating " << path << " to " << keep << " bytes";

// Copy the last "keep" bytes of the file to the beginning of the file
read_offset = statbuf.st_size - keep;
read_offset = statbuf.st_size - static_cast<off_t>(keep);
write_offset = 0;
ssize_t bytesin, bytesout;
while ((bytesin = pread(fd, copybuf, sizeof(copybuf), read_offset)) > 0) {
Expand Down Expand Up @@ -2401,12 +2403,12 @@ void TruncateLogFile(const char *path, int64 limit, int64 keep) {
#else
LOG(ERROR) << "No log truncation support.";
#endif
}
}

void TruncateStdoutStderr() {
#ifdef HAVE_UNISTD_H
int64 limit = MaxLogSize() << 20;
int64 keep = 1 << 20;
uint64 limit = MaxLogSize() << 20U;
uint64 keep = 1U << 20U;
TruncateLogFile("/proc/self/fd/1", limit, keep);
TruncateLogFile("/proc/self/fd/2", limit, keep);
#else
Expand Down
7 changes: 4 additions & 3 deletions src/logging_custom_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ static void TestErrno() {
CHECK_EQ(errno, ENOENT);
}

static void TestOneTruncate(const char *path, int64 limit, int64 keep,
static void TestOneTruncate(const char *path, uint64 limit, uint64 keep,
size_t dsize, size_t ksize, size_t expect) {
int fd;
CHECK_ERR(fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600));
Expand Down Expand Up @@ -909,7 +909,7 @@ static void TestOneTruncate(const char *path, int64 limit, int64 keep,
memset(buf, 0, buf_size);
CHECK_ERR(read(fd, buf, buf_size));

const char *p = buf;
const char* p = buf;
size_t checked = 0;
while (checked < expect) {
size_t bytes = min(expect - checked, keep_size);
Expand All @@ -929,7 +929,8 @@ static void TestTruncate() {
TestOneTruncate(path.c_str(), 10, 10, 10, 10, 10);

// And a big file (multiple blocks to copy)
TestOneTruncate(path.c_str(), 2<<20, 4<<10, 3<<20, 4<<10, 4<<10);
TestOneTruncate(path.c_str(), 2U << 20U, 4U << 10U, 3U << 20U, 4U << 10U,
4U << 10U);

// Check edge-case limits
TestOneTruncate(path.c_str(), 10, 20, 0, 20, 20);
Expand Down
7 changes: 4 additions & 3 deletions src/logging_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ static void TestErrno() {
CHECK_EQ(errno, ENOENT);
}

static void TestOneTruncate(const char *path, int64 limit, int64 keep,
static void TestOneTruncate(const char *path, uint64 limit, uint64 keep,
size_t dsize, size_t ksize, size_t expect) {
int fd;
CHECK_ERR(fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600));
Expand Down Expand Up @@ -904,7 +904,7 @@ static void TestOneTruncate(const char *path, int64 limit, int64 keep,
memset(buf, 0, buf_size);
CHECK_ERR(read(fd, buf, buf_size));

const char *p = buf;
const char* p = buf;
size_t checked = 0;
while (checked < expect) {
size_t bytes = min(expect - checked, keep_size);
Expand All @@ -924,7 +924,8 @@ static void TestTruncate() {
TestOneTruncate(path.c_str(), 10, 10, 10, 10, 10);

// And a big file (multiple blocks to copy)
TestOneTruncate(path.c_str(), 2<<20, 4<<10, 3<<20, 4<<10, 4<<10);
TestOneTruncate(path.c_str(), 2U << 20U, 4U << 10U, 3U << 20U, 4U << 10U,
4U << 10U);

// Check edge-case limits
TestOneTruncate(path.c_str(), 10, 20, 0, 20, 20);
Expand Down

0 comments on commit b3abfaa

Please sign in to comment.