Skip to content

Commit

Permalink
Merge pull request #573 from msamoila/msamoila-use-utc-time
Browse files Browse the repository at this point in the history
Add FLAGS_log_utc_time; when 'true' the time will be written in log in UTC, resolves #571
  • Loading branch information
sergiud authored Sep 29, 2020
2 parents fa0d50f + 909069e commit f28ae96
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
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 @@ -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 '/'

Expand Down Expand Up @@ -1162,7 +1165,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 @@ -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';

Expand Down Expand Up @@ -1458,7 +1464,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

0 comments on commit f28ae96

Please sign in to comment.