blob: e35f3654542424277b420371fbfc26516da56a6d [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_UTIL_DEATH_TEST_LOG_IMPLEMENTATION_H_
2#define AOS_UTIL_DEATH_TEST_LOG_IMPLEMENTATION_H_
Brian Silverman01be0002014-05-10 15:44:38 -07003
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdlib>
Brian Silverman01be0002014-05-10 15:44:38 -07005
Austin Schuhad9e5eb2021-11-19 20:33:55 -08006#include "aos/logging/context.h"
John Park33858a32018-09-28 23:05:48 -07007#include "aos/logging/implementations.h"
Brian Silverman01be0002014-05-10 15:44:38 -07008
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08009namespace aos::util {
Brian Silverman01be0002014-05-10 15:44:38 -070010
11// Prints all FATAL messages to stderr and then abort(3)s before the regular
12// stuff can print out anything else. Ignores all other messages.
13// This is useful in death tests that expect a LOG(FATAL) to cause the death.
Tyler Chatowbf0609c2021-07-31 16:13:27 -070014class DeathTestLogImplementation
15 : public logging::HandleMessageLogImplementation {
Brian Silverman01be0002014-05-10 15:44:38 -070016 public:
Austin Schuhad9e5eb2021-11-19 20:33:55 -080017 std::string_view MyName() override {
18 logging::internal::Context *context = logging::internal::Context::Get();
19 return context->MyName();
20 }
Brian Silverman01be0002014-05-10 15:44:38 -070021 virtual void HandleMessage(const logging::LogMessage &message) override {
22 if (message.level == FATAL) {
23 logging::internal::PrintMessage(stderr, message);
24 abort();
25 }
26 }
27};
28
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080029} // namespace aos::util
Brian Silverman01be0002014-05-10 15:44:38 -070030
John Park33858a32018-09-28 23:05:48 -070031#endif // AOS_UTIL_DEATH_TEST_LOG_IMPLEMENTATION_H_