Commit 6e701508 by Andy Getzendanner Committed by Copybara-Service

Encode some additional metadata when writing protobuf-encoded logs.

PiperOrigin-RevId: 649138261
Change-Id: I8687eb7b06cc9e96779d1d3e98b44b4f643c95a8
parent 0d9c2fc7
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/log/internal/test_helpers.h" #include "absl/log/internal/test_helpers.h"
#include "absl/log/log_entry.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/time/clock.h" #include "absl/time/clock.h"
#include "absl/time/time.h" #include "absl/time/time.h"
...@@ -131,11 +132,9 @@ Matcher<const absl::LogEntry&> Timestamp( ...@@ -131,11 +132,9 @@ Matcher<const absl::LogEntry&> Timestamp(
return Property("timestamp", &absl::LogEntry::timestamp, timestamp); return Property("timestamp", &absl::LogEntry::timestamp, timestamp);
} }
Matcher<const absl::LogEntry&> TimestampInMatchWindow() { Matcher<absl::Time> InMatchWindow() {
return Property("timestamp", &absl::LogEntry::timestamp, return AllOf(Ge(absl::Now()),
AllOf(Ge(absl::Now()), Truly([](absl::Time arg) { Truly([](absl::Time arg) { return arg <= absl::Now(); }));
return arg <= absl::Now();
})));
} }
Matcher<const absl::LogEntry&> ThreadID( Matcher<const absl::LogEntry&> ThreadID(
......
...@@ -62,7 +62,7 @@ namespace log_internal { ...@@ -62,7 +62,7 @@ namespace log_internal {
const ::testing::Matcher<absl::Time>& timestamp); const ::testing::Matcher<absl::Time>& timestamp);
// Matches if the `LogEntry`'s timestamp falls after the instantiation of this // Matches if the `LogEntry`'s timestamp falls after the instantiation of this
// matcher and before its execution, as is normal when used with EXPECT_CALL. // matcher and before its execution, as is normal when used with EXPECT_CALL.
::testing::Matcher<const absl::LogEntry&> TimestampInMatchWindow(); ::testing::Matcher<absl::Time> InMatchWindow();
::testing::Matcher<const absl::LogEntry&> ThreadID( ::testing::Matcher<const absl::LogEntry&> ThreadID(
const ::testing::Matcher<absl::LogEntry::tid_t>&); const ::testing::Matcher<absl::LogEntry::tid_t>&);
::testing::Matcher<const absl::LogEntry&> TextMessageWithPrefixAndNewline( ::testing::Matcher<const absl::LogEntry&> TextMessageWithPrefixAndNewline(
......
...@@ -156,9 +156,11 @@ TEST(TailCallsModifiesTest, WithMetadataFrom) { ...@@ -156,9 +156,11 @@ TEST(TailCallsModifiesTest, WithMetadataFrom) {
Timestamp(Eq(absl::UnixEpoch())), Timestamp(Eq(absl::UnixEpoch())),
ThreadID(Eq(absl::LogEntry::tid_t{456})), ThreadID(Eq(absl::LogEntry::tid_t{456})),
TextMessage(Eq("forwarded: hello world")), Verbosity(Eq(7)), TextMessage(Eq("forwarded: hello world")), Verbosity(Eq(7)),
ENCODED_MESSAGE( ENCODED_MESSAGE(MatchesEvent(
EqualsProto(R"pb(value { literal: "forwarded: " } Eq("fake/file"), Eq(123), Eq(absl::UnixEpoch()),
value { str: "hello world" })pb"))))); Eq(logging::proto::WARNING), Eq(456),
ElementsAre(EqualsProto(R"pb(literal: "forwarded: ")pb"),
EqualsProto(R"pb(str: "hello world")pb")))))));
test_sink.StartCapturingLogs(); test_sink.StartCapturingLogs();
LOG(WARNING) LOG(WARNING)
...@@ -179,25 +181,15 @@ TEST(TailCallsModifiesTest, WithPerror) { ...@@ -179,25 +181,15 @@ TEST(TailCallsModifiesTest, WithPerror) {
Send(AllOf(TextMessage(AnyOf(Eq("hello world: Bad file number [9]"), Send(AllOf(TextMessage(AnyOf(Eq("hello world: Bad file number [9]"),
Eq("hello world: Bad file descriptor [9]"), Eq("hello world: Bad file descriptor [9]"),
Eq("hello world: Bad file descriptor [8]"))), Eq("hello world: Bad file descriptor [8]"))),
ENCODED_MESSAGE( ENCODED_MESSAGE(HasValues(ElementsAre(
AnyOf(EqualsProto(R"pb(value { literal: "hello world" } EqualsProto(R"pb(literal: "hello world")pb"),
value { literal: ": " } EqualsProto(R"pb(literal: ": ")pb"),
value { str: "Bad file number" } AnyOf(EqualsProto(R"pb(str: "Bad file number")pb"),
value { literal: " [" } EqualsProto(R"pb(str: "Bad file descriptor")pb")),
value { str: "9" } EqualsProto(R"pb(literal: " [")pb"),
value { literal: "]" })pb"), AnyOf(EqualsProto(R"pb(str: "8")pb"),
EqualsProto(R"pb(value { literal: "hello world" } EqualsProto(R"pb(str: "9")pb")),
value { literal: ": " } EqualsProto(R"pb(literal: "]")pb")))))));
value { str: "Bad file descriptor" }
value { literal: " [" }
value { str: "9" }
value { literal: "]" })pb"),
EqualsProto(R"pb(value { literal: "hello world" }
value { literal: ": " }
value { str: "Bad file descriptor" }
value { literal: " [" }
value { str: "8" }
value { literal: "]" })pb"))))));
test_sink.StartCapturingLogs(); test_sink.StartCapturingLogs();
errno = EBADF; errno = EBADF;
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
namespace { namespace {
using ::absl::log_internal::MatchesOstream; using ::absl::log_internal::MatchesOstream;
using ::absl::log_internal::TextMessage; using ::absl::log_internal::TextMessage;
using ::testing::ElementsAre;
using ::testing::Eq; using ::testing::Eq;
auto *test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( auto *test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
...@@ -53,8 +54,8 @@ TEST(StreamingFormatTest, LogAsLiteral) { ...@@ -53,8 +54,8 @@ TEST(StreamingFormatTest, LogAsLiteral) {
EXPECT_CALL(sink, EXPECT_CALL(sink,
Send(AllOf(TextMessage(MatchesOstream(stream)), Send(AllOf(TextMessage(MatchesOstream(stream)),
TextMessage(Eq("hello world")), TextMessage(Eq("hello world")),
ENCODED_MESSAGE(EqualsProto( ENCODED_MESSAGE(HasValues(ElementsAre(
R"pb(value { literal: "hello world" })pb"))))); EqualsProto(R"pb(literal: "hello world")pb")))))));
sink.StartCapturingLogs(); sink.StartCapturingLogs();
LOG(INFO) << absl::LogAsLiteral(not_a_literal); LOG(INFO) << absl::LogAsLiteral(not_a_literal);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment