Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unify LogMessage and LogMessageInfo #1057

Merged
merged 1 commit into from
Jan 10, 2024
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
4 changes: 2 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
tasks:
ubuntu1804:
name: "Ubuntu 18.04"
platform: ubuntu1804
name: "Ubuntu 22.04"
platform: ubuntu2204
build_flags:
- "--features=layering_check"
- "--copt=-Werror"
Expand Down
92 changes: 48 additions & 44 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,54 @@ Example:
caution when comparing the low bits of timestamps from different machines.


Customizing the Log Line Prefix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The predefined log line prefix can be replaced using a user-provided callback
that formats the corresponding output.

For each log entry, the callback will be invoked with a reference to a
``google::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 MyPrefixFormatter(std::ostream& s, const google::LogMessage& m, void* /*data*/) {
s << google::GetLogSeverityName(m.severity())[0]
<< setw(4) << 1900 + m.time().year()
<< setw(2) << 1 + m.time().month()
<< setw(2) << m.time().day()
<< ' '
<< setw(2) << m.time().hour() << ':'
<< setw(2) << m.time().min() << ':'
<< setw(2) << m.time().sec() << "."
<< setw(6) << m.time().usec()
<< ' '
<< setfill(' ') << setw(5)
<< m.thread_id() << setfill('0')
<< ' '
<< m.basename() << ':' << m.line() << "]";
}


To enable the use of a prefix formatter, use the

.. code:: cpp

google::InstallPrefixFormatter(&MyPrefixFormatter);

function to pass a pointer to the corresponding :cpp:`MyPrefixFormatter`
callback during initialization. :cpp:`InstallPrefixFormatter` takes a second
optional argument of type :cpp:`void*` that allows supplying user data to the
callback.


Setting Flags
~~~~~~~~~~~~~

Expand Down Expand Up @@ -650,50 +698,6 @@ severity level.
"Present occurrence is " << google::COUNTER;


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 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()
<< ' '
<< setw(2) << l.time.hour() << ':'
<< setw(2) << l.time.min() << ':'
<< setw(2) << l.time.sec() << "."
<< setw(6) << l.time.usec()
<< ' '
<< setfill(' ') << setw(5)
<< l.thread_id << setfill('0')
<< ' '
<< l.filename << ':' << l.line_number << "]";
}


To enable the use of a custom prefix, use the

.. code:: cpp

InitGoogleLogging(argv[0], &CustomPrefix);

overload to pass a pointer to the corresponding :cpp:`CustomPrefix` function during
initialization. :cpp:`InitGoogleLogging()` takes a third optional argument of
type :cpp:`void*` that allows supplying user data to the callback.

Failure Signal Handler
~~~~~~~~~~~~~~~~~~~~~~
Expand Down
53 changes: 38 additions & 15 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,9 +133,6 @@ struct LogMessageInfo {
const LogMessageTime& time;
};

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

} // namespace google

// The global value of GOOGLE_STRIP_LOG. All the messages logged to
Expand Down Expand Up @@ -485,9 +482,26 @@ namespace google {
// specified by argv0 in log outputs.
GLOG_EXPORT void InitGoogleLogging(const char* argv0);

GLOG_EXPORT void InitGoogleLogging(const char* argv0,
CustomPrefixCallback prefix_callback,
void* prefix_callback_data = nullptr);
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 CustomPrefixCallback
[[deprecated("Use PrefixFormatterCallback instead.")]] =
void (*)(std::ostream&, const LogMessageInfo&, void*);
#if defined(__GNUG__)
# pragma GCC diagnostic pop
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif // __GNUG__
[[deprecated("Use InstallPrefixFormatter instead.")]] GLOG_EXPORT void
InitGoogleLogging(const char* argv0, CustomPrefixCallback prefix_callback,
void* prefix_callback_data = nullptr);

// Check if google's logging library has been initialized.
GLOG_EXPORT bool IsGoogleLoggingInitialized();
Expand All @@ -501,6 +515,12 @@ typedef void (*logging_fail_func_t)() __attribute__((noreturn));
typedef void (*logging_fail_func_t)();
#endif

using PrefixFormatterCallback = void (*)(std::ostream&, const LogMessage&,
void*);

GLOG_EXPORT void InstallPrefixFormatter(PrefixFormatterCallback callback,
void* data = nullptr);

// Install a function which will be called after LOG(FATAL).
GLOG_EXPORT void InstallFailureFunction(logging_fail_func_t fail_func);

Expand Down Expand Up @@ -1343,9 +1363,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 +1399,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 +1516,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 +1542,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
Loading