From 92fdbfb301f8b301b28ab5c99e7361e775c2fb8a Mon Sep 17 00:00:00 2001 From: Gennadiy Rozental Date: Thu, 25 Aug 2022 14:15:03 -0700 Subject: Release the Abseil Logging library PiperOrigin-RevId: 470080638 Change-Id: I8d9ddfabc7704c383ed5a73abf0411f4c58a4bf7 --- absl/log/log_modifier_methods_test.cc | 231 ++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 absl/log/log_modifier_methods_test.cc (limited to 'absl/log/log_modifier_methods_test.cc') diff --git a/absl/log/log_modifier_methods_test.cc b/absl/log/log_modifier_methods_test.cc new file mode 100644 index 00000000..a9bf38b7 --- /dev/null +++ b/absl/log/log_modifier_methods_test.cc @@ -0,0 +1,231 @@ +// +// Copyright 2022 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/log/internal/test_actions.h" +#include "absl/log/internal/test_helpers.h" +#include "absl/log/internal/test_matchers.h" +#include "absl/log/log.h" +#include "absl/log/log_sink.h" +#include "absl/log/scoped_mock_log.h" +#include "absl/strings/match.h" +#include "absl/strings/string_view.h" +#include "absl/time/time.h" + +namespace { +#if GTEST_HAS_DEATH_TEST +using ::absl::log_internal::DeathTestExpectedLogging; +using ::absl::log_internal::DeathTestUnexpectedLogging; +using ::absl::log_internal::DeathTestValidateExpectations; +using ::absl::log_internal::DiedOfQFatal; +#endif +using ::absl::log_internal::LogSeverity; +using ::absl::log_internal::Prefix; +using ::absl::log_internal::SourceBasename; +using ::absl::log_internal::SourceFilename; +using ::absl::log_internal::SourceLine; +using ::absl::log_internal::Stacktrace; +using ::absl::log_internal::TextMessage; +using ::absl::log_internal::TextMessageWithPrefix; +using ::absl::log_internal::TextMessageWithPrefixAndNewline; +using ::absl::log_internal::TextPrefix; +using ::absl::log_internal::ThreadID; +using ::absl::log_internal::Timestamp; +using ::absl::log_internal::Verbosity; + +using ::testing::AllOf; +using ::testing::AnyNumber; +using ::testing::AnyOf; +using ::testing::Eq; +using ::testing::IsEmpty; +using ::testing::IsFalse; +using ::testing::Truly; + +TEST(TailCallsModifiesTest, AtLocationFileLine) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL( + test_sink, + Send(AllOf( + // The metadata should change: + SourceFilename(Eq("/my/very/very/very_long_source_file.cc")), + SourceBasename(Eq("very_long_source_file.cc")), SourceLine(Eq(777)), + // The logged line should change too, even though the prefix must + // grow to fit the new metadata. + TextMessageWithPrefix(Truly([](absl::string_view msg) { + return absl::EndsWith(msg, + " very_long_source_file.cc:777] hello world"); + }))))); + + test_sink.StartCapturingLogs(); + LOG(INFO).AtLocation("/my/very/very/very_long_source_file.cc", 777) + << "hello world"; +} + +TEST(TailCallsModifiesTest, NoPrefix) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send(AllOf(Prefix(IsFalse()), TextPrefix(IsEmpty()), + TextMessageWithPrefix(Eq("hello world"))))); + + test_sink.StartCapturingLogs(); + LOG(INFO).NoPrefix() << "hello world"; +} + +TEST(TailCallsModifiesTest, NoPrefixNoMessageNoShirtNoShoesNoService) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, + Send(AllOf(Prefix(IsFalse()), TextPrefix(IsEmpty()), + TextMessageWithPrefix(IsEmpty()), + TextMessageWithPrefixAndNewline(Eq("\n"))))); + test_sink.StartCapturingLogs(); + LOG(INFO).NoPrefix(); +} + +TEST(TailCallsModifiesTest, WithVerbosity) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send(Verbosity(Eq(2)))); + + test_sink.StartCapturingLogs(); + LOG(INFO).WithVerbosity(2) << "hello world"; +} + +TEST(TailCallsModifiesTest, WithVerbosityNoVerbosity) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, + Send(Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)))); + + test_sink.StartCapturingLogs(); + LOG(INFO).WithVerbosity(2).WithVerbosity(absl::LogEntry::kNoVerbosityLevel) + << "hello world"; +} + +TEST(TailCallsModifiesTest, WithTimestamp) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send(Timestamp(Eq(absl::UnixEpoch())))); + + test_sink.StartCapturingLogs(); + LOG(INFO).WithTimestamp(absl::UnixEpoch()) << "hello world"; +} + +TEST(TailCallsModifiesTest, WithThreadID) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send(AllOf(ThreadID(Eq(1234))))); + + test_sink.StartCapturingLogs(); + LOG(INFO).WithThreadID(1234) << "hello world"; +} + +TEST(TailCallsModifiesTest, WithMetadataFrom) { + class ForwardingLogSink : public absl::LogSink { + public: + void Send(const absl::LogEntry &entry) override { + LOG(LEVEL(entry.log_severity())).WithMetadataFrom(entry) + << "forwarded: " << entry.text_message(); + } + } forwarding_sink; + + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq("fake/file")), SourceBasename(Eq("file")), + SourceLine(Eq(123)), Prefix(IsFalse()), + LogSeverity(Eq(absl::LogSeverity::kWarning)), + Timestamp(Eq(absl::UnixEpoch())), ThreadID(Eq(456)), + TextMessage(Eq("forwarded: hello world")), Verbosity(Eq(7)), + ENCODED_MESSAGE( + EqualsProto(R"pb(value { literal: "forwarded: " } + value { str: "hello world" })pb"))))); + + test_sink.StartCapturingLogs(); + LOG(WARNING) + .AtLocation("fake/file", 123) + .NoPrefix() + .WithTimestamp(absl::UnixEpoch()) + .WithThreadID(456) + .WithVerbosity(7) + .ToSinkOnly(&forwarding_sink) + << "hello world"; +} + +TEST(TailCallsModifiesTest, WithPerror) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL( + test_sink, + 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")))))); + + test_sink.StartCapturingLogs(); + errno = EBADF; + LOG(INFO).WithPerror() << "hello world"; +} + +#if GTEST_HAS_DEATH_TEST +TEST(ModifierMethodDeathTest, ToSinkOnlyQFatal) { + EXPECT_EXIT( + { + absl::ScopedMockLog test_sink( + absl::MockLogDefault::kDisallowUnexpected); + + auto do_log = [&test_sink] { + LOG(QFATAL).ToSinkOnly(&test_sink.UseAsLocalSink()) << "hello world"; + }; + + EXPECT_CALL(test_sink, Send) + .Times(AnyNumber()) + .WillRepeatedly(DeathTestUnexpectedLogging()); + + EXPECT_CALL(test_sink, Send(AllOf(TextMessage(Eq("hello world")), + Stacktrace(IsEmpty())))) + .WillOnce(DeathTestExpectedLogging()); + + test_sink.StartCapturingLogs(); + do_log(); + }, + DiedOfQFatal, DeathTestValidateExpectations()); +} +#endif + +} // namespace -- cgit v1.2.3 From 94433ef0615616b1ccb8b2193bde777c10406ac0 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 17 Oct 2022 15:40:12 -0700 Subject: Fix various warnings for _WIN32. Bug: chromium:1292951 PiperOrigin-RevId: 481757795 Change-Id: I03c808222c6c4d3d7052576ab4b36141e5f1ebbc --- absl/log/internal/log_format.cc | 33 ++++++++++++++++++++++++++------- absl/log/internal/test_helpers.cc | 2 +- absl/log/log_entry_test.cc | 10 ++++++++-- absl/log/log_modifier_methods_test.cc | 6 ++++-- absl/log/scoped_mock_log_test.cc | 2 +- 5 files changed, 40 insertions(+), 13 deletions(-) (limited to 'absl/log/log_modifier_methods_test.cc') diff --git a/absl/log/internal/log_format.cc b/absl/log/internal/log_format.cc index 5d40d253..5b280a2d 100644 --- a/absl/log/internal/log_format.cc +++ b/absl/log/internal/log_format.cc @@ -46,6 +46,31 @@ ABSL_NAMESPACE_BEGIN namespace log_internal { namespace { +// This templated function avoids compiler warnings about tautological +// comparisons when log_internal::Tid is unsigned. It can be replaced with a +// constexpr if once the minimum C++ version Abseil suppports is C++17. +template +inline std::enable_if_t::value> +PutLeadingWhitespace(T tid, char*& p) { + if (tid < 10) *p++ = ' '; + if (tid < 100) *p++ = ' '; + if (tid < 1000) *p++ = ' '; + if (tid < 10000) *p++ = ' '; + if (tid < 100000) *p++ = ' '; + if (tid < 1000000) *p++ = ' '; +} + +template +inline std::enable_if_t::value> +PutLeadingWhitespace(T tid, char*& p) { + if (tid >= 0 && tid < 10) *p++ = ' '; + if (tid > -10 && tid < 100) *p++ = ' '; + if (tid > -100 && tid < 1000) *p++ = ' '; + if (tid > -1000 && tid < 10000) *p++ = ' '; + if (tid > -10000 && tid < 100000) *p++ = ' '; + if (tid > -100000 && tid < 1000000) *p++ = ' '; +} + // The fields before the filename are all fixed-width except for the thread ID, // which is of bounded width. size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp, @@ -110,13 +135,7 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp, absl::numbers_internal::PutTwoDigits(static_cast(usecs % 100), p); p += 2; *p++ = ' '; - constexpr bool unsigned_tid_t = !std::is_signed::value; - if ((unsigned_tid_t || tid >= 0) && tid < 10) *p++ = ' '; - if ((unsigned_tid_t || tid > -10) && tid < 100) *p++ = ' '; - if ((unsigned_tid_t || tid > -100) && tid < 1000) *p++ = ' '; - if ((unsigned_tid_t || tid > -1000) && tid < 10000) *p++ = ' '; - if ((unsigned_tid_t || tid > -10000) && tid < 100000) *p++ = ' '; - if ((unsigned_tid_t || tid > -100000) && tid < 1000000) *p++ = ' '; + PutLeadingWhitespace(tid, p); p = absl::numbers_internal::FastIntToBuffer(tid, p); *p++ = ' '; const size_t bytes_formatted = static_cast(p - buf.data()); diff --git a/absl/log/internal/test_helpers.cc b/absl/log/internal/test_helpers.cc index bff5cc17..0de5b96b 100644 --- a/absl/log/internal/test_helpers.cc +++ b/absl/log/internal/test_helpers.cc @@ -46,7 +46,7 @@ bool DiedOfFatal(int exit_status) { // Depending on NDEBUG and (configuration?) MSVC's abort either results // in error code 3 (SIGABRT) or error code 0x80000003 (breakpoint // triggered). - return ::testing::ExitedWithCode(3)(exit_status & ~0x80000000); + return ::testing::ExitedWithCode(3)(exit_status & 0x7fffffff); #elif defined(__Fuchsia__) // The Fuchsia death test implementation kill()'s the process when it detects // an exception, so it should exit with the corresponding code. See diff --git a/absl/log/log_entry_test.cc b/absl/log/log_entry_test.cc index 8d0afb3c..7238356e 100644 --- a/absl/log/log_entry_test.cc +++ b/absl/log/log_entry_test.cc @@ -208,10 +208,13 @@ TEST(LogEntryTest, EmptyFields) { } TEST(LogEntryTest, NegativeFields) { + // When Abseil's minimum C++ version is C++17, this conditional can be + // converted to a constexpr if and the static_cast below removed. if (std::is_signed::value) { LogEntryTestPeer entry("foo.cc", -1234, kUsePrefix, absl::LogSeverity::kInfo, "2020-01-02T03:04:05.6789", - -451, "hello world"); + static_cast(-451), + "hello world"); EXPECT_THAT(entry.FormatLogMessage(), Eq("I0102 03:04:05.678900 -451 foo.cc:-1234] hello world")); EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000), @@ -313,12 +316,15 @@ TEST(LogEntryTest, LongFields) { } TEST(LogEntryTest, LongNegativeFields) { + // When Abseil's minimum C++ version is C++17, this conditional can be + // converted to a constexpr if and the static_cast below removed. if (std::is_signed::value) { LogEntryTestPeer entry( "I am the very model of a modern Major-General / " "I've information vegetable, animal, and mineral.", -2147483647, kUsePrefix, absl::LogSeverity::kInfo, - "2020-01-02T03:04:05.678967896789", -2147483647, + "2020-01-02T03:04:05.678967896789", + static_cast(-2147483647), "I know the kings of England, and I quote the fights historical / " "From Marathon to Waterloo, in order categorical."); EXPECT_THAT( diff --git a/absl/log/log_modifier_methods_test.cc b/absl/log/log_modifier_methods_test.cc index a9bf38b7..42e13b1b 100644 --- a/absl/log/log_modifier_methods_test.cc +++ b/absl/log/log_modifier_methods_test.cc @@ -130,7 +130,8 @@ TEST(TailCallsModifiesTest, WithTimestamp) { TEST(TailCallsModifiesTest, WithThreadID) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - EXPECT_CALL(test_sink, Send(AllOf(ThreadID(Eq(1234))))); + EXPECT_CALL(test_sink, + Send(AllOf(ThreadID(Eq(absl::LogEntry::tid_t{1234}))))); test_sink.StartCapturingLogs(); LOG(INFO).WithThreadID(1234) << "hello world"; @@ -152,7 +153,8 @@ TEST(TailCallsModifiesTest, WithMetadataFrom) { Send(AllOf(SourceFilename(Eq("fake/file")), SourceBasename(Eq("file")), SourceLine(Eq(123)), Prefix(IsFalse()), LogSeverity(Eq(absl::LogSeverity::kWarning)), - Timestamp(Eq(absl::UnixEpoch())), ThreadID(Eq(456)), + 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: " } diff --git a/absl/log/scoped_mock_log_test.cc b/absl/log/scoped_mock_log_test.cc index 50689a0e..44b8d737 100644 --- a/absl/log/scoped_mock_log_test.cc +++ b/absl/log/scoped_mock_log_test.cc @@ -98,7 +98,7 @@ TEST(ScopedMockLogTest, LogMockCatchAndMatchSendExpectations) { log, Send(AllOf(SourceFilename(Eq("/my/very/very/very_long_source_file.cc")), SourceBasename(Eq("very_long_source_file.cc")), - SourceLine(Eq(777)), ThreadID(Eq(1234)), + SourceLine(Eq(777)), ThreadID(Eq(absl::LogEntry::tid_t{1234})), TextMessageWithPrefix(Truly([](absl::string_view msg) { return absl::EndsWith( msg, " very_long_source_file.cc:777] Info message"); -- cgit v1.2.3