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"
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;
24 Application::Application(const string &n):
28 throw logic_error("instance already exists");
33 name_ = FS::basename(argv0_);
36 int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *))
40 IO::cerr.write("Application::run called with no RegisteredApplication class!\n");
44 set_startup_info(argv[0], data);
50 app_ = starter_->create_app(argc, argv);
52 catch(const usage_error &e)
54 IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help());
59 created_callback(data);
61 int result = app_->main();
62 Application *a = app_;
67 catch(const exception &e)
70 if(const Debug::ErrorReporter *er = Debug::ErrorReporter::get_current())
71 handled = er->report_uncaught_exception(e);
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');
79 IO::print(IO::cerr, " what(): %s\n", e.what());
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);
95 void Application::set_startup_info(const char *argv0, void *data)
98 throw logic_error("startup info already set");
102 if(!argv0 || !*argv0)
105 argv0 = "application.exe";
107 argv0 = "./application";
111 bool has_slash = strchr(argv0, FS::DIRSEP);
113 exe = FS::path_lookup(argv0);
115 exe = FS::realpath(argv0);
117 argv0_ = exe.c_str();
121 int Application::main()
130 void Application::catch_signal(int s)
132 signal(s, &sighandler_);
135 void Application::exit(int c)
141 void Application::sighandler_(int s)
147 Application::Starter::Starter()
150 throw logic_error("Can't create more than one Starter instance");