X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fapplication.cpp;h=8447eed669d2b8f58c9b078b547b1543b0bab27f;hp=badc52304ab4df6dc8c563aa9708109e5c387959;hb=HEAD;hpb=5ad225535ae12d53fd79c8f9fa764bd586211b8f diff --git a/source/core/application.cpp b/source/core/application.cpp index badc523..c214a06 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -1,116 +1,138 @@ -/* $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; namespace Msp { -Application::Application(): - exit_code(0) -{ } +Application *Application::_app = nullptr; +Application::Starter *Application::_starter = nullptr; +const char *Application::_argv0 = nullptr; +string Application::_name; +void *Application::_data = nullptr; -/** -Constructs an instance of the registered application class and runs it. If the -application throws a UsageError, the static usage() function is called. - -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) +Application::Application(const string &n) { - static bool called=false; - if(called) - { - cerr<<"Trying to call Application::run_app twice!\n"; - return 125; - } - called=true; + if(_app) + throw already_called("Application::Application"); + + if(!n.empty()) + _name = n; + else + _name = FS::basename(_argv0); +} - if(!starter_) +int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *)) +{ + 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 { try { - app_=starter_->create_app(argc, argv); + _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; } - int result=app_->main(); - Application *a=app_; - app_=0; + if(created_callback) + created_callback(data); + + int result = _app->main(); + Application *a = _app; + _app = nullptr; delete a; return result; } 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(auto i=lines.begin(); ++i!=lines.end(); ) + IO::print(IO::cerr, " %s\n", *i); + } } -#endif + + delete _app; + _app = nullptr; return 124; } } -/** -Prints a message describing the usage of the application. The default version -will blame the programmer for being lazy. +void Application::set_startup_info(const char *argv0, void *data) +{ + if(_argv0) + throw already_called("Application::set_startup_info"); + + static FS::Path exe; + + if(!argv0 || !*argv0) + { +#ifdef _WIN32 + argv0 = "application.exe"; +#else + argv0 = "./application"; +#endif + } + + bool has_slash = strchr(argv0, FS::DIRSEP); + if(!has_slash) + exe = FS::path_lookup(argv0); + if(exe.empty()) + exe = FS::realpath(argv0); + + _argv0 = exe.c_str(); + _data = data; +} + +void *Application::get_data() +{ + return _data; +} + +const char *Application::get_argv0() +{ + return _argv0; +} -@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) +const std::string &Application::get_name() { - if(reason) - cerr<<"UsageError: "<sighandler(s); + _app->sighandler(s); } Application::Starter::Starter() { - if(starter_) - throw InvalidState("Can't create more than one Starter instance"); + if(_starter) + throw already_called("Application::Starter::Starter"); - starter_=this; + _starter = this; } -Application *Application::app_=0; -Application::RegBase *Application::reg_app_=0; -void *Application::data_=0; - } // namespace Msp