blob: 7765b5b0d884769fe35fe0013217b9f09f8bcf8d [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
9namespace aos {
10namespace util {
11
12// Prints all FATAL messages to stderr and then abort(3)s before the regular
13// stuff can print out anything else. Ignores all other messages.
14// This is useful in death tests that expect a LOG(FATAL) to cause the death.
Tyler Chatowbf0609c2021-07-31 16:13:27 -070015class DeathTestLogImplementation
16 : public logging::HandleMessageLogImplementation {
Brian Silverman01be0002014-05-10 15:44:38 -070017 public:
Austin Schuhad9e5eb2021-11-19 20:33:55 -080018 std::string_view MyName() override {
19 logging::internal::Context *context = logging::internal::Context::Get();
20 return context->MyName();
21 }
Brian Silverman01be0002014-05-10 15:44:38 -070022 virtual void HandleMessage(const logging::LogMessage &message) override {
23 if (message.level == FATAL) {
24 logging::internal::PrintMessage(stderr, message);
25 abort();
26 }
27 }
28};
29
30} // namespace util
31} // namespace aos
32
John Park33858a32018-09-28 23:05:48 -070033#endif // AOS_UTIL_DEATH_TEST_LOG_IMPLEMENTATION_H_