]> git.tdb.fi Git - libs/core.git/blob - source/core/application.cpp
Nicer formatting of exceptions with multi-line whats
[libs/core.git] / source / core / application.cpp
1 #include <cstring>
2 #include <typeinfo>
3 #include <signal.h>
4 #include <msp/debug/demangle.h>
5 #include <msp/debug/errorreporter.h>
6 #include <msp/fs/dir.h>
7 #include <msp/fs/path.h>
8 #include <msp/fs/utils.h>
9 #include <msp/io/print.h>
10 #include <msp/strings/utils.h>
11 #include "application.h"
12 #include "getopt.h"
13
14 using namespace std;
15
16 namespace Msp {
17
18 Application *Application::app_ = 0;
19 Application::Starter *Application::starter_ = 0;
20 const char *Application::argv0_ = 0;
21 string Application::name_;
22 void *Application::data_ = 0;
23
24 Application::Application(const string &n):
25         exit_code(0)
26 {
27         if(app_)
28                 throw logic_error("instance already exists");
29
30         if(!n.empty())
31                 name_ = n;
32         else
33                 name_ = FS::basename(argv0_);
34 }
35
36 int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *))
37 {
38         if(!starter_)
39         {
40                 IO::cerr.write("Application::run called with no RegisteredApplication class!\n");
41                 return 126;
42         }
43
44         set_startup_info(argv[0], data);
45
46         try
47         {
48                 try
49                 {
50                         app_ = starter_->create_app(argc, argv);
51                 }
52                 catch(const usage_error &e)
53                 {
54                         IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help());
55                         return 1;
56                 }
57
58                 if(created_callback)
59                         created_callback(data);
60
61                 int result = app_->main();
62                 Application *a = app_;
63                 app_ = 0;
64                 delete a;
65                 return result;
66         }
67         catch(const exception &e)
68         {
69                 bool handled = false;
70                 if(const Debug::ErrorReporter *er = Debug::ErrorReporter::get_current())
71                         handled = er->report_uncaught_exception(e);
72
73                 if(!handled)
74                 {
75                         IO::print(IO::cerr, "An uncaught exception occurred.\n");
76                         IO::print(IO::cerr, "  type:   %s\n", Debug::demangle(typeid(e).name()));
77                         vector<string> lines = split(e.what(), '\n');
78                         if(lines.size()<2)
79                                 IO::print(IO::cerr, "  what(): %s\n", e.what());
80                         else
81                         {
82                                 IO::print(IO::cerr, "  what(): %s\n", lines.front());
83                                 for(vector<string>::const_iterator i=lines.begin(); ++i!=lines.end(); )
84                                         IO::print(IO::cerr, "          %s\n", *i);
85                         }
86                 }
87
88                 delete app_;
89                 app_ = 0;
90
91                 return 124;
92         }
93 }
94
95 void Application::set_startup_info(const char *argv0, void *data)
96 {
97         if(argv0_)
98                 throw logic_error("startup info already set");
99
100         static FS::Path exe;
101
102         if(!argv0 || !*argv0)
103         {
104 #ifdef _WIN32
105                 argv0 = "application.exe";
106 #else
107                 argv0 = "./application";
108 #endif
109         }
110
111         bool has_slash = strchr(argv0, FS::DIRSEP);
112         if(!has_slash)
113                 exe = FS::path_lookup(argv0);
114         if(exe.empty())
115                 exe = FS::realpath(argv0);
116
117         argv0_ = exe.c_str();
118         data_ = data;
119 }
120
121 int Application::main()
122 {
123         done = false;
124         while(!done)
125                 tick();
126
127         return exit_code;
128 }
129
130 void Application::catch_signal(int s)
131 {
132         signal(s, &sighandler_);
133 }
134
135 void Application::exit(int c)
136 {
137         done = true;
138         exit_code = c;
139 }
140
141 void Application::sighandler_(int s)
142 {
143         app_->sighandler(s);
144 }
145
146
147 Application::Starter::Starter()
148 {
149         if(starter_)
150                 throw logic_error("Can't create more than one Starter instance");
151
152         starter_ = this;
153 }
154
155 } // namespace Msp