From 909069ea8237b7261e21c47607fc89a16610a8f9 Mon Sep 17 00:00:00 2001 From: M Samoila Date: Tue, 1 Sep 2020 10:55:53 -0700 Subject: [PATCH] Add FLAGS_log_utc_time; when 'true' the time will be written in log in UTC --- src/config.h.cmake.in | 3 +++ src/glog/logging.h.in | 3 +++ src/logging.cc | 15 ++++++++++++--- src/windows/glog/logging.h | 3 +++ src/windows/port.cc | 6 ++++++ src/windows/port.h | 4 ++++ 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/config.h.cmake.in b/src/config.h.cmake.in index 20f099050..4f014a140 100644 --- a/src/config.h.cmake.in +++ b/src/config.h.cmake.in @@ -142,6 +142,9 @@ /* define if localtime_r is available in time.h */ #cmakedefine HAVE_LOCALTIME_R +/* define if gmtime_r is available in time.h */ +#cmakedefine HAVE_GMTIME_R + /* Define to the sub-directory in which libtool stores uninstalled libraries. */ #cmakedefine LT_OBJDIR diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in index 1e552fc1a..8e9c0d9cf 100644 --- a/src/glog/logging.h.in +++ b/src/glog/logging.h.in @@ -380,6 +380,9 @@ DECLARE_int32(max_log_size); // Sets whether to avoid logging to the disk if the disk is full. DECLARE_bool(stop_logging_if_full_disk); +// Use UTC time for logging +DECLARE_bool(log_utc_time); + #ifdef MUST_UNDEF_GFLAGS_DECLARE_MACROS #undef MUST_UNDEF_GFLAGS_DECLARE_MACROS #undef DECLARE_VARIABLE diff --git a/src/logging.cc b/src/logging.cc index ddcf91007..6b9e9e02a 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -187,6 +187,9 @@ GLOG_DEFINE_bool(stop_logging_if_full_disk, false, GLOG_DEFINE_string(log_backtrace_at, "", "Emit a backtrace when logging at file:linenum."); +GLOG_DEFINE_bool(log_utc_time, false, + "Use UTC time for logging."); + // TODO(hamaji): consider windows #define PATH_SEPARATOR '/' @@ -1129,7 +1132,10 @@ void LogFileObject::Write(bool force_flush, rollover_attempt_ = 0; struct ::tm tm_time; - localtime_r(×tamp, &tm_time); + if (FLAGS_log_utc_time) + gmtime_r(×tamp, &tm_time); + else + localtime_r(×tamp, &tm_time); // The logfile's filename will have the date/time & pid in it ostringstream time_pid_stream; @@ -1213,7 +1219,7 @@ void LogFileObject::Write(bool force_flush, << ' ' << setw(2) << tm_time.tm_hour << ':' << setw(2) << tm_time.tm_min << ':' - << setw(2) << tm_time.tm_sec << '\n' + << setw(2) << tm_time.tm_sec << (FLAGS_log_utc_time ? " UTC\n" : "\n") << "Running on machine: " << LogDestination::hostname() << '\n' << "Log line format: [IWEF]yyyymmdd hh:mm:ss.uuuuuu " @@ -1418,7 +1424,10 @@ void LogMessage::Init(const char* file, data_->outvec_ = NULL; WallTime now = WallTime_Now(); data_->timestamp_ = static_cast(now); - localtime_r(&data_->timestamp_, &data_->tm_time_); + if(FLAGS_log_utc_time) + gmtime_r(&data_->timestamp_, &data_->tm_time_); + else + localtime_r(&data_->timestamp_, &data_->tm_time_); data_->usecs_ = static_cast((now - data_->timestamp_) * 1000000); data_->num_chars_to_log_ = 0; diff --git a/src/windows/glog/logging.h b/src/windows/glog/logging.h index e111bf07c..7710152f0 100755 --- a/src/windows/glog/logging.h +++ b/src/windows/glog/logging.h @@ -381,6 +381,9 @@ DECLARE_int32(max_log_size); // Sets whether to avoid logging to the disk if the disk is full. DECLARE_bool(stop_logging_if_full_disk); +// Use UTC time for logging. +DECLARE_bool(log_utc_time); + #ifdef MUST_UNDEF_GFLAGS_DECLARE_MACROS #undef MUST_UNDEF_GFLAGS_DECLARE_MACROS #undef DECLARE_VARIABLE diff --git a/src/windows/port.cc b/src/windows/port.cc index 19bda367c..3a0c930af 100755 --- a/src/windows/port.cc +++ b/src/windows/port.cc @@ -54,6 +54,12 @@ struct tm* localtime_r(const time_t* timep, struct tm* result) { return result; } #endif // not HAVE_LOCALTIME_R +#ifndef HAVE_GMTIME_R +struct tm* gmtime_r(const time_t* timep, struct tm* result) { + gmtime_s(result, timep); + return result; +} +#endif // not HAVE_GMTIME_R #ifndef HAVE_SNPRINTF int snprintf(char *str, size_t size, const char *format, ...) { va_list ap; diff --git a/src/windows/port.h b/src/windows/port.h index 7b4b9c854..75761d41c 100755 --- a/src/windows/port.h +++ b/src/windows/port.h @@ -159,6 +159,10 @@ enum { PTHREAD_ONCE_INIT = 0 }; // important that this be 0! for SpinLock extern GOOGLE_GLOG_DLL_DECL struct tm* localtime_r(const time_t* timep, struct tm* result); #endif // not HAVE_LOCALTIME_R +#ifndef HAVE_GMTIME_R +extern GOOGLE_GLOG_DLL_DECL struct tm* gmtime_r(const time_t* timep, struct tm* result); +#endif // not HAVE_GMTIME_R + inline char* strerror_r(int errnum, char* buf, size_t buflen) { strerror_s(buf, buflen, errnum); return buf;