X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fapplication.cpp;h=290f20b05239702c40746f9048aba993aa877b21;hp=139f94f2d88054d745ad9119b562dd7ec174d596;hb=9c8bb278a26c0c4356d6ee5e66d3f34c70521fb8;hpb=d5dd704b2576f878809e87dbb8ff8591b9bdbce4 diff --git a/source/core/application.cpp b/source/core/application.cpp index 139f94f..290f20b 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -1,19 +1,15 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2008, 2011 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include -#include +#include #include -#include "../debug/backtrace.h" -#include "../debug/demangle.h" -#include "../time/units.h" -#include "../time/utils.h" +#include +#include +#include +#include +#include +#include +#include +#include #include "application.h" -#include "except.h" +#include "getopt.h" using namespace std; @@ -21,36 +17,31 @@ namespace Msp { Application *Application::app_ = 0; Application::Starter *Application::starter_ = 0; +const char *Application::argv0_ = 0; +string Application::name_; void *Application::data_ = 0; -Application::Application(): +Application::Application(const string &n): exit_code(0) -{ } +{ + if(app_) + throw logic_error("instance already exists"); -/** -Constructs an instance of the registered application class and runs it. If the -application throws a UsageError, the static usage() function is called. + if(!n.empty()) + name_ = n; + else + name_ = FS::basename(argv0_); +} -This function can only be called once. The global main() function provided by -the library normally does it automatically at program startup. -*/ -int Application::run(int argc, char **argv, void *data) +int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *)) { - static bool called = false; - if(called) - { - cerr<<"Trying to call Application::run_app twice!\n"; - return 125; - } - called = true; - if(!starter_) { - cerr<<"Trying to run with no RegisteredApplication class!\n"; + IO::cerr.write("Application::run called with no RegisteredApplication class!\n"); return 126; } - data_ = data; + set_startup_info(argv[0], data); try { @@ -58,12 +49,15 @@ int Application::run(int argc, char **argv, void *data) { app_ = starter_->create_app(argc, argv); } - catch(const UsageError &e) + catch(const usage_error &e) { - starter_->usage(e.what(), argv[0], e.get_brief()); + IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help()); return 1; } + if(created_callback) + created_callback(data); + int result = app_->main(); Application *a = app_; app_ = 0; @@ -72,49 +66,58 @@ int Application::run(int argc, char **argv, void *data) } catch(const exception &e) { - delete app_; - -#ifdef WIN32 - string msg = Debug::demangle(typeid(e).name())+":\n"+e.what(); - MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR); -#else - cerr<<"An uncaught exception occurred.\n"; - cerr<<" type: "<report_uncaught_exception(e); - const Exception *exc = dynamic_cast(&e); - if(exc && !exc->get_backtrace().get_frames().empty()) + if(!handled) { - cerr<<" backtrace:\n"; - const list &frames = exc->get_backtrace().get_frames(); - for(list::const_iterator i=frames.begin(); i!=frames.end(); ++i) - cerr<<" "<<*i<<'\n'; + IO::print(IO::cerr, "An uncaught exception occurred.\n"); + IO::print(IO::cerr, " type: %s\n", Debug::demangle(typeid(e).name())); + vector lines = split(e.what(), '\n'); + if(lines.size()<2) + IO::print(IO::cerr, " what(): %s\n", e.what()); + else + { + IO::print(IO::cerr, " what(): %s\n", lines.front()); + for(vector::const_iterator i=lines.begin(); ++i!=lines.end(); ) + IO::print(IO::cerr, " %s\n", *i); + } } -#endif + + delete app_; + app_ = 0; return 124; } } -/** -Prints a message describing the usage of the application. The default version -will blame the programmer for being lazy. - -@param reason Why the function was called -@param argv0 The value of argv[0], to be used in the message -@param brief Whether to print a brief or long usage message -*/ -void Application::usage(const char *reason, const char *, bool) +void Application::set_startup_info(const char *argv0, void *data) { - if(reason) - cerr<<"UsageError: "<sighandler(s); @@ -153,7 +147,7 @@ void Application::sighandler_(int s) Application::Starter::Starter() { if(starter_) - throw InvalidState("Can't create more than one Starter instance"); + throw logic_error("Can't create more than one Starter instance"); starter_ = this; }