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 @@
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/log/internal/test_helpers.h"
#include "absl/log/log_entry.h"
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
......@@ -131,11 +132,9 @@ Matcher<const absl::LogEntry&> Timestamp(
return Property("timestamp", &absl::LogEntry::timestamp, timestamp);
}
Matcher<const absl::LogEntry&> TimestampInMatchWindow() {
return Property("timestamp", &absl::LogEntry::timestamp,
AllOf(Ge(absl::Now()), Truly([](absl::Time arg) {
return arg <= absl::Now();
})));
Matcher<absl::Time> InMatchWindow() {
return AllOf(Ge(absl::Now()),
Truly([](absl::Time arg) { return arg <= absl::Now(); }));
}
Matcher<const absl::LogEntry&> ThreadID(
......
......@@ -62,7 +62,7 @@ namespace log_internal {
const ::testing::Matcher<absl::Time>& timestamp);
// 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.
::testing::Matcher<const absl::LogEntry&> TimestampInMatchWindow();
::testing::Matcher<absl::Time> InMatchWindow();
::testing::Matcher<const absl::LogEntry&> ThreadID(
const ::testing::Matcher<absl::LogEntry::tid_t>&);
::testing::Matcher<const absl::LogEntry&> TextMessageWithPrefixAndNewline(
......
......@@ -156,9 +156,11 @@ TEST(TailCallsModifiesTest, WithMetadataFrom) {
Timestamp(Eq(absl::UnixEpoch())),
ThreadID(Eq(absl::LogEntry::tid_t{456})),
TextMessage(Eq("forwarded: hello world")), Verbosity(Eq(7)),
ENCODED_MESSAGE(
EqualsProto(R"pb(value { literal: "forwarded: " }
value { str: "hello world" })pb")))));
ENCODED_MESSAGE(MatchesEvent(
Eq("fake/file"), Eq(123), Eq(absl::UnixEpoch()),
Eq(logging::proto::WARNING), Eq(456),
ElementsAre(EqualsProto(R"pb(literal: "forwarded: ")pb"),
EqualsProto(R"pb(str: "hello world")pb")))))));
test_sink.StartCapturingLogs();
LOG(WARNING)
......@@ -179,25 +181,15 @@ TEST(TailCallsModifiesTest, WithPerror) {
Send(AllOf(TextMessage(AnyOf(Eq("hello world: Bad file number [9]"),
Eq("hello world: Bad file descriptor [9]"),
Eq("hello world: Bad file descriptor [8]"))),
ENCODED_MESSAGE(
AnyOf(EqualsProto(R"pb(value { literal: "hello world" }
value { literal: ": " }
value { str: "Bad file number" }
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: "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"))))));
ENCODED_MESSAGE(HasValues(ElementsAre(
EqualsProto(R"pb(literal: "hello world")pb"),
EqualsProto(R"pb(literal: ": ")pb"),
AnyOf(EqualsProto(R"pb(str: "Bad file number")pb"),
EqualsProto(R"pb(str: "Bad file descriptor")pb")),
EqualsProto(R"pb(literal: " [")pb"),
AnyOf(EqualsProto(R"pb(str: "8")pb"),
EqualsProto(R"pb(str: "9")pb")),
EqualsProto(R"pb(literal: "]")pb")))))));
test_sink.StartCapturingLogs();
errno = EBADF;
......
......@@ -30,6 +30,7 @@
namespace {
using ::absl::log_internal::MatchesOstream;
using ::absl::log_internal::TextMessage;
using ::testing::ElementsAre;
using ::testing::Eq;
auto *test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
......@@ -53,8 +54,8 @@ TEST(StreamingFormatTest, LogAsLiteral) {
EXPECT_CALL(sink,
Send(AllOf(TextMessage(MatchesOstream(stream)),
TextMessage(Eq("hello world")),
ENCODED_MESSAGE(EqualsProto(
R"pb(value { literal: "hello world" })pb")))));
ENCODED_MESSAGE(HasValues(ElementsAre(
EqualsProto(R"pb(literal: "hello world")pb")))))));
sink.StartCapturingLogs();
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