feat: support log file truncation on windows (#960)
This commit is contained in:
parent
615966eb6d
commit
4dbc26af72
@ -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 execinfo.h HAVE_EXECINFO_BACKTRACE)
|
||||||
check_cxx_symbol_exists (backtrace_symbols execinfo.h
|
check_cxx_symbol_exists (backtrace_symbols execinfo.h
|
||||||
HAVE_EXECINFO_BACKTRACE_SYMBOLS)
|
HAVE_EXECINFO_BACKTRACE_SYMBOLS)
|
||||||
|
check_cxx_symbol_exists (_chsize_s io.h HAVE__CHSIZE_S)
|
||||||
|
|
||||||
# NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
|
# 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*
|
# argument. However, it will happily fail if you pass the corresponding -W*
|
||||||
|
|||||||
@ -114,6 +114,7 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
|
|||||||
"-DGLOG_EXPORT=__declspec(dllexport)",
|
"-DGLOG_EXPORT=__declspec(dllexport)",
|
||||||
"-DGLOG_NO_ABBREVIATED_SEVERITIES",
|
"-DGLOG_NO_ABBREVIATED_SEVERITIES",
|
||||||
"-DHAVE_SNPRINTF",
|
"-DHAVE_SNPRINTF",
|
||||||
|
"-DHAVE__CHSIZE_S",
|
||||||
"-I" + src_windows,
|
"-I" + src_windows,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@ -130,6 +130,9 @@
|
|||||||
/* define if gmtime_r is available in time.h */
|
/* define if gmtime_r is available in time.h */
|
||||||
#cmakedefine HAVE_GMTIME_R
|
#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.
|
/* Define to the sub-directory in which libtool stores uninstalled libraries.
|
||||||
*/
|
*/
|
||||||
#cmakedefine LT_OBJDIR
|
#cmakedefine LT_OBJDIR
|
||||||
|
|||||||
@ -58,6 +58,9 @@
|
|||||||
#ifdef HAVE_SYSLOG_H
|
#ifdef HAVE_SYSLOG_H
|
||||||
# include <syslog.h>
|
# include <syslog.h>
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef HAVE__CHSIZE_S
|
||||||
|
#include <io.h> // for truncate log file
|
||||||
|
#endif
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cerrno> // for errno
|
#include <cerrno> // for errno
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -2425,7 +2428,7 @@ void GetExistingTempDirectories(vector<string>* list) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TruncateLogFile(const char *path, uint64 limit, uint64 keep) {
|
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;
|
struct stat statbuf;
|
||||||
const int kCopyBlockSize = 8 << 10;
|
const int kCopyBlockSize = 8 << 10;
|
||||||
char copybuf[kCopyBlockSize];
|
char copybuf[kCopyBlockSize];
|
||||||
@ -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
|
// all of base/...) with -D_FILE_OFFSET_BITS=64 but that's
|
||||||
// rather scary.
|
// rather scary.
|
||||||
// Instead just truncate the file to something we can manage
|
// 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) {
|
if (truncate(path, 0) == -1) {
|
||||||
|
#endif
|
||||||
PLOG(ERROR) << "Unable to truncate " << path;
|
PLOG(ERROR) << "Unable to truncate " << path;
|
||||||
} else {
|
} else {
|
||||||
LOG(ERROR) << "Truncated " << path << " due to EFBIG error";
|
LOG(ERROR) << "Truncated " << path << " due to EFBIG error";
|
||||||
@ -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
|
// 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
|
// end of the file after our last read() above, we lose their latest
|
||||||
// data. Too bad ...
|
// data. Too bad ...
|
||||||
|
#ifdef HAVE__CHSIZE_S
|
||||||
|
if (_chsize_s(fd, write_offset) != 0) {
|
||||||
|
#else
|
||||||
if (ftruncate(fd, write_offset) == -1) {
|
if (ftruncate(fd, write_offset) == -1) {
|
||||||
|
#endif
|
||||||
PLOG(ERROR) << "Unable to truncate " << path;
|
PLOG(ERROR) << "Unable to truncate " << path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user