Skip to content

Commit

Permalink
provide backward compatible send overload
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Dec 15, 2021
1 parent c575116 commit dfb9ffc
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 58 deletions.
5 changes: 4 additions & 1 deletion bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,12 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
# generated for Bazel.
"@bazel_tools//src/conditions:windows": [
"GOOGLE_GLOG_DLL_DECL=__declspec(dllexport)",
"GLOG_DEPRECATED=__declspec(deprecated)",
"GLOG_NO_ABBREVIATED_SEVERITIES",
],
"//conditions:default": [],
"//conditions:default": [
"GLOG_DEPRECATED=__attribute__((deprecated))",
],
}),
copts =
select({
Expand Down
47 changes: 25 additions & 22 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,17 @@ typedef unsigned __int64 uint64;
#error Do not know how to define a 32-bit integer quantity on your system
#endif

@ac_google_end_namespace@
typedef double WallTime;

struct GOOGLE_GLOG_DLL_DECL LogMessageTime {
LogMessageTime(): time_struct_(), timestamp_(0), usecs_(0), gmtoffset_(0) {}

void setTimeInfo(struct::tm time_struct, time_t timestamp, int32_t usecs){
time_struct_ = time_struct;
timestamp_ = timestamp;
usecs_ = usecs;

CalcGmtOffset();
}
LogMessageTime();
LogMessageTime(std::tm t);
LogMessageTime(std::time_t timestamp, WallTime now);

const time_t& timestamp() const { return timestamp_; }
const int& sec() const { return time_struct_.tm_sec; }
const int32_t& usec() const { return usecs_; }
const int& (min)() const { return time_struct_.tm_min; }
const int&(min)() const { return time_struct_.tm_min; }
const int& hour() const { return time_struct_.tm_hour; }
const int& day() const { return time_struct_.tm_mday; }
const int& month() const { return time_struct_.tm_mon; }
Expand All @@ -158,14 +152,16 @@ struct GOOGLE_GLOG_DLL_DECL LogMessageTime {
const int& dayInYear() const { return time_struct_.tm_yday; }
const int& dst() const { return time_struct_.tm_isdst; }
const long int& gmtoff() const { return gmtoffset_; }
const std::tm& tm() const { return time_struct_; }

private:
struct::tm time_struct_; // Time of creation of LogMessage
time_t timestamp_; // Time of creation of LogMessage in seconds
int32_t usecs_; // Time of creation of LogMessage - microseconds part
long int gmtoffset_;
private:
void init(const std::tm& t, std::time_t timestamp, WallTime now);
std::tm time_struct_; // Time of creation of LogMessage
time_t timestamp_; // Time of creation of LogMessage in seconds
int32_t usecs_; // Time of creation of LogMessage - microseconds part
long int gmtoffset_;

void CalcGmtOffset();
void CalcGmtOffset();
};

#ifdef GLOG_CUSTOM_PREFIX_SUPPORT
Expand All @@ -187,8 +183,12 @@ struct LogMessageInfo {
};

typedef void(*CustomPrefixCallback)(std::ostream& s, const LogMessageInfo& l, void* data);

#endif

@ac_google_end_namespace@


// The global value of GOOGLE_STRIP_LOG. All the messages logged to
// LOG(XXX) with severity less than GOOGLE_STRIP_LOG will not be displayed.
// If it can be determined at compile time that the message will not be
Expand Down Expand Up @@ -1562,9 +1562,7 @@ public:
// Must be called without the log_mutex held. (L < log_mutex)
static int64 num_messages(int severity);

const LogMessageTime& getLogMessageTime() const {
return logmsgtime_;
}
const LogMessageTime& getLogMessageTime() const;

struct LogMessageData;

Expand Down Expand Up @@ -1736,8 +1734,13 @@ class GOOGLE_GLOG_DLL_DECL LogSink {
// during this call.
virtual void send(LogSeverity severity, const char* full_filename,
const char* base_filename, int line,
const LogMessageTime &logmsgtime,
const char* message, size_t message_len) = 0;
const LogMessageTime& logmsgtime, const char* message,
size_t message_len);
// Provide an overload for compatibility purposes
GLOG_DEPRECATED
virtual void send(LogSeverity severity, const char* full_filename,
const char* base_filename, int line, const std::tm* t,
const char* message, size_t message_len);

// Redefine this to implement waiting for
// the sink's logging logic to complete.
Expand Down
114 changes: 81 additions & 33 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -487,17 +487,15 @@ class LogCleaner {
bool enabled() const { return enabled_; }

private:
vector<string> GetOverdueLogNames(string log_directory,
unsigned int days,
vector<string> GetOverdueLogNames(string log_directory, unsigned int days,
const string& base_filename,
const string& filename_extension) const;

bool IsLogFromCurrentProject(const string& filepath,
const string& base_filename,
const string& filename_extension) const;

bool IsLogLastModifiedOver(const string& filepath,
unsigned int days) const;
bool IsLogLastModifiedOver(const string& filepath, unsigned int days) const;

bool enabled_;
unsigned int overdue_days_;
Expand Down Expand Up @@ -567,12 +565,9 @@ class LogDestination {
const char* message, size_t len);

// Send logging info to all registered sinks.
static void LogToSinks(LogSeverity severity,
const char *full_filename,
const char *base_filename,
int line,
const LogMessageTime &logmsgtime,
const char* message,
static void LogToSinks(LogSeverity severity, const char* full_filename,
const char* base_filename, int line,
const LogMessageTime& logmsgtime, const char* message,
size_t message_len);

// Wait for all registered sinks via WaitTillSent
Expand Down Expand Up @@ -850,10 +845,9 @@ inline void LogDestination::LogToAllLogfiles(LogSeverity severity,
}

inline void LogDestination::LogToSinks(LogSeverity severity,
const char *full_filename,
const char *base_filename,
int line,
const LogMessageTime &logmsgtime,
const char* full_filename,
const char* base_filename, int line,
const LogMessageTime& logmsgtime,
const char* message,
size_t message_len) {
ReaderMutexLock l(&sink_mutex_);
Expand Down Expand Up @@ -1134,10 +1128,11 @@ void LogFileObject::Write(bool force_flush,
rollover_attempt_ = 0;

struct ::tm tm_time;
if (FLAGS_log_utc_time)
gmtime_r(&timestamp, &tm_time);
else
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 @@ -1343,10 +1338,9 @@ void LogCleaner::Run(bool base_filename_selected,
}
}

vector<string> LogCleaner::GetOverdueLogNames(string log_directory,
unsigned int days,
const string& base_filename,
const string& filename_extension) const {
vector<string> LogCleaner::GetOverdueLogNames(
string log_directory, unsigned int days, const string& base_filename,
const string& filename_extension) const {
// The names of overdue logs.
vector<string> overdue_log_names;

Expand Down Expand Up @@ -1605,14 +1599,7 @@ void LogMessage::Init(const char* file,
data_->outvec_ = NULL;
WallTime now = WallTime_Now();
time_t timestamp_now = static_cast<time_t>(now);
std::tm time_struct;
if(FLAGS_log_utc_time)
gmtime_r(&timestamp_now, &time_struct);
else
localtime_r(&timestamp_now, &time_struct);

logmsgtime_.setTimeInfo(time_struct, timestamp_now,
static_cast<int32>((now - timestamp_now) * 1000000));
logmsgtime_ = LogMessageTime(timestamp_now, now);

data_->num_chars_to_log_ = 0;
data_->num_chars_to_syslog_ = 0;
Expand Down Expand Up @@ -1674,6 +1661,10 @@ void LogMessage::Init(const char* file,
}
}

const LogMessageTime& LogMessage::getLogMessageTime() const {
return logmsgtime_;
}

LogMessage::~LogMessage() {
Flush();
#ifdef GLOG_THREAD_LOCAL_STORAGE
Expand Down Expand Up @@ -2023,13 +2014,45 @@ void SetLogSymlink(LogSeverity severity, const char* symlink_basename) {
LogSink::~LogSink() {
}

void LogSink::send(LogSeverity severity, const char* full_filename,
const char* base_filename, int line,
const LogMessageTime& time, const char* message,
size_t message_len) {
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996)
#endif // __GNUC__
send(severity, full_filename, base_filename, line, &time.tm(), message,
message_len);
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif // __GNUC__
}

void LogSink::send(LogSeverity severity, const char* full_filename,
const char* base_filename, int line, const std::tm* t,
const char* message, size_t message_len) {
(void)severity;
(void)full_filename;
(void)base_filename;
(void)line;
(void)t;
(void)message;
(void)message_len;
}

void LogSink::WaitTillSent() {
// noop default
}

string LogSink::ToString(LogSeverity severity, const char* file, int line,
const LogMessageTime &logmsgtime,
const char* message, size_t message_len) {
const LogMessageTime& logmsgtime, const char* message,
size_t message_len) {
ostringstream stream(string(message, message_len));
stream.fill('0');

Expand Down Expand Up @@ -2559,7 +2582,31 @@ void DisableLogCleaner() {
log_cleaner.Disable();
}

_END_GOOGLE_NAMESPACE_
LogMessageTime::LogMessageTime()
: time_struct_(), timestamp_(0), usecs_(0), gmtoffset_(0) {}

LogMessageTime::LogMessageTime(std::tm t) {
std::time_t timestamp = std::mktime(&t);
init(t, timestamp, 0);
}

LogMessageTime::LogMessageTime(std::time_t timestamp, WallTime now) {
std::tm t;
if (FLAGS_log_utc_time)
gmtime_r(&timestamp, &t);
else
localtime_r(&timestamp, &t);
init(t, timestamp, now);
}

void LogMessageTime::init(const std::tm& t, std::time_t timestamp,
WallTime now) {
time_struct_ = t;
timestamp_ = timestamp;
usecs_ = static_cast<int32>((now - timestamp) * 1000000);

CalcGmtOffset();
}

void LogMessageTime::CalcGmtOffset() {
std::tm gmt_struct;
Expand All @@ -2579,3 +2626,4 @@ void LogMessageTime::CalcGmtOffset() {
gmtoffset_ = static_cast<long int>(timestamp_ - gmt_sec + (isDst ? hour_secs : 0) ) ;
}

_END_GOOGLE_NAMESPACE_
2 changes: 0 additions & 2 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ const char* ProgramInvocationShortName();
int64 CycleClock_Now();

int64 UsecToCycles(int64 usec);

typedef double WallTime;
WallTime WallTime_Now();

int32 GetMainThreadPid();
Expand Down

0 comments on commit dfb9ffc

Please sign in to comment.