File: ExceptionCatchingApplication.hpp

package info (click to toggle)
wsjtx 2.6.1%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 69,664 kB
  • sloc: cpp: 86,977; f90: 42,417; python: 27,241; ansic: 12,510; fortran: 2,382; makefile: 197; sh: 134
file content (49 lines) | stat: -rwxr-xr-x 1,327 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef EXCEPTION_CATCHING_APPLICATION_HPP__
#define EXCEPTION_CATCHING_APPLICATION_HPP__

#include <QApplication>

#include <boost/log/core.hpp>
#include "Logger.hpp"

class QObject;
class QEvent;

//
// We  can't  use the  GUI  after  QApplication::exit() is  called  so
// uncaught exceptions can get lost  on Windows systems where there is
// no console terminal, so here we override QApplication::notify() and
// wrap the  base class  call with a  try block to  catch and  log any
// uncaught exceptions.
//
class ExceptionCatchingApplication
  : public QApplication
{
public:
  explicit ExceptionCatchingApplication (int& argc, char * * argv)
    : QApplication {argc, argv}
  {
  }
  bool notify (QObject * receiver, QEvent * e) override
  {
    try
      {
        return QApplication::notify (receiver, e);
      }
    catch (std::exception const& e)
      {
        LOG_FATAL ("Unexpected exception caught in event loop: " << e.what ());
      }
    catch (...)
      {
        LOG_FATAL ("Unexpected unknown exception caught in event loop");
      }
    // There's nowhere to go from here as Qt will not pass exceptions
    // through the event loop, so we must abort.
    boost::log::core::get ()->flush ();
    qFatal ("Aborting");
    return false;
  }
};

#endif