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

Support truncate log file for windows #960

Merged
merged 3 commits into from
Oct 8, 2023
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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ check_function_exists (sigaltstack HAVE_SIGALTSTACK)
check_cxx_symbol_exists (backtrace execinfo.h HAVE_EXECINFO_BACKTRACE)
check_cxx_symbol_exists (backtrace_symbols execinfo.h
HAVE_EXECINFO_BACKTRACE_SYMBOLS)
check_cxx_symbol_exists (_chsize_s io.h HAVE__CHSIZE_S)
bqhuyy marked this conversation as resolved.
Show resolved Hide resolved

# NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
# argument. However, it will happily fail if you pass the corresponding -W*
Expand Down
1 change: 1 addition & 0 deletions bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
"-DGLOG_EXPORT=__declspec(dllexport)",
"-DGLOG_NO_ABBREVIATED_SEVERITIES",
"-DHAVE_SNPRINTF",
"-DHAVE__CHSIZE_S",
"-I" + src_windows,
]

Expand Down
3 changes: 3 additions & 0 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
/* define if gmtime_r is available in time.h */
#cmakedefine HAVE_GMTIME_R

/* define if _chsize_s is available in io.h */
#cmakedefine HAVE__CHSIZE_S

/* Define to the sub-directory in which libtool stores uninstalled libraries.
*/
#cmakedefine LT_OBJDIR
Expand Down
13 changes: 12 additions & 1 deletion src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
#endif
#ifdef HAVE__CHSIZE_S
#include <io.h> // for truncate log file
#endif
#include <vector>
#include <cerrno> // for errno
#include <sstream>
Expand Down Expand Up @@ -2425,7 +2428,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(HAVE__CHSIZE_S)
struct stat statbuf;
const int kCopyBlockSize = 8 << 10;
char copybuf[kCopyBlockSize];
Expand All @@ -2446,7 +2449,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 HAVE__CHSIZE_S
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 +2498,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 HAVE__CHSIZE_S
if (_chsize_s(fd, write_offset) != 0) {
#else
if (ftruncate(fd, write_offset) == -1) {
#endif
PLOG(ERROR) << "Unable to truncate " << path;
}

Expand Down