+#include <cstring>
#include <typeinfo>
#include <signal.h>
#include <msp/debug/demangle.h>
#include <msp/debug/errorreporter.h>
+#include <msp/fs/dir.h>
+#include <msp/fs/path.h>
#include <msp/fs/utils.h>
#include <msp/io/print.h>
+#include <msp/strings/utils.h>
#include "application.h"
+#include "except.h"
#include "getopt.h"
using namespace std;
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::_app = nullptr;
+Application::Starter *Application::_starter = nullptr;
+const char *Application::_argv0 = nullptr;
+string Application::_name;
+void *Application::_data = nullptr;
-Application::Application(const string &n):
- exit_code(0)
+Application::Application(const string &n)
{
- if(app_)
- throw logic_error("instance already exists");
+ if(_app)
+ throw already_called("Application::Application");
if(!n.empty())
- name_ = n;
+ _name = n;
else
- name_ = FS::basename(argv0_);
+ _name = FS::basename(_argv0);
}
int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *))
{
- if(!starter_)
+ if(!_starter)
{
IO::cerr.write("Application::run called with no RegisteredApplication class!\n");
return 126;
}
- argv0_ = argv[0];
- data_ = data;
+ set_startup_info(argv[0], data);
try
{
try
{
- app_ = starter_->create_app(argc, argv);
+ _app = _starter->create_app(argc, argv);
}
catch(const usage_error &e)
{
if(created_callback)
created_callback(data);
- int result = app_->main();
- Application *a = app_;
- app_ = 0;
+ int result = _app->main();
+ Application *a = _app;
+ _app = nullptr;
delete a;
return result;
}
{
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());
+ vector<string> 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);
+ }
}
- delete app_;
- app_ = 0;
+ delete _app;
+ _app = nullptr;
return 124;
}
}
+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;
+}
+
int Application::main()
{
done = false;
void Application::catch_signal(int s)
{
- signal(s, &sighandler_);
+ signal(s, &_sighandler);
}
void Application::exit(int c)
exit_code = c;
}
-void Application::sighandler_(int s)
+void Application::_sighandler(int s)
{
- app_->sighandler(s);
+ _app->sighandler(s);
}
Application::Starter::Starter()
{
- if(starter_)
- throw logic_error("Can't create more than one Starter instance");
+ if(_starter)
+ throw already_called("Application::Starter::Starter");
- starter_ = this;
+ _starter = this;
}
} // namespace Msp