1
#pragma once
2

            
3
#include <exception>
4

            
5
#include "source/common/common/logger.h"
6
#include "source/common/common/non_copyable.h"
7

            
8
namespace Envoy {
9

            
10
class TerminateHandler : NonCopyable, protected Logger::Loggable<Logger::Id::main> {
11
public:
12
49
  TerminateHandler() : previous_terminate_(logOnTerminate()) {}
13
49
  ~TerminateHandler() { std::set_terminate(previous_terminate_); }
14

            
15
private:
16
  friend class TerminateHandlerTest;
17

            
18
  /**
19
   * Sets the std::terminate to a function which will log as much of a backtrace as
20
   * possible, then call abort. Returns the previous handler.
21
   */
22
  std::terminate_handler logOnTerminate() const;
23

            
24
  /**
25
   * Logs exception details when possible
26
   */
27
  static void logException(const std::exception_ptr);
28

            
29
  const std::terminate_handler previous_terminate_;
30
};
31
} // namespace Envoy