Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
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
3 changes: 3 additions & 0 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 '/'

Expand Down Expand Up @@ -1129,7 +1132,10 @@ void LogFileObject::Write(bool force_flush,
rollover_attempt_ = 0;

struct ::tm tm_time;
localtime_r(&timestamp, &tm_time);
if (FLAGS_log_utc_time)
gmtime_r(&timestamp, &tm_time);
else
localtime_r(&timestamp, &tm_time);

// The logfile's filename will have the date/time & pid in it
ostringstream time_pid_stream;
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -1418,7 +1424,10 @@ void LogMessage::Init(const char* file,
data_->outvec_ = NULL;
WallTime now = WallTime_Now();
data_->timestamp_ = static_cast<time_t>(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<int32>((now - data_->timestamp_) * 1000000);

data_->num_chars_to_log_ = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/windows/glog/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/windows/port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/windows/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down