From d440c3f50a918a932293ad98bcec96eaa4683222 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sun, 4 Dec 2011 01:19:09 -0800 Subject: Reworked some things in the architecture, mainly exceptions and asserts. -) Removed Assert() -) Added AAssert(), IAssert(), RAssert(), TAssert() and Failure() -) Reworked all asserts in the code, and added meaningful messages to them. -) Changed the way the startup code is generated; BALAU_STARTUP is no longer necessary. --- src/Main.cc | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'src/Main.cc') diff --git a/src/Main.cc b/src/Main.cc index eb5e589..d42a1a5 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -1,4 +1,7 @@ #include "Main.h" +#include "TaskMan.h" +#include "Printer.h" +#include "AtStartExit.h" Balau::AtStart * Balau::AtStart::s_head = 0; Balau::AtExit * Balau::AtExit::s_head = 0; @@ -6,6 +9,7 @@ Balau::AtExit * Balau::AtExit::s_head = 0; Balau::AtStart::AtStart(int priority) : m_priority(priority) { if (priority < 0) return; + AAssert(!Main::hasMain(), "An AtStart can't be created dynamically"); AtStart ** ptr = &s_head; @@ -20,6 +24,7 @@ Balau::AtStart::AtStart(int priority) : m_priority(priority) { Balau::AtExit::AtExit(int priority) : m_priority(priority) { if (priority < 0) return; + AAssert(!Main::hasMain(), "An AtExit can't be created dynamically"); AtExit ** ptr = &s_head; @@ -32,3 +37,83 @@ Balau::AtExit::AtExit(int priority) : m_priority(priority) { } Balau::Main * Balau::Main::s_application = NULL; + +Balau::MainTask::~MainTask() { + if (m_stopTaskManOnExit) + TaskMan::stop(0); +} + +const char * Balau::MainTask::getName() { + return "Main Task"; +} + +int Balau::Main::bootstrap(int _argc, char ** _argv) { + int r = 0; + m_status = STARTING; + + argc = _argc; + argv = _argv; + enve = NULL; + + for (AtStart * ptr = AtStart::s_head; ptr; ptr = ptr->m_next) + ptr->doStart(); + + try { + m_status = RUNNING; + MainTask * mainTask = createTask(new MainTask()); + r = TaskMan::getDefaultTaskMan()->mainLoop(); + m_status = STOPPING; + } + catch (Exit e) { + m_status = STOPPING; + Printer::log(M_ERROR, "We shouldn't have gotten an Exit exception here... exitting anyway"); + std::vector trace = e.getTrace(); + for (std::vector::iterator i = trace.begin(); i != trace.end(); i++) + Printer::log(M_ERROR, "%s", i->to_charp()); + r = e.getCode(); + } + catch (RessourceException e) { + m_status = STOPPING; + Printer::log(M_ERROR | M_ALERT, "The application got a ressource problem: %s", e.getMsg()); + const char * details = e.getDetails(); + if (details) + Printer::log(M_ERROR, " %s", details); + std::vector trace = e.getTrace(); + for (std::vector::iterator i = trace.begin(); i != trace.end(); i++) + Printer::log(M_DEBUG, "%s", i->to_charp()); + r = -1; + } + catch (GeneralException e) { + m_status = STOPPING; + Printer::log(M_ERROR | M_ALERT, "The application caused an exception: %s", e.getMsg()); + const char * details = e.getDetails(); + if (details) + Printer::log(M_ERROR, " %s", details); + std::vector trace = e.getTrace(); + for (std::vector::iterator i = trace.begin(); i != trace.end(); i++) + Printer::log(M_DEBUG, "%s", i->to_charp()); + r = -1; + } + catch (...) { + m_status = STOPPING; + Printer::log(M_ERROR | M_ALERT, "The application caused an unknown exception"); + r = -1; + } + m_status = STOPPING; + + for (AtExit * ptr = AtExit::s_head; ptr; ptr = ptr->m_next) + ptr->doExit(); + + m_status = STOPPED; + return r; +} + +extern "C" { + +int main(int argc, char ** argv) { + setlocale(LC_ALL, ""); + Balau::Main mainClass; + return mainClass.bootstrap(argc, argv); +} + +}; -- cgit v1.2.3