summaryrefslogtreecommitdiff
path: root/absl/log
diff options
context:
space:
mode:
authorGravatar Phoebe Liang <phoebeliang@google.com>2022-11-02 15:27:54 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-11-02 15:28:46 -0700
commite6044634dd7caec2d79a13aecc9e765023768757 (patch)
tree3ffe2c00f4b5e49da4f4e7d9e50c64999f318408 /absl/log
parent1649c037c556bdaca7241bc0113275506bdb9638 (diff)
Support logging of user-defined types that implement `AbslStringify()`
If a user-defined type has `AbslStringify()` defined, it will always be used for logging over `operator<<`. `HasAbslStringify` now uses the empty class `UnimplementedSink` for its checks instead of `StringifySink` in order to make it work in cases involving other sinks. PiperOrigin-RevId: 485710377 Change-Id: Ibdd916151c7abc3269c35fbe79b772867f3d25e1
Diffstat (limited to 'absl/log')
-rw-r--r--absl/log/BUILD.bazel1
-rw-r--r--absl/log/CMakeLists.txt1
-rw-r--r--absl/log/internal/log_message.h48
-rw-r--r--absl/log/log.h37
-rw-r--r--absl/log/log_format_test.cc131
5 files changed, 214 insertions, 4 deletions
diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel
index dadc8856..16788ae2 100644
--- a/absl/log/BUILD.bazel
+++ b/absl/log/BUILD.bazel
@@ -324,6 +324,7 @@ cc_test(
"//absl/log/internal:config",
"//absl/log/internal:test_matchers",
"//absl/strings",
+ "//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)
diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt
index 09e4ca0c..28d4b519 100644
--- a/absl/log/CMakeLists.txt
+++ b/absl/log/CMakeLists.txt
@@ -681,6 +681,7 @@ absl_cc_test(
absl::log_internal_config
absl::log_internal_test_matchers
absl::scoped_mock_log
+ absl::str_format
absl::strings
GTest::gmock
GTest::gtest_main
diff --git a/absl/log/internal/log_message.h b/absl/log/internal/log_message.h
index 37a267c0..992bb630 100644
--- a/absl/log/internal/log_message.h
+++ b/absl/log/internal/log_message.h
@@ -41,6 +41,7 @@
#include "absl/log/internal/nullguard.h"
#include "absl/log/log_entry.h"
#include "absl/log/log_sink.h"
+#include "absl/strings/internal/has_absl_stringify.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
@@ -153,8 +154,17 @@ class LogMessage {
template <int SIZE>
LogMessage& operator<<(char (&buf)[SIZE]) ABSL_ATTRIBUTE_NOINLINE;
- // Default: uses `ostream` logging to convert `v` to a string.
- template <typename T>
+ // Types that support `AbslStringify()` are serialized that way.
+ template <typename T,
+ typename std::enable_if<
+ strings_internal::HasAbslStringify<T>::value, int>::type = 0>
+ LogMessage& operator<<(const T& v) ABSL_ATTRIBUTE_NOINLINE;
+
+ // Types that don't support `AbslStringify()` but do support streaming into a
+ // `std::ostream&` are serialized that way.
+ template <typename T,
+ typename std::enable_if<
+ !strings_internal::HasAbslStringify<T>::value, int>::type = 0>
LogMessage& operator<<(const T& v) ABSL_ATTRIBUTE_NOINLINE;
// Note: We explicitly do not support `operator<<` for non-const references
@@ -205,12 +215,44 @@ class LogMessage {
std::ostream stream_;
};
+// Helper class so that `AbslStringify()` can modify the LogMessage.
+class StringifySink final {
+ public:
+ explicit StringifySink(LogMessage& message) : message_(message) {}
+
+ void Append(size_t count, char ch) { message_ << std::string(count, ch); }
+
+ void Append(absl::string_view v) { message_ << v; }
+
+ // For types that implement `AbslStringify` using `absl::Format()`.
+ friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
+ sink->Append(v);
+ }
+
+ private:
+ LogMessage& message_;
+};
+
+// Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE`
+template <typename T,
+ typename std::enable_if<strings_internal::HasAbslStringify<T>::value,
+ int>::type>
+LogMessage& LogMessage::operator<<(const T& v) {
+ StringifySink sink(*this);
+ // Replace with public API.
+ AbslStringify(sink, v);
+ return *this;
+}
+
// Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE`
-template <typename T>
+template <typename T,
+ typename std::enable_if<!strings_internal::HasAbslStringify<T>::value,
+ int>::type>
LogMessage& LogMessage::operator<<(const T& v) {
stream_ << log_internal::NullGuard<T>().Guard(v);
return *this;
}
+
inline LogMessage& LogMessage::operator<<(
std::ostream& (*m)(std::ostream& os)) {
stream_ << m;
diff --git a/absl/log/log.h b/absl/log/log.h
index 5331fdbf..4cd52041 100644
--- a/absl/log/log.h
+++ b/absl/log/log.h
@@ -132,10 +132,45 @@
// as they would be if streamed into a `std::ostream`, however it should be
// noted that their actual type is unspecified.
//
-// To implement a custom formatting operator for a type you own, define
+// To implement a custom formatting operator for a type you own, there are two
+// options: `AbslStringify()` or `std::ostream& operator<<(std::ostream&, ...)`.
+// It is recommended that users make their types loggable through
+// `AbslStringify()` as it is a universal stringification extension that also
+// enables `absl::StrFormat` and `absl::StrCat` support. If both
+// `AbslStringify()` and `std::ostream& operator<<(std::ostream&, ...)` are
+// defined, `AbslStringify()` will be used.
+//
+// To use the `AbslStringify()` API, define a friend function template in your
+// type's namespace with the following signature:
+//
+// template <typename Sink>
+// void AbslStringify(Sink& sink, const UserDefinedType& value);
+//
+// `Sink` has the same interface as `absl::FormatSink`, but without
+// `PutPaddedString()`.
+//
+// Example:
+//
+// struct Point {
+// template <typename Sink>
+// friend void AbslStringify(Sink& sink, const Point& p) {
+// absl::Format(&sink, "(%v, %v)", p.x, p.y);
+// }
+//
+// int x;
+// int y;
+// };
+//
+// To use `std::ostream& operator<<(std::ostream&, ...)`, define
// `std::ostream& operator<<(std::ostream&, ...)` in your type's namespace (for
// ADL) just as you would to stream it to `std::cout`.
//
+// Currently `AbslStringify()` ignores output manipulators but this is not
+// guaranteed behavior and may be subject to change in the future. If you would
+// like guaranteed behavior regarding output manipulators, please use
+// `std::ostream& operator<<(std::ostream&, ...)` to make custom types loggable
+// instead.
+//
// Those macros that support streaming honor output manipulators and `fmtflag`
// changes that output data (e.g. `std::ends`) or control formatting of data
// (e.g. `std::hex` and `std::fixed`), however flushing such a stream is
diff --git a/absl/log/log_format_test.cc b/absl/log/log_format_test.cc
index c629fce7..397c8d0c 100644
--- a/absl/log/log_format_test.cc
+++ b/absl/log/log_format_test.cc
@@ -32,6 +32,7 @@
#include "absl/log/scoped_mock_log.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
+#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
namespace {
@@ -865,6 +866,111 @@ TEST(LogFormatTest, CustomNonCopyable) {
LOG(INFO) << value;
}
+struct Point {
+ template <typename Sink>
+ friend void AbslStringify(Sink& sink, const Point& p) {
+ absl::Format(&sink, "(%d, %d)", p.x, p.y);
+ }
+
+ int x = 10;
+ int y = 20;
+};
+
+TEST(LogFormatTest, AbslStringifyExample) {
+ absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
+
+ Point p;
+
+ EXPECT_CALL(
+ test_sink,
+ Send(AllOf(
+ TextMessage(Eq("(10, 20)")), TextMessage(Eq(absl::StrCat(p))),
+ ENCODED_MESSAGE(EqualsProto(R"pb(value { str: "(10, 20)" })pb")))));
+
+ test_sink.StartCapturingLogs();
+ LOG(INFO) << p;
+}
+
+struct PointWithAbslStringifiyAndOstream {
+ template <typename Sink>
+ friend void AbslStringify(Sink& sink,
+ const PointWithAbslStringifiyAndOstream& p) {
+ absl::Format(&sink, "(%d, %d)", p.x, p.y);
+ }
+
+ int x = 10;
+ int y = 20;
+};
+
+ABSL_ATTRIBUTE_UNUSED std::ostream& operator<<(
+ std::ostream& os, const PointWithAbslStringifiyAndOstream&) {
+ return os << "Default to AbslStringify()";
+}
+
+TEST(LogFormatTest, CustomWithAbslStringifyAndOstream) {
+ absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
+
+ PointWithAbslStringifiyAndOstream p;
+
+ EXPECT_CALL(
+ test_sink,
+ Send(AllOf(
+ TextMessage(Eq("(10, 20)")), TextMessage(Eq(absl::StrCat(p))),
+ ENCODED_MESSAGE(EqualsProto(R"pb(value { str: "(10, 20)" })pb")))));
+
+ test_sink.StartCapturingLogs();
+ LOG(INFO) << p;
+}
+
+struct PointStreamsNothing {
+ template <typename Sink>
+ friend void AbslStringify(Sink&, const PointStreamsNothing&) {}
+
+ int x = 10;
+ int y = 20;
+};
+
+TEST(LogFormatTest, AbslStringifyStreamsNothing) {
+ absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
+
+ PointStreamsNothing p;
+
+ EXPECT_CALL(
+ test_sink,
+ Send(AllOf(TextMessage(Eq("77")), TextMessage(Eq(absl::StrCat(p, 77))),
+ ENCODED_MESSAGE(EqualsProto(R"pb(value { str: "77" })pb")))));
+
+ test_sink.StartCapturingLogs();
+ LOG(INFO) << p << 77;
+}
+
+struct PointMultipleAppend {
+ template <typename Sink>
+ friend void AbslStringify(Sink& sink, const PointMultipleAppend& p) {
+ sink.Append("(");
+ sink.Append(absl::StrCat(p.x, ", ", p.y, ")"));
+ }
+
+ int x = 10;
+ int y = 20;
+};
+
+TEST(LogFormatTest, AbslStringifyMultipleAppend) {
+ absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
+
+ PointMultipleAppend p;
+
+ EXPECT_CALL(
+ test_sink,
+ Send(AllOf(
+ TextMessage(Eq("(10, 20)")), TextMessage(Eq(absl::StrCat(p))),
+ ENCODED_MESSAGE(EqualsProto(R"pb(value { str: "(" }
+ value { str: "10, 20)" })pb")))));
+
+ test_sink.StartCapturingLogs();
+ LOG(INFO) << p;
+}
+
TEST(ManipulatorLogFormatTest, BoolAlphaTrue) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
@@ -1501,6 +1607,31 @@ TEST(ManipulatorLogFormatTest, CustomClassStreamsNothing) {
LOG(INFO) << value << 77;
}
+struct PointPercentV {
+ template <typename Sink>
+ friend void AbslStringify(Sink& sink, const PointPercentV& p) {
+ absl::Format(&sink, "(%v, %v)", p.x, p.y);
+ }
+
+ int x = 10;
+ int y = 20;
+};
+
+TEST(ManipulatorLogFormatTest, IOManipsDoNotAffectAbslStringify) {
+ absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
+
+ PointPercentV p;
+
+ EXPECT_CALL(
+ test_sink,
+ Send(AllOf(
+ TextMessage(Eq("(10, 20)")), TextMessage(Eq(absl::StrCat(p))),
+ ENCODED_MESSAGE(EqualsProto(R"pb(value { str: "(10, 20)" })pb")))));
+
+ test_sink.StartCapturingLogs();
+ LOG(INFO) << std::hex << p;
+}
+
// Tests that verify the behavior when more data are streamed into a `LOG`
// statement than fit in the buffer.
// Structured logging scenario is tested in other unit tests since the output is