Skip to content

Commit

Permalink
fix: unify LogMessage and LogMessageInfo
Browse files Browse the repository at this point in the history
Deprecate the custom prefix callback accepting LogMessageInfo in favor
of existing LogMessage data structure extended with necessary data
fields to avoid redundancies.
  • Loading branch information
sergiud committed Jan 9, 2024
1 parent ec455f2 commit 5a5e59a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 66 deletions.
31 changes: 16 additions & 15 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -656,32 +656,33 @@ Custom Log Prefix Format
glog supports changing the format of the prefix attached to log messages by
receiving a user-provided callback that generates such strings.

For each log entry, the callback will be invoked with a ``LogMessageInfo``
struct containing the severity, filename, line number, thread ID, and time of
the event. It will also be given a reference to the output stream, whose
contents will be prepended to the actual message in the final log line.
For each log entry, the callback will be invoked with a reference to a
``LogMessage`` instance containing the severity, filename, line number, thread
ID, and time of the event. It will also be given a reference to the output
stream, whose contents will be prepended to the actual message in the final log
line.

For example, the following function outputs a prefix that matches glog's default
format. The third parameter ``data`` can be used to access user-supplied data
which unless specified defaults to :cpp:`nullptr`.

.. code:: cpp
void CustomPrefix(std::ostream& s, const LogMessageInfo& l, void* /*data*/) {
s << l.severity[0]
<< setw(4) << 1900 + l.time.year()
<< setw(2) << 1 + l.time.month()
<< setw(2) << l.time.day()
void CustomPrefix(std::ostream& s, const LogMessage& m, void* /*data*/) {
s << GetLogSeverityName(m.severity())[0]
<< setw(4) << 1900 + m.time().year()
<< setw(2) << 1 + m.time().month()
<< setw(2) << m.time().day()
<< ' '
<< setw(2) << l.time.hour() << ':'
<< setw(2) << l.time.min() << ':'
<< setw(2) << l.time.sec() << "."
<< setw(6) << l.time.usec()
<< setw(2) << m.time().hour() << ':'
<< setw(2) << m.time().min() << ':'
<< setw(2) << m.time().sec() << "."
<< setw(6) << m.time().usec()
<< ' '
<< setfill(' ') << setw(5)
<< l.thread_id << setfill('0')
<< m.thread_id() << setfill('0')
<< ' '
<< l.filename << ':' << l.line_number << "]";
<< m.basename() << ':' << m.line() << "]";
}
Expand Down
52 changes: 40 additions & 12 deletions src/glog/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct GLOG_EXPORT LogMessageTime {
std::chrono::seconds gmtoffset_;
};

struct LogMessageInfo {
struct [[deprecated("Use LogMessage instead.")]] LogMessageInfo {
explicit LogMessageInfo(const char* const severity_,
const char* const filename_, const int& line_number_,
std::thread::id thread_id_,
Expand All @@ -133,8 +133,28 @@ struct LogMessageInfo {
const LogMessageTime& time;
};

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

#if defined(__GNUG__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4996)
#endif // __GNUG__
using CustomPrefixCallbackV1
[[deprecated("Use CustomPrefixCallbackV2 instead.")]] =
void (*)(std::ostream&, const LogMessageInfo&, void*);
using CustomPrefixCallback
[[deprecated("Use CustomPrefixCallbackV2 instead.")]] =
CustomPrefixCallbackV1;
#if defined(__GNUG__)
# pragma GCC diagnostic pop
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif // __GNUG__
using CustomPrefixCallbackV2 = void (*)(std::ostream&, const LogMessage&,
void*);

} // namespace google

Expand Down Expand Up @@ -485,8 +505,13 @@ namespace google {
// specified by argv0 in log outputs.
GLOG_EXPORT void InitGoogleLogging(const char* argv0);

[[deprecated(
"Use InitGoogleLogging with the CustomPrefixCallbackV2 overload "
"instead.")]] GLOG_EXPORT void
InitGoogleLogging(const char* argv0, CustomPrefixCallbackV1 prefix_callback,
void* prefix_callback_data = nullptr);
GLOG_EXPORT void InitGoogleLogging(const char* argv0,
CustomPrefixCallback prefix_callback,
CustomPrefixCallbackV2 prefix_callback,
void* prefix_callback_data = nullptr);

// Check if google's logging library has been initialized.
Expand Down Expand Up @@ -1343,9 +1368,15 @@ class GLOG_EXPORT LogMessage {
return time();
}

const LogMessageTime& time() const;
LogSeverity severity() const noexcept;
int line() const noexcept;
const std::thread::id& thread_id() const noexcept;
const char* fullname() const noexcept;
const char* basename() const noexcept;
const LogMessageTime& time() const noexcept;

struct LogMessageData;
LogMessage(const LogMessage&) = delete;
LogMessage& operator=(const LogMessage&) = delete;

private:
// Fully internal SendMethod cases:
Expand Down Expand Up @@ -1373,9 +1404,6 @@ class GLOG_EXPORT LogMessage {
LogMessageTime time_;

friend class LogDestination;

LogMessage(const LogMessage&);
void operator=(const LogMessage&);
};

// This class happens to be thread-hostile because all instances share
Expand Down Expand Up @@ -1493,7 +1521,7 @@ class GLOG_EXPORT 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,
const LogMessageTime& time, const char* message,
size_t message_len);
// Provide an overload for compatibility purposes
GLOG_DEPRECATED
Expand All @@ -1519,8 +1547,8 @@ class GLOG_EXPORT LogSink {
// Returns the normal text output of the log message.
// Can be useful to implement send().
static std::string ToString(LogSeverity severity, const char* file, int line,
const LogMessageTime& logmsgtime,
const char* message, size_t message_len);
const LogMessageTime& time, const char* message,
size_t message_len);
};

// Add or remove a LogSink as a consumer of logging data. Thread-safe.
Expand Down
Loading

0 comments on commit 5a5e59a

Please sign in to comment.