Skip to content

Commit

Permalink
support truncate log file for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bqhuyy committed Oct 6, 2023
1 parent 857df01 commit 1dddaae
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include <cctype> // for std::isspace
#ifdef GLOG_OS_WINDOWS
#include "windows/dirent.h"
#include <io.h> // for truncate log file
#else
#include <dirent.h> // for automatic removal of old logs
#endif
Expand Down Expand Up @@ -2425,7 +2426,7 @@ void GetExistingTempDirectories(vector<string>* list) {
}

void TruncateLogFile(const char *path, uint64 limit, uint64 keep) {
#ifdef HAVE_UNISTD_H
#if defined(HAVE_UNISTD_H) || defined(GLOG_OS_WINDOWS)
struct stat statbuf;
const int kCopyBlockSize = 8 << 10;
char copybuf[kCopyBlockSize];
Expand All @@ -2446,7 +2447,11 @@ void TruncateLogFile(const char *path, uint64 limit, uint64 keep) {
// all of base/...) with -D_FILE_OFFSET_BITS=64 but that's
// rather scary.
// Instead just truncate the file to something we can manage
#ifdef GLOG_OS_WINDOWS
if (_chsize_s(fd, 0) != 0) {
#else
if (truncate(path, 0) == -1) {
#endif
PLOG(ERROR) << "Unable to truncate " << path;
} else {
LOG(ERROR) << "Truncated " << path << " due to EFBIG error";
Expand Down Expand Up @@ -2491,7 +2496,11 @@ void TruncateLogFile(const char *path, uint64 limit, uint64 keep) {
// Truncate the remainder of the file. If someone else writes to the
// end of the file after our last read() above, we lose their latest
// data. Too bad ...
#ifdef GLOG_OS_WINDOWS
if (_chsize_s(fd, write_offset) != 0) {
#else
if (ftruncate(fd, write_offset) == -1) {
#endif
PLOG(ERROR) << "Unable to truncate " << path;
}

Expand Down

0 comments on commit 1dddaae

Please sign in to comment.