From 939dba6076e0056c997fa789f1a61b4ee42f057e Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Fri, 21 Jun 2024 18:06:16 -0700 Subject: [PATCH] Restore "Check failure stack trace" message on LOG(FATAL) https://github.com/google/glog/pull/1074 refactored some of the code to enable the failure function to throw. This made it so when the LogMessageFatal class was used, the error ended up being printed differently. Before: LOG_AT_LEVEL(google::LogSeverity::FATAL) << "Crash: Hello world!"; -> F20240621 18:12:44.710584 139620827212672 log_demo.cc:16] Crash: Hello world! *** Check failure stack trace: *** @ 0x559e2704711a @ 0x7efc01fac24a @ 0x7efc01fac305 @ 0x559e27046dd5 Aborted LOG(FATAL) << "Crash: Hello world!"; -> F20240621 18:13:05.760556 140518290856832 log_demo.cc:16] Crash: Hello world! @ 0x55cdc2475130 @ 0x7fccf6fb324a @ 0x7fccf6fb3305 @ 0x55cdc2474df5 Aborted With this patch, they both produce the same output. Signed-off-by: Austin Schuh --- src/logging.cc | 7 +++++-- src/logging_unittest.cc | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/logging.cc b/src/logging.cc index 08b2ab387..78ca31f22 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -2541,8 +2541,11 @@ LogMessageFatal::LogMessageFatal(const char* file, int line, : LogMessage(file, line, result) {} LogMessageFatal::~LogMessageFatal() noexcept(false) { - Flush(); - LogMessage::Fail(); + // We really want [[noreturn]] on the destructor so the compiler can use it. + // We really just want to reuse the parent class's destructor since it has all + // the right logic in it. + LogMessage::~LogMessage(); + Fail(); } namespace logging { diff --git a/src/logging_unittest.cc b/src/logging_unittest.cc index 321f38fa3..85a7fc0c6 100644 --- a/src/logging_unittest.cc +++ b/src/logging_unittest.cc @@ -1586,3 +1586,12 @@ TEST(Logging, FatalThrow) { ScopedExit restore{restore_fail}; EXPECT_THROW({ LOG(FATAL) << "must throw to fail"; }, std::logic_error); } + +TEST(DeathLogging, ErrorMessage) { + ASSERT_DEATH({ LOG(FATAL) << "foo"; }, "Check failure stack trace"); + ASSERT_DEATH( + { + LOG_AT_LEVEL(google; : LogSeverity::FATAL) << "foo"; + }, + "Check failure stack trace"); +}