Warning, /include/absl/log/log_basic_test_impl.inc is written in an unsupported language. File is not indexed.
0001 //
0002 // Copyright 2022 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 // https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015
0016 // The testcases in this file are expected to pass or be skipped with any value
0017 // of ABSL_MIN_LOG_LEVEL
0018
0019 #ifndef ABSL_LOG_LOG_BASIC_TEST_IMPL_H_
0020 #define ABSL_LOG_LOG_BASIC_TEST_IMPL_H_
0021
0022 // Verify that both sets of macros behave identically by parameterizing the
0023 // entire test file.
0024 #ifndef ABSL_TEST_LOG
0025 #error ABSL_TEST_LOG must be defined for these tests to work.
0026 #endif
0027
0028 #ifndef ABSL_TEST_DLOG
0029 #error ABSL_TEST_DLOG must be defined for these tests to work.
0030 #endif
0031
0032 #include <cerrno>
0033 #include <sstream>
0034 #include <string>
0035
0036 #include "gmock/gmock.h"
0037 #include "gtest/gtest.h"
0038 #include "absl/base/internal/sysinfo.h"
0039 #include "absl/base/log_severity.h"
0040 #include "absl/log/globals.h"
0041 #include "absl/log/internal/globals.h"
0042 #include "absl/log/internal/test_actions.h"
0043 #include "absl/log/internal/test_helpers.h"
0044 #include "absl/log/internal/test_matchers.h"
0045 #include "absl/log/log_entry.h"
0046 #include "absl/log/scoped_mock_log.h"
0047
0048 namespace absl_log_internal {
0049 #if GTEST_HAS_DEATH_TEST
0050 using ::absl::log_internal::DeathTestExpectedLogging;
0051 using ::absl::log_internal::DeathTestUnexpectedLogging;
0052 using ::absl::log_internal::DeathTestValidateExpectations;
0053 using ::absl::log_internal::DiedOfFatal;
0054 using ::absl::log_internal::DiedOfQFatal;
0055 #endif
0056 using ::absl::log_internal::InMatchWindow;
0057 using ::absl::log_internal::LoggingEnabledAt;
0058 using ::absl::log_internal::LogSeverity;
0059 using ::absl::log_internal::Prefix;
0060 using ::absl::log_internal::SourceBasename;
0061 using ::absl::log_internal::SourceFilename;
0062 using ::absl::log_internal::SourceLine;
0063 using ::absl::log_internal::Stacktrace;
0064 using ::absl::log_internal::TextMessage;
0065 using ::absl::log_internal::ThreadID;
0066 using ::absl::log_internal::Timestamp;
0067 using ::absl::log_internal::Verbosity;
0068 using ::testing::AnyNumber;
0069 using ::testing::Eq;
0070 using ::testing::IsEmpty;
0071 using ::testing::IsTrue;
0072
0073 class BasicLogTest : public testing::TestWithParam<absl::LogSeverityAtLeast> {};
0074
0075 std::string ThresholdName(
0076 testing::TestParamInfo<absl::LogSeverityAtLeast> severity) {
0077 std::stringstream ostr;
0078 ostr << severity.param;
0079 return ostr.str().substr(
0080 severity.param == absl::LogSeverityAtLeast::kInfinity ? 0 : 2);
0081 }
0082
0083 INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogTest,
0084 testing::Values(absl::LogSeverityAtLeast::kInfo,
0085 absl::LogSeverityAtLeast::kWarning,
0086 absl::LogSeverityAtLeast::kError,
0087 absl::LogSeverityAtLeast::kFatal,
0088 absl::LogSeverityAtLeast::kInfinity),
0089 ThresholdName);
0090
0091 TEST_P(BasicLogTest, Info) {
0092 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0093
0094 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
0095
0096 const int log_line = __LINE__ + 1;
0097 auto do_log = [] { ABSL_TEST_LOG(INFO) << "hello world"; };
0098
0099 if (LoggingEnabledAt(absl::LogSeverity::kInfo)) {
0100 EXPECT_CALL(
0101 test_sink,
0102 Send(AllOf(
0103 SourceFilename(Eq(__FILE__)),
0104 SourceBasename(Eq("log_basic_test_impl.inc")),
0105 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0106 LogSeverity(Eq(absl::LogSeverity::kInfo)),
0107 Timestamp(InMatchWindow()),
0108 ThreadID(Eq(absl::base_internal::GetTID())),
0109 TextMessage(Eq("hello world")),
0110 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0111 ENCODED_MESSAGE(MatchesEvent(
0112 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0113 Eq(logging::proto::INFO), Eq(absl::base_internal::GetTID()),
0114 ElementsAre(EqualsProto(R"pb(literal: "hello world")pb")))),
0115 Stacktrace(IsEmpty()))));
0116 }
0117
0118 test_sink.StartCapturingLogs();
0119 do_log();
0120 }
0121
0122 TEST_P(BasicLogTest, Warning) {
0123 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0124
0125 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
0126
0127 const int log_line = __LINE__ + 1;
0128 auto do_log = [] { ABSL_TEST_LOG(WARNING) << "hello world"; };
0129
0130 if (LoggingEnabledAt(absl::LogSeverity::kWarning)) {
0131 EXPECT_CALL(
0132 test_sink,
0133 Send(AllOf(
0134 SourceFilename(Eq(__FILE__)),
0135 SourceBasename(Eq("log_basic_test_impl.inc")),
0136 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0137 LogSeverity(Eq(absl::LogSeverity::kWarning)),
0138 Timestamp(InMatchWindow()),
0139 ThreadID(Eq(absl::base_internal::GetTID())),
0140 TextMessage(Eq("hello world")),
0141 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0142 ENCODED_MESSAGE(MatchesEvent(
0143 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0144 Eq(logging::proto::WARNING), Eq(absl::base_internal::GetTID()),
0145 ElementsAre(EqualsProto(R"pb(literal: "hello world")pb")))),
0146 Stacktrace(IsEmpty()))));
0147 }
0148
0149 test_sink.StartCapturingLogs();
0150 do_log();
0151 }
0152
0153 TEST_P(BasicLogTest, Error) {
0154 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0155
0156 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
0157
0158 const int log_line = __LINE__ + 1;
0159 auto do_log = [] { ABSL_TEST_LOG(ERROR) << "hello world"; };
0160
0161 if (LoggingEnabledAt(absl::LogSeverity::kError)) {
0162 EXPECT_CALL(
0163 test_sink,
0164 Send(AllOf(
0165 SourceFilename(Eq(__FILE__)),
0166 SourceBasename(Eq("log_basic_test_impl.inc")),
0167 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0168 LogSeverity(Eq(absl::LogSeverity::kError)),
0169 Timestamp(InMatchWindow()),
0170 ThreadID(Eq(absl::base_internal::GetTID())),
0171 TextMessage(Eq("hello world")),
0172 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0173 ENCODED_MESSAGE(MatchesEvent(
0174 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0175 Eq(logging::proto::ERROR), Eq(absl::base_internal::GetTID()),
0176 ElementsAre(EqualsProto(R"pb(literal: "hello world")pb")))),
0177 Stacktrace(IsEmpty()))));
0178 }
0179
0180 test_sink.StartCapturingLogs();
0181 do_log();
0182 }
0183
0184 #if GTEST_HAS_DEATH_TEST
0185 using BasicLogDeathTest = BasicLogTest;
0186
0187 INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogDeathTest,
0188 testing::Values(absl::LogSeverityAtLeast::kInfo,
0189 absl::LogSeverityAtLeast::kFatal,
0190 absl::LogSeverityAtLeast::kInfinity),
0191 ThresholdName);
0192
0193 TEST_P(BasicLogDeathTest, Fatal) {
0194 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0195
0196 const int log_line = __LINE__ + 1;
0197 auto do_log = [] { ABSL_TEST_LOG(FATAL) << "hello world"; };
0198
0199 EXPECT_EXIT(
0200 {
0201 absl::ScopedMockLog test_sink(
0202 absl::MockLogDefault::kDisallowUnexpected);
0203
0204 EXPECT_CALL(test_sink, Send)
0205 .Times(AnyNumber())
0206 .WillRepeatedly(DeathTestUnexpectedLogging());
0207
0208 ::testing::InSequence s;
0209
0210 // Note the logic in DeathTestValidateExpectations() caters for the case
0211 // of logging being disabled at FATAL level.
0212
0213 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) {
0214 // The first call without the stack trace.
0215 EXPECT_CALL(
0216 test_sink,
0217 Send(AllOf(SourceFilename(Eq(__FILE__)),
0218 SourceBasename(Eq("log_basic_test_impl.inc")),
0219 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0220 LogSeverity(Eq(absl::LogSeverity::kFatal)),
0221 Timestamp(InMatchWindow()),
0222 ThreadID(Eq(absl::base_internal::GetTID())),
0223 TextMessage(Eq("hello world")),
0224 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0225 ENCODED_MESSAGE(MatchesEvent(
0226 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0227 Eq(logging::proto::FATAL),
0228 Eq(absl::base_internal::GetTID()),
0229 ElementsAre(EqualsProto(
0230 R"pb(literal: "hello world")pb")))),
0231 Stacktrace(IsEmpty()))))
0232 .WillOnce(DeathTestExpectedLogging());
0233
0234 // The second call with the stack trace.
0235 EXPECT_CALL(
0236 test_sink,
0237 Send(AllOf(SourceFilename(Eq(__FILE__)),
0238 SourceBasename(Eq("log_basic_test_impl.inc")),
0239 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0240 LogSeverity(Eq(absl::LogSeverity::kFatal)),
0241 Timestamp(InMatchWindow()),
0242 ThreadID(Eq(absl::base_internal::GetTID())),
0243 TextMessage(Eq("hello world")),
0244 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0245 ENCODED_MESSAGE(MatchesEvent(
0246 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0247 Eq(logging::proto::FATAL),
0248 Eq(absl::base_internal::GetTID()),
0249 ElementsAre(EqualsProto(
0250 R"pb(literal: "hello world")pb")))),
0251 Stacktrace(Not(IsEmpty())))))
0252 .WillOnce(DeathTestExpectedLogging());
0253 }
0254
0255 test_sink.StartCapturingLogs();
0256 do_log();
0257 },
0258 DiedOfFatal, DeathTestValidateExpectations());
0259 }
0260
0261 TEST_P(BasicLogDeathTest, QFatal) {
0262 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0263
0264 const int log_line = __LINE__ + 1;
0265 auto do_log = [] { ABSL_TEST_LOG(QFATAL) << "hello world"; };
0266
0267 EXPECT_EXIT(
0268 {
0269 absl::ScopedMockLog test_sink(
0270 absl::MockLogDefault::kDisallowUnexpected);
0271
0272 EXPECT_CALL(test_sink, Send)
0273 .Times(AnyNumber())
0274 .WillRepeatedly(DeathTestUnexpectedLogging());
0275
0276 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) {
0277 EXPECT_CALL(
0278 test_sink,
0279 Send(AllOf(SourceFilename(Eq(__FILE__)),
0280 SourceBasename(Eq("log_basic_test_impl.inc")),
0281 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0282 LogSeverity(Eq(absl::LogSeverity::kFatal)),
0283 Timestamp(InMatchWindow()),
0284 ThreadID(Eq(absl::base_internal::GetTID())),
0285 TextMessage(Eq("hello world")),
0286 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0287 ENCODED_MESSAGE(MatchesEvent(
0288 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0289 Eq(logging::proto::FATAL),
0290 Eq(absl::base_internal::GetTID()),
0291 ElementsAre(EqualsProto(
0292 R"pb(literal: "hello world")pb")))),
0293 Stacktrace(IsEmpty()))))
0294 .WillOnce(DeathTestExpectedLogging());
0295 }
0296
0297 test_sink.StartCapturingLogs();
0298 do_log();
0299 },
0300 DiedOfQFatal, DeathTestValidateExpectations());
0301 }
0302 #endif
0303
0304 #ifdef NDEBUG
0305 TEST_P(BasicLogTest, DFatal) {
0306 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0307
0308 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
0309
0310 const int log_line = __LINE__ + 1;
0311 auto do_log = [] { ABSL_TEST_LOG(DFATAL) << "hello world"; };
0312
0313 if (LoggingEnabledAt(absl::LogSeverity::kError)) {
0314 EXPECT_CALL(
0315 test_sink,
0316 Send(AllOf(
0317 SourceFilename(Eq(__FILE__)),
0318 SourceBasename(Eq("log_basic_test_impl.inc")),
0319 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0320 LogSeverity(Eq(absl::LogSeverity::kError)),
0321 Timestamp(InMatchWindow()),
0322 ThreadID(Eq(absl::base_internal::GetTID())),
0323 TextMessage(Eq("hello world")),
0324 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0325 ENCODED_MESSAGE(MatchesEvent(
0326 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0327 Eq(logging::proto::ERROR), Eq(absl::base_internal::GetTID()),
0328 ElementsAre(EqualsProto(R"pb(literal: "hello world")pb")))),
0329 Stacktrace(IsEmpty()))));
0330 }
0331
0332 test_sink.StartCapturingLogs();
0333 do_log();
0334 }
0335
0336 #elif GTEST_HAS_DEATH_TEST
0337 TEST_P(BasicLogDeathTest, DFatal) {
0338 // TODO(b/242568884): re-enable once bug is fixed.
0339 // absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0340
0341 const int log_line = __LINE__ + 1;
0342 auto do_log = [] { ABSL_TEST_LOG(DFATAL) << "hello world"; };
0343
0344 EXPECT_EXIT(
0345 {
0346 absl::ScopedMockLog test_sink(
0347 absl::MockLogDefault::kDisallowUnexpected);
0348
0349 EXPECT_CALL(test_sink, Send)
0350 .Times(AnyNumber())
0351 .WillRepeatedly(DeathTestUnexpectedLogging());
0352
0353 ::testing::InSequence s;
0354
0355 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) {
0356 // The first call without the stack trace.
0357 EXPECT_CALL(
0358 test_sink,
0359 Send(AllOf(SourceFilename(Eq(__FILE__)),
0360 SourceBasename(Eq("log_basic_test_impl.inc")),
0361 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0362 LogSeverity(Eq(absl::LogSeverity::kFatal)),
0363 Timestamp(InMatchWindow()),
0364 ThreadID(Eq(absl::base_internal::GetTID())),
0365 TextMessage(Eq("hello world")),
0366 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0367 ENCODED_MESSAGE(MatchesEvent(
0368 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0369 Eq(logging::proto::FATAL),
0370 Eq(absl::base_internal::GetTID()),
0371 ElementsAre(EqualsProto(
0372 R"pb(literal: "hello world")pb")))),
0373 Stacktrace(IsEmpty()))))
0374 .WillOnce(DeathTestExpectedLogging());
0375
0376 // The second call with the stack trace.
0377 EXPECT_CALL(
0378 test_sink,
0379 Send(AllOf(SourceFilename(Eq(__FILE__)),
0380 SourceBasename(Eq("log_basic_test_impl.inc")),
0381 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0382 LogSeverity(Eq(absl::LogSeverity::kFatal)),
0383 Timestamp(InMatchWindow()),
0384 ThreadID(Eq(absl::base_internal::GetTID())),
0385 TextMessage(Eq("hello world")),
0386 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0387 ENCODED_MESSAGE(MatchesEvent(
0388 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0389 Eq(logging::proto::FATAL),
0390 Eq(absl::base_internal::GetTID()),
0391 ElementsAre(EqualsProto(
0392 R"pb(literal: "hello world")pb")))),
0393 Stacktrace(Not(IsEmpty())))))
0394 .WillOnce(DeathTestExpectedLogging());
0395 }
0396
0397 test_sink.StartCapturingLogs();
0398 do_log();
0399 },
0400 DiedOfFatal, DeathTestValidateExpectations());
0401 }
0402 #endif
0403
0404 #ifndef NDEBUG
0405 TEST_P(BasicLogTest, DFatalIsCancellable) {
0406 // LOG(DFATAL) does not die when DFATAL death is disabled.
0407 absl::log_internal::SetExitOnDFatal(false);
0408 ABSL_TEST_LOG(DFATAL) << "hello world";
0409 absl::log_internal::SetExitOnDFatal(true);
0410 }
0411
0412 #if GTEST_HAS_DEATH_TEST
0413 TEST_P(BasicLogDeathTest, DLogFatalIsNotCancellable) {
0414 EXPECT_EXIT(
0415 {
0416 absl::log_internal::SetExitOnDFatal(false);
0417 ABSL_TEST_DLOG(FATAL) << "hello world";
0418 absl::log_internal::SetExitOnDFatal(true);
0419 },
0420 DiedOfFatal, "");
0421 }
0422 #endif
0423 #endif
0424
0425 TEST_P(BasicLogTest, Level) {
0426 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0427
0428 for (auto severity : {absl::LogSeverity::kInfo, absl::LogSeverity::kWarning,
0429 absl::LogSeverity::kError}) {
0430 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
0431
0432 const int log_line = __LINE__ + 2;
0433 auto do_log = [severity] {
0434 ABSL_TEST_LOG(LEVEL(severity)) << "hello world";
0435 };
0436
0437 if (LoggingEnabledAt(severity)) {
0438 EXPECT_CALL(
0439 test_sink,
0440 Send(AllOf(
0441 SourceFilename(Eq(__FILE__)),
0442 SourceBasename(Eq("log_basic_test_impl.inc")),
0443 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0444 LogSeverity(Eq(severity)), Timestamp(InMatchWindow()),
0445 ThreadID(Eq(absl::base_internal::GetTID())),
0446 TextMessage(Eq("hello world")),
0447 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0448 ENCODED_MESSAGE(MatchesEvent(
0449 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0450 Eq(severity == absl::LogSeverity::kInfo ? logging::proto::INFO
0451 : severity == absl::LogSeverity::kWarning
0452 ? logging::proto::WARNING
0453 : severity == absl::LogSeverity::kError
0454 ? logging::proto::ERROR
0455 : 0),
0456 Eq(absl::base_internal::GetTID()),
0457 ElementsAre(EqualsProto(R"pb(literal: "hello world")pb")))),
0458 Stacktrace(IsEmpty()))));
0459 }
0460 test_sink.StartCapturingLogs();
0461 do_log();
0462 }
0463 }
0464
0465 #if GTEST_HAS_DEATH_TEST
0466 TEST_P(BasicLogDeathTest, Level) {
0467 // TODO(b/242568884): re-enable once bug is fixed.
0468 // absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0469
0470 // Ensure that `severity` is not a compile-time constant to prove that
0471 // `LOG(LEVEL(severity))` works regardless:
0472 auto volatile severity = absl::LogSeverity::kFatal;
0473
0474 const int log_line = __LINE__ + 1;
0475 auto do_log = [severity] { ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; };
0476
0477 EXPECT_EXIT(
0478 {
0479 absl::ScopedMockLog test_sink(
0480 absl::MockLogDefault::kDisallowUnexpected);
0481
0482 EXPECT_CALL(test_sink, Send)
0483 .Times(AnyNumber())
0484 .WillRepeatedly(DeathTestUnexpectedLogging());
0485
0486 ::testing::InSequence s;
0487
0488 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) {
0489 EXPECT_CALL(
0490 test_sink,
0491 Send(AllOf(SourceFilename(Eq(__FILE__)),
0492 SourceBasename(Eq("log_basic_test_impl.inc")),
0493 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0494 LogSeverity(Eq(absl::LogSeverity::kFatal)),
0495 Timestamp(InMatchWindow()),
0496 ThreadID(Eq(absl::base_internal::GetTID())),
0497 TextMessage(Eq("hello world")),
0498 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0499 ENCODED_MESSAGE(MatchesEvent(
0500 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0501 Eq(logging::proto::FATAL),
0502 Eq(absl::base_internal::GetTID()),
0503 ElementsAre(EqualsProto(
0504 R"pb(literal: "hello world")pb")))),
0505 Stacktrace(IsEmpty()))))
0506 .WillOnce(DeathTestExpectedLogging());
0507
0508 EXPECT_CALL(
0509 test_sink,
0510 Send(AllOf(SourceFilename(Eq(__FILE__)),
0511 SourceBasename(Eq("log_basic_test_impl.inc")),
0512 SourceLine(Eq(log_line)), Prefix(IsTrue()),
0513 LogSeverity(Eq(absl::LogSeverity::kFatal)),
0514 Timestamp(InMatchWindow()),
0515 ThreadID(Eq(absl::base_internal::GetTID())),
0516 TextMessage(Eq("hello world")),
0517 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)),
0518 ENCODED_MESSAGE(MatchesEvent(
0519 Eq(__FILE__), Eq(log_line), InMatchWindow(),
0520 Eq(logging::proto::FATAL),
0521 Eq(absl::base_internal::GetTID()),
0522 ElementsAre(EqualsProto(
0523 R"pb(literal: "hello world")pb")))),
0524 Stacktrace(Not(IsEmpty())))))
0525 .WillOnce(DeathTestExpectedLogging());
0526 }
0527
0528 test_sink.StartCapturingLogs();
0529 do_log();
0530 },
0531 DiedOfFatal, DeathTestValidateExpectations());
0532 }
0533 #endif
0534
0535 TEST_P(BasicLogTest, LevelClampsNegativeValues) {
0536 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0537
0538 if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) {
0539 GTEST_SKIP() << "This test cases required INFO log to be enabled";
0540 return;
0541 }
0542
0543 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
0544
0545 EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kInfo))));
0546
0547 test_sink.StartCapturingLogs();
0548 ABSL_TEST_LOG(LEVEL(-1)) << "hello world";
0549 }
0550
0551 TEST_P(BasicLogTest, LevelClampsLargeValues) {
0552 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
0553
0554 if (!LoggingEnabledAt(absl::LogSeverity::kError)) {
0555 GTEST_SKIP() << "This test cases required ERROR log to be enabled";
0556 return;
0557 }
0558
0559 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
0560
0561 EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kError))));
0562
0563 test_sink.StartCapturingLogs();
0564 ABSL_TEST_LOG(LEVEL(static_cast<int>(absl::LogSeverity::kFatal) + 1))
0565 << "hello world";
0566 }
0567
0568 TEST(ErrnoPreservationTest, InSeverityExpression) {
0569 errno = 77;
0570 int saved_errno;
0571 ABSL_TEST_LOG(LEVEL((saved_errno = errno, absl::LogSeverity::kInfo)));
0572 EXPECT_THAT(saved_errno, Eq(77));
0573 }
0574
0575 TEST(ErrnoPreservationTest, InStreamedExpression) {
0576 if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) {
0577 GTEST_SKIP() << "This test cases required INFO log to be enabled";
0578 return;
0579 }
0580
0581 errno = 77;
0582 int saved_errno = 0;
0583 ABSL_TEST_LOG(INFO) << (saved_errno = errno, "hello world");
0584 EXPECT_THAT(saved_errno, Eq(77));
0585 }
0586
0587 TEST(ErrnoPreservationTest, AfterStatement) {
0588 errno = 77;
0589 ABSL_TEST_LOG(INFO);
0590 const int saved_errno = errno;
0591 EXPECT_THAT(saved_errno, Eq(77));
0592 }
0593
0594 // Tests that using a variable/parameter in a logging statement suppresses
0595 // unused-variable/parameter warnings.
0596 // -----------------------------------------------------------------------
0597 class UnusedVariableWarningCompileTest {
0598 // These four don't prove anything unless `ABSL_MIN_LOG_LEVEL` is greater than
0599 // `kInfo`.
0600 static void LoggedVariable() {
0601 const int x = 0;
0602 ABSL_TEST_LOG(INFO) << x;
0603 }
0604 static void LoggedParameter(const int x) { ABSL_TEST_LOG(INFO) << x; }
0605 static void SeverityVariable() {
0606 const int x = 0;
0607 ABSL_TEST_LOG(LEVEL(x)) << "hello world";
0608 }
0609 static void SeverityParameter(const int x) {
0610 ABSL_TEST_LOG(LEVEL(x)) << "hello world";
0611 }
0612 };
0613
0614 } // namespace absl_log_internal
0615
0616 #endif // ABSL_LOG_LOG_BASIC_TEST_IMPL_H_