log_basic_test_impl.inc (24588B)
1 // 2 // Copyright 2022 The Abseil Authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // https://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 // SKIP_ABSL_INLINE_NAMESPACE_CHECK 17 18 // The testcases in this file are expected to pass or be skipped with any value 19 // of ABSL_MIN_LOG_LEVEL 20 21 #ifndef ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ 22 #define ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ 23 24 // Verify that both sets of macros behave identically by parameterizing the 25 // entire test file. 26 #ifndef ABSL_TEST_LOG 27 #error ABSL_TEST_LOG must be defined for these tests to work. 28 #endif 29 30 #ifndef ABSL_TEST_DLOG 31 #error ABSL_TEST_DLOG must be defined for these tests to work. 32 #endif 33 34 #include <cerrno> 35 #include <sstream> 36 #include <string> 37 38 #include "gmock/gmock.h" 39 #include "gtest/gtest.h" 40 #include "absl/base/internal/sysinfo.h" 41 #include "absl/base/log_severity.h" 42 #include "absl/log/globals.h" 43 #include "absl/log/internal/globals.h" 44 #include "absl/log/internal/test_actions.h" 45 #include "absl/log/internal/test_helpers.h" 46 #include "absl/log/internal/test_matchers.h" 47 #include "absl/log/log_entry.h" 48 #include "absl/log/scoped_mock_log.h" 49 50 namespace absl_log_internal { 51 #if GTEST_HAS_DEATH_TEST 52 using ::absl::log_internal::DeathTestExpectedLogging; 53 using ::absl::log_internal::DeathTestUnexpectedLogging; 54 using ::absl::log_internal::DeathTestValidateExpectations; 55 using ::absl::log_internal::DiedOfFatal; 56 using ::absl::log_internal::DiedOfQFatal; 57 #endif 58 using ::absl::log_internal::InMatchWindow; 59 using ::absl::log_internal::LoggingEnabledAt; 60 using ::absl::log_internal::LogSeverity; 61 using ::absl::log_internal::Prefix; 62 using ::absl::log_internal::SourceBasename; 63 using ::absl::log_internal::SourceFilename; 64 using ::absl::log_internal::SourceLine; 65 using ::absl::log_internal::Stacktrace; 66 using ::absl::log_internal::TextMessage; 67 using ::absl::log_internal::ThreadID; 68 using ::absl::log_internal::Timestamp; 69 using ::absl::log_internal::Verbosity; 70 using ::testing::AnyNumber; 71 using ::testing::Eq; 72 using ::testing::IsEmpty; 73 using ::testing::IsTrue; 74 75 class BasicLogTest : public testing::TestWithParam<absl::LogSeverityAtLeast> {}; 76 77 std::string ThresholdName( 78 testing::TestParamInfo<absl::LogSeverityAtLeast> severity) { 79 std::stringstream ostr; 80 ostr << severity.param; 81 return ostr.str().substr( 82 severity.param == absl::LogSeverityAtLeast::kInfinity ? 0 : 2); 83 } 84 85 INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogTest, 86 testing::Values(absl::LogSeverityAtLeast::kInfo, 87 absl::LogSeverityAtLeast::kWarning, 88 absl::LogSeverityAtLeast::kError, 89 absl::LogSeverityAtLeast::kFatal, 90 absl::LogSeverityAtLeast::kInfinity), 91 ThresholdName); 92 93 TEST_P(BasicLogTest, Info) { 94 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 95 96 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 97 98 const int log_line = __LINE__ + 1; 99 auto do_log = [] { ABSL_TEST_LOG(INFO) << "hello world"; }; 100 101 if (LoggingEnabledAt(absl::LogSeverity::kInfo)) { 102 EXPECT_CALL( 103 test_sink, 104 Send(AllOf( 105 SourceFilename(Eq(__FILE__)), 106 SourceBasename(Eq("log_basic_test_impl.inc")), 107 SourceLine(Eq(log_line)), Prefix(IsTrue()), 108 LogSeverity(Eq(absl::LogSeverity::kInfo)), 109 Timestamp(InMatchWindow()), 110 ThreadID(Eq(absl::base_internal::GetTID())), 111 TextMessage(Eq("hello world")), 112 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 113 ENCODED_MESSAGE(MatchesEvent( 114 Eq(__FILE__), Eq(log_line), InMatchWindow(), 115 Eq(logging::proto::INFO), Eq(absl::base_internal::GetTID()), 116 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 117 Stacktrace(IsEmpty())))); 118 } 119 120 test_sink.StartCapturingLogs(); 121 do_log(); 122 } 123 124 TEST_P(BasicLogTest, Warning) { 125 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 126 127 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 128 129 const int log_line = __LINE__ + 1; 130 auto do_log = [] { ABSL_TEST_LOG(WARNING) << "hello world"; }; 131 132 if (LoggingEnabledAt(absl::LogSeverity::kWarning)) { 133 EXPECT_CALL( 134 test_sink, 135 Send(AllOf( 136 SourceFilename(Eq(__FILE__)), 137 SourceBasename(Eq("log_basic_test_impl.inc")), 138 SourceLine(Eq(log_line)), Prefix(IsTrue()), 139 LogSeverity(Eq(absl::LogSeverity::kWarning)), 140 Timestamp(InMatchWindow()), 141 ThreadID(Eq(absl::base_internal::GetTID())), 142 TextMessage(Eq("hello world")), 143 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 144 ENCODED_MESSAGE(MatchesEvent( 145 Eq(__FILE__), Eq(log_line), InMatchWindow(), 146 Eq(logging::proto::WARNING), Eq(absl::base_internal::GetTID()), 147 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 148 Stacktrace(IsEmpty())))); 149 } 150 151 test_sink.StartCapturingLogs(); 152 do_log(); 153 } 154 155 TEST_P(BasicLogTest, Error) { 156 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 157 158 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 159 160 const int log_line = __LINE__ + 1; 161 auto do_log = [] { ABSL_TEST_LOG(ERROR) << "hello world"; }; 162 163 if (LoggingEnabledAt(absl::LogSeverity::kError)) { 164 EXPECT_CALL( 165 test_sink, 166 Send(AllOf( 167 SourceFilename(Eq(__FILE__)), 168 SourceBasename(Eq("log_basic_test_impl.inc")), 169 SourceLine(Eq(log_line)), Prefix(IsTrue()), 170 LogSeverity(Eq(absl::LogSeverity::kError)), 171 Timestamp(InMatchWindow()), 172 ThreadID(Eq(absl::base_internal::GetTID())), 173 TextMessage(Eq("hello world")), 174 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 175 ENCODED_MESSAGE(MatchesEvent( 176 Eq(__FILE__), Eq(log_line), InMatchWindow(), 177 Eq(logging::proto::ERROR), Eq(absl::base_internal::GetTID()), 178 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 179 Stacktrace(IsEmpty())))); 180 } 181 182 test_sink.StartCapturingLogs(); 183 do_log(); 184 } 185 186 TEST_P(BasicLogTest, DoNotSubmit) { 187 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 188 189 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 190 191 const int log_line = __LINE__ + 1; 192 auto do_log = [] { ABSL_TEST_LOG(DO_NOT_SUBMIT) << "hello world"; }; 193 194 if (LoggingEnabledAt(absl::LogSeverity::kError)) { 195 EXPECT_CALL( 196 test_sink, 197 Send(AllOf( 198 SourceFilename(Eq(__FILE__)), 199 SourceBasename(Eq("log_basic_test_impl.inc")), 200 SourceLine(Eq(log_line)), Prefix(IsTrue()), 201 LogSeverity(Eq(absl::LogSeverity::kError)), 202 Timestamp(InMatchWindow()), 203 ThreadID(Eq(absl::base_internal::GetTID())), 204 TextMessage(Eq("hello world")), 205 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 206 ENCODED_MESSAGE(MatchesEvent( 207 Eq(__FILE__), Eq(log_line), InMatchWindow(), 208 Eq(logging::proto::ERROR), Eq(absl::base_internal::GetTID()), 209 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 210 Stacktrace(IsEmpty())))); 211 } 212 213 test_sink.StartCapturingLogs(); 214 do_log(); 215 } 216 217 #if GTEST_HAS_DEATH_TEST 218 using BasicLogDeathTest = BasicLogTest; 219 220 INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogDeathTest, 221 testing::Values(absl::LogSeverityAtLeast::kInfo, 222 absl::LogSeverityAtLeast::kFatal, 223 absl::LogSeverityAtLeast::kInfinity), 224 ThresholdName); 225 226 TEST_P(BasicLogDeathTest, Fatal) { 227 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 228 229 const int log_line = __LINE__ + 1; 230 auto do_log = [] { ABSL_TEST_LOG(FATAL) << "hello world"; }; 231 232 EXPECT_EXIT( 233 { 234 absl::ScopedMockLog test_sink( 235 absl::MockLogDefault::kDisallowUnexpected); 236 237 EXPECT_CALL(test_sink, Send) 238 .Times(AnyNumber()) 239 .WillRepeatedly(DeathTestUnexpectedLogging()); 240 241 ::testing::InSequence s; 242 243 // Note the logic in DeathTestValidateExpectations() caters for the case 244 // of logging being disabled at FATAL level. 245 246 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { 247 // The first call without the stack trace. 248 EXPECT_CALL( 249 test_sink, 250 Send(AllOf(SourceFilename(Eq(__FILE__)), 251 SourceBasename(Eq("log_basic_test_impl.inc")), 252 SourceLine(Eq(log_line)), Prefix(IsTrue()), 253 LogSeverity(Eq(absl::LogSeverity::kFatal)), 254 Timestamp(InMatchWindow()), 255 ThreadID(Eq(absl::base_internal::GetTID())), 256 TextMessage(Eq("hello world")), 257 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 258 ENCODED_MESSAGE(MatchesEvent( 259 Eq(__FILE__), Eq(log_line), InMatchWindow(), 260 Eq(logging::proto::FATAL), 261 Eq(absl::base_internal::GetTID()), 262 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 263 Stacktrace(IsEmpty())))) 264 .WillOnce(DeathTestExpectedLogging()); 265 266 // The second call with the stack trace. 267 EXPECT_CALL( 268 test_sink, 269 Send(AllOf(SourceFilename(Eq(__FILE__)), 270 SourceBasename(Eq("log_basic_test_impl.inc")), 271 SourceLine(Eq(log_line)), Prefix(IsTrue()), 272 LogSeverity(Eq(absl::LogSeverity::kFatal)), 273 Timestamp(InMatchWindow()), 274 ThreadID(Eq(absl::base_internal::GetTID())), 275 TextMessage(Eq("hello world")), 276 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 277 ENCODED_MESSAGE(MatchesEvent( 278 Eq(__FILE__), Eq(log_line), InMatchWindow(), 279 Eq(logging::proto::FATAL), 280 Eq(absl::base_internal::GetTID()), 281 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 282 Stacktrace(Not(IsEmpty()))))) 283 .WillOnce(DeathTestExpectedLogging()); 284 } 285 286 test_sink.StartCapturingLogs(); 287 do_log(); 288 }, 289 DiedOfFatal, DeathTestValidateExpectations()); 290 } 291 292 TEST_P(BasicLogDeathTest, QFatal) { 293 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 294 295 const int log_line = __LINE__ + 1; 296 auto do_log = [] { ABSL_TEST_LOG(QFATAL) << "hello world"; }; 297 298 EXPECT_EXIT( 299 { 300 absl::ScopedMockLog test_sink( 301 absl::MockLogDefault::kDisallowUnexpected); 302 303 EXPECT_CALL(test_sink, Send) 304 .Times(AnyNumber()) 305 .WillRepeatedly(DeathTestUnexpectedLogging()); 306 307 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { 308 EXPECT_CALL( 309 test_sink, 310 Send(AllOf(SourceFilename(Eq(__FILE__)), 311 SourceBasename(Eq("log_basic_test_impl.inc")), 312 SourceLine(Eq(log_line)), Prefix(IsTrue()), 313 LogSeverity(Eq(absl::LogSeverity::kFatal)), 314 Timestamp(InMatchWindow()), 315 ThreadID(Eq(absl::base_internal::GetTID())), 316 TextMessage(Eq("hello world")), 317 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 318 ENCODED_MESSAGE(MatchesEvent( 319 Eq(__FILE__), Eq(log_line), InMatchWindow(), 320 Eq(logging::proto::FATAL), 321 Eq(absl::base_internal::GetTID()), 322 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 323 Stacktrace(IsEmpty())))) 324 .WillOnce(DeathTestExpectedLogging()); 325 } 326 327 test_sink.StartCapturingLogs(); 328 do_log(); 329 }, 330 DiedOfQFatal, DeathTestValidateExpectations()); 331 } 332 #endif 333 334 #ifdef NDEBUG 335 TEST_P(BasicLogTest, DFatal) { 336 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 337 338 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 339 340 const int log_line = __LINE__ + 1; 341 auto do_log = [] { ABSL_TEST_LOG(DFATAL) << "hello world"; }; 342 343 if (LoggingEnabledAt(absl::LogSeverity::kError)) { 344 EXPECT_CALL( 345 test_sink, 346 Send(AllOf( 347 SourceFilename(Eq(__FILE__)), 348 SourceBasename(Eq("log_basic_test_impl.inc")), 349 SourceLine(Eq(log_line)), Prefix(IsTrue()), 350 LogSeverity(Eq(absl::LogSeverity::kError)), 351 Timestamp(InMatchWindow()), 352 ThreadID(Eq(absl::base_internal::GetTID())), 353 TextMessage(Eq("hello world")), 354 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 355 ENCODED_MESSAGE(MatchesEvent( 356 Eq(__FILE__), Eq(log_line), InMatchWindow(), 357 Eq(logging::proto::ERROR), Eq(absl::base_internal::GetTID()), 358 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 359 Stacktrace(IsEmpty())))); 360 } 361 362 test_sink.StartCapturingLogs(); 363 do_log(); 364 } 365 366 #elif GTEST_HAS_DEATH_TEST 367 TEST_P(BasicLogDeathTest, DFatal) { 368 // TODO(b/242568884): re-enable once bug is fixed. 369 // absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 370 371 const int log_line = __LINE__ + 1; 372 auto do_log = [] { ABSL_TEST_LOG(DFATAL) << "hello world"; }; 373 374 EXPECT_EXIT( 375 { 376 absl::ScopedMockLog test_sink( 377 absl::MockLogDefault::kDisallowUnexpected); 378 379 EXPECT_CALL(test_sink, Send) 380 .Times(AnyNumber()) 381 .WillRepeatedly(DeathTestUnexpectedLogging()); 382 383 ::testing::InSequence s; 384 385 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { 386 // The first call without the stack trace. 387 EXPECT_CALL( 388 test_sink, 389 Send(AllOf(SourceFilename(Eq(__FILE__)), 390 SourceBasename(Eq("log_basic_test_impl.inc")), 391 SourceLine(Eq(log_line)), Prefix(IsTrue()), 392 LogSeverity(Eq(absl::LogSeverity::kFatal)), 393 Timestamp(InMatchWindow()), 394 ThreadID(Eq(absl::base_internal::GetTID())), 395 TextMessage(Eq("hello world")), 396 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 397 ENCODED_MESSAGE(MatchesEvent( 398 Eq(__FILE__), Eq(log_line), InMatchWindow(), 399 Eq(logging::proto::FATAL), 400 Eq(absl::base_internal::GetTID()), 401 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 402 Stacktrace(IsEmpty())))) 403 .WillOnce(DeathTestExpectedLogging()); 404 405 // The second call with the stack trace. 406 EXPECT_CALL( 407 test_sink, 408 Send(AllOf(SourceFilename(Eq(__FILE__)), 409 SourceBasename(Eq("log_basic_test_impl.inc")), 410 SourceLine(Eq(log_line)), Prefix(IsTrue()), 411 LogSeverity(Eq(absl::LogSeverity::kFatal)), 412 Timestamp(InMatchWindow()), 413 ThreadID(Eq(absl::base_internal::GetTID())), 414 TextMessage(Eq("hello world")), 415 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 416 ENCODED_MESSAGE(MatchesEvent( 417 Eq(__FILE__), Eq(log_line), InMatchWindow(), 418 Eq(logging::proto::FATAL), 419 Eq(absl::base_internal::GetTID()), 420 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 421 Stacktrace(Not(IsEmpty()))))) 422 .WillOnce(DeathTestExpectedLogging()); 423 } 424 425 test_sink.StartCapturingLogs(); 426 do_log(); 427 }, 428 DiedOfFatal, DeathTestValidateExpectations()); 429 } 430 #endif 431 432 #ifndef NDEBUG 433 TEST_P(BasicLogTest, DFatalIsCancellable) { 434 // LOG(DFATAL) does not die when DFATAL death is disabled. 435 absl::log_internal::SetExitOnDFatal(false); 436 ABSL_TEST_LOG(DFATAL) << "hello world"; 437 absl::log_internal::SetExitOnDFatal(true); 438 } 439 440 #if GTEST_HAS_DEATH_TEST 441 TEST_P(BasicLogDeathTest, DLogFatalIsNotCancellable) { 442 EXPECT_EXIT( 443 { 444 absl::log_internal::SetExitOnDFatal(false); 445 ABSL_TEST_DLOG(FATAL) << "hello world"; 446 absl::log_internal::SetExitOnDFatal(true); 447 }, 448 DiedOfFatal, ""); 449 } 450 #endif 451 #endif 452 453 TEST_P(BasicLogTest, Level) { 454 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 455 456 for (auto severity : {absl::LogSeverity::kInfo, absl::LogSeverity::kWarning, 457 absl::LogSeverity::kError}) { 458 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 459 460 const int log_line = __LINE__ + 2; 461 auto do_log = [severity] { 462 ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; 463 }; 464 465 if (LoggingEnabledAt(severity)) { 466 EXPECT_CALL( 467 test_sink, 468 Send(AllOf( 469 SourceFilename(Eq(__FILE__)), 470 SourceBasename(Eq("log_basic_test_impl.inc")), 471 SourceLine(Eq(log_line)), Prefix(IsTrue()), 472 LogSeverity(Eq(severity)), Timestamp(InMatchWindow()), 473 ThreadID(Eq(absl::base_internal::GetTID())), 474 TextMessage(Eq("hello world")), 475 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 476 ENCODED_MESSAGE(MatchesEvent( 477 Eq(__FILE__), Eq(log_line), InMatchWindow(), 478 Eq(severity == absl::LogSeverity::kInfo ? logging::proto::INFO 479 : severity == absl::LogSeverity::kWarning 480 ? logging::proto::WARNING 481 : severity == absl::LogSeverity::kError 482 ? logging::proto::ERROR 483 : 0), 484 Eq(absl::base_internal::GetTID()), 485 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 486 Stacktrace(IsEmpty())))); 487 } 488 test_sink.StartCapturingLogs(); 489 do_log(); 490 } 491 } 492 493 #if GTEST_HAS_DEATH_TEST 494 TEST_P(BasicLogDeathTest, Level) { 495 // TODO(b/242568884): re-enable once bug is fixed. 496 // absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 497 498 // Ensure that `severity` is not a compile-time constant to prove that 499 // `LOG(LEVEL(severity))` works regardless: 500 auto volatile severity = absl::LogSeverity::kFatal; 501 502 const int log_line = __LINE__ + 1; 503 auto do_log = [severity] { ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; }; 504 505 EXPECT_EXIT( 506 { 507 absl::ScopedMockLog test_sink( 508 absl::MockLogDefault::kDisallowUnexpected); 509 510 EXPECT_CALL(test_sink, Send) 511 .Times(AnyNumber()) 512 .WillRepeatedly(DeathTestUnexpectedLogging()); 513 514 ::testing::InSequence s; 515 516 if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { 517 EXPECT_CALL( 518 test_sink, 519 Send(AllOf(SourceFilename(Eq(__FILE__)), 520 SourceBasename(Eq("log_basic_test_impl.inc")), 521 SourceLine(Eq(log_line)), Prefix(IsTrue()), 522 LogSeverity(Eq(absl::LogSeverity::kFatal)), 523 Timestamp(InMatchWindow()), 524 ThreadID(Eq(absl::base_internal::GetTID())), 525 TextMessage(Eq("hello world")), 526 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 527 ENCODED_MESSAGE(MatchesEvent( 528 Eq(__FILE__), Eq(log_line), InMatchWindow(), 529 Eq(logging::proto::FATAL), 530 Eq(absl::base_internal::GetTID()), 531 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 532 Stacktrace(IsEmpty())))) 533 .WillOnce(DeathTestExpectedLogging()); 534 535 EXPECT_CALL( 536 test_sink, 537 Send(AllOf(SourceFilename(Eq(__FILE__)), 538 SourceBasename(Eq("log_basic_test_impl.inc")), 539 SourceLine(Eq(log_line)), Prefix(IsTrue()), 540 LogSeverity(Eq(absl::LogSeverity::kFatal)), 541 Timestamp(InMatchWindow()), 542 ThreadID(Eq(absl::base_internal::GetTID())), 543 TextMessage(Eq("hello world")), 544 Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), 545 ENCODED_MESSAGE(MatchesEvent( 546 Eq(__FILE__), Eq(log_line), InMatchWindow(), 547 Eq(logging::proto::FATAL), 548 Eq(absl::base_internal::GetTID()), 549 ElementsAre(ValueWithLiteral(Eq("hello world"))))), 550 Stacktrace(Not(IsEmpty()))))) 551 .WillOnce(DeathTestExpectedLogging()); 552 } 553 554 test_sink.StartCapturingLogs(); 555 do_log(); 556 }, 557 DiedOfFatal, DeathTestValidateExpectations()); 558 } 559 #endif 560 561 TEST_P(BasicLogTest, LevelClampsNegativeValues) { 562 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 563 564 if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) { 565 GTEST_SKIP() << "This test cases required INFO log to be enabled"; 566 return; 567 } 568 569 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 570 571 EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kInfo)))); 572 573 test_sink.StartCapturingLogs(); 574 ABSL_TEST_LOG(LEVEL(-1)) << "hello world"; 575 } 576 577 TEST_P(BasicLogTest, LevelClampsLargeValues) { 578 absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); 579 580 if (!LoggingEnabledAt(absl::LogSeverity::kError)) { 581 GTEST_SKIP() << "This test cases required ERROR log to be enabled"; 582 return; 583 } 584 585 absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); 586 587 EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kError)))); 588 589 test_sink.StartCapturingLogs(); 590 ABSL_TEST_LOG(LEVEL(static_cast<int>(absl::LogSeverity::kFatal) + 1)) 591 << "hello world"; 592 } 593 594 TEST(ErrnoPreservationTest, InSeverityExpression) { 595 errno = 77; 596 int saved_errno; 597 ABSL_TEST_LOG(LEVEL((saved_errno = errno, absl::LogSeverity::kInfo))); 598 EXPECT_THAT(saved_errno, Eq(77)); 599 } 600 601 TEST(ErrnoPreservationTest, InStreamedExpression) { 602 if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) { 603 GTEST_SKIP() << "This test cases required INFO log to be enabled"; 604 return; 605 } 606 607 errno = 77; 608 int saved_errno = 0; 609 ABSL_TEST_LOG(INFO) << (saved_errno = errno, "hello world"); 610 EXPECT_THAT(saved_errno, Eq(77)); 611 } 612 613 TEST(ErrnoPreservationTest, AfterStatement) { 614 errno = 77; 615 ABSL_TEST_LOG(INFO); 616 const int saved_errno = errno; 617 EXPECT_THAT(saved_errno, Eq(77)); 618 } 619 620 // Tests that using a variable/parameter in a logging statement suppresses 621 // unused-variable/parameter warnings. 622 // ----------------------------------------------------------------------- 623 class UnusedVariableWarningCompileTest { 624 // These four don't prove anything unless `ABSL_MIN_LOG_LEVEL` is greater than 625 // `kInfo`. 626 static void LoggedVariable() { 627 const int x = 0; 628 ABSL_TEST_LOG(INFO) << x; 629 } 630 static void LoggedParameter(const int x) { ABSL_TEST_LOG(INFO) << x; } 631 static void SeverityVariable() { 632 const int x = 0; 633 ABSL_TEST_LOG(LEVEL(x)) << "hello world"; 634 } 635 static void SeverityParameter(const int x) { 636 ABSL_TEST_LOG(LEVEL(x)) << "hello world"; 637 } 638 }; 639 640 } // namespace absl_log_internal 641 642 #endif // ABSL_LOG_LOG_BASIC_TEST_IMPL_H_