X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fapplication.cpp;h=d5745ed8a36ae2cbe139a14de0f1ba76c1b22d5c;hp=139f94f2d88054d745ad9119b562dd7ec174d596;hb=0f5501ad79950e9658fb672ac6b97a608e3a2dd9;hpb=b56eb5ec1da675da0c66abc53c1e4f6c4e4cccbd diff --git a/source/core/application.cpp b/source/core/application.cpp index 139f94f..d5745ed 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -1,19 +1,10 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2008, 2011 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include -#include #include -#include "../debug/backtrace.h" -#include "../debug/demangle.h" -#include "../time/units.h" -#include "../time/utils.h" +#include +#include +#include +#include #include "application.h" -#include "except.h" +#include "getopt.h" using namespace std; @@ -25,28 +16,16 @@ void *Application::data_ = 0; Application::Application(): exit_code(0) -{ } - -/** -Constructs an instance of the registered application class and runs it. If the -application throws a UsageError, the static usage() function is called. +{ + if(app_) + throw logic_error("instance already exists"); +} -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) { - 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; } @@ -58,9 +37,9 @@ 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; } @@ -72,49 +51,24 @@ 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())); + IO::print(IO::cerr, " what(): %s\n", e.what()); } -#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) -{ - if(reason) - cerr<<"UsageError: "<sighandler(s); @@ -153,7 +98,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; }