diff --git a/src/config.h.cmake.in b/src/config.h.cmake.in index 20f0990..4f014a1 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 19b5462..da5dd19 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 2873795..25a30f9 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -191,6 +191,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 '/' @@ -1162,7 +1165,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; @@ -1246,7 +1252,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'; @@ -1458,7 +1464,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 f42d936..0b4241b 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 19bda36..3a0c930 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 7b4b9c8..75761d4 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;