]> git.tdb.fi Git - libs/core.git/commitdiff
Redesign uncaught exception handling to allow external reporters
authorMikko Rasa <tdb@tdb.fi>
Sat, 5 Oct 2013 12:12:35 +0000 (15:12 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 5 Oct 2013 12:12:35 +0000 (15:12 +0300)
This helps keep UI code out of the core library.  It was becoming a
problem especially on OS X where including an Objective-C wrapper around
NSAlert would have broken Builder's bootstrap.  Libmspgui will acquire
a class that will present graphical error dialogs on each platform.

source/core/application.cpp
source/core/application.h
source/core/unix/application.cpp [deleted file]
source/core/windows/application.cpp [deleted file]
source/debug/errorreporter.cpp [new file with mode: 0644]
source/debug/errorreporter.h [new file with mode: 0644]

index ebd928b7b7ce049cd2d33ebcedba2ab8f8d4f190..43bb023143679e7fdf5e64d893208f859222d973 100644 (file)
@@ -1,4 +1,7 @@
+#include <typeinfo>
 #include <signal.h>
+#include <msp/debug/demangle.h>
+#include <msp/debug/errorreporter.h>
 #include <msp/io/print.h>
 #include "application.h"
 #include "getopt.h"
@@ -53,9 +56,18 @@ int Application::run(int argc, char **argv, void *data)
        }
        catch(const exception &e)
        {
-               delete app_;
+               bool handled = false;
+               if(const Debug::ErrorReporter *er = Debug::ErrorReporter::get_current())
+                       handled = er->report_uncaught_exception(e);
 
-               display_exception(e);
+               if(!handled)
+               {
+                       IO::print(IO::cerr, "An uncaught exception occurred.\n");
+                       IO::print(IO::cerr, "  type:   %s\n", Debug::demangle(typeid(e).name()));
+                       IO::print(IO::cerr, "  what(): %s\n", e.what());
+               }
+
+               delete app_;
 
                return 124;
        }
index 1384085ef58c3b134b9aa0abecbc68fcbd9d7e9e..e1d3a8b5e2be95d0c2fd0ffce151a1347264f080 100644 (file)
@@ -66,8 +66,6 @@ private:
        /** Static wrapper function to call a member function of the Application
        instance. */
        static void sighandler_(int);
-
-       static void display_exception(const std::exception &);
 };
 
 
diff --git a/source/core/unix/application.cpp b/source/core/unix/application.cpp
deleted file mode 100644 (file)
index 7f45fbc..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <typeinfo>
-#include <msp/debug/demangle.h>
-#include <msp/io/print.h>
-#include "application.h"
-
-using namespace std;
-
-namespace Msp {
-
-void Application::display_exception(const exception &e)
-{
-       IO::print(IO::cerr, "An uncaught exception occurred.\n");
-       IO::print(IO::cerr, "  type:   %s\n", Debug::demangle(typeid(e).name()));
-       IO::print(IO::cerr, "  what(): %s\n", e.what());
-}
-
-} // namespace Msp
diff --git a/source/core/windows/application.cpp b/source/core/windows/application.cpp
deleted file mode 100644 (file)
index ed8702a..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <windows.h>
-#include <typeinfo>
-#include <msp/debug/demangle.h>
-#include "application.h"
-
-using namespace std;
-
-namespace Msp {
-
-void Application::display_exception(const exception &e)
-{
-       string msg = Debug::demangle(typeid(e).name())+":\n"+e.what();
-       MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR);
-}
-
-} // namespace Msp
diff --git a/source/debug/errorreporter.cpp b/source/debug/errorreporter.cpp
new file mode 100644 (file)
index 0000000..d4d51bc
--- /dev/null
@@ -0,0 +1,20 @@
+#include "errorreporter.h"
+
+namespace Msp {
+namespace Debug {
+
+ErrorReporter *ErrorReporter::current = 0;
+
+ErrorReporter::ErrorReporter():
+       prev(current)
+{
+       current = this;
+}
+
+ErrorReporter::~ErrorReporter()
+{
+       current = prev;
+}
+
+} // namespace Debug
+} // namespace Msp
diff --git a/source/debug/errorreporter.h b/source/debug/errorreporter.h
new file mode 100644 (file)
index 0000000..b0385ff
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef MSP_DEBUG_ERRORREPORTER_H_
+#define MSP_DEBUG_ERRORREPORTER_H_
+
+#include <stdexcept>
+
+namespace Msp {
+namespace Debug {
+
+class ErrorReporter
+{
+private:
+       ErrorReporter *prev;
+
+       static ErrorReporter *current;
+
+protected:
+       ErrorReporter();
+public:
+       virtual ~ErrorReporter();
+
+       static const ErrorReporter *get_current() { return current; }
+
+       virtual bool report_uncaught_exception(const std::exception &) const = 0;
+};
+
+} // namespace Debug
+} // namespace Msp
+
+#endif