
#include <WFX/lang/WFXException.h>

using namespace WFX;
using namespace WFX::lang;
using namespace std;

WFXException::WFXException(const Char* text, const Char* file, const Int line, const Char* func) {
    CallStack = new std::list<CallEntry>;
    
    init(text, file, line, func);
}

WFXException::WFXException(const WFXException& ex, const Char* text, const Char* file, const Int line, const Char* func) {
    CallStack = ex.CallStack;    

    init(text, file, line, func);
}

WFXException::WFXException(const WFXException& ex) {
    CallStack = ex.CallStack;
}

void WFXException::addCallEntry(const std::string& text, const std::string& file, Int line, const std::string func) {
    CallEntry ce;

    ce.Text = text;
    ce.File = file;
    ce.Line = line;
    ce.Function = func;

    CallStack->push_front(ce);
}

void WFXException::init(const Char* text, const Char* file, const Int line, const Char* func) {
    string txt(text);
    string f(file);
    string fct(func);
    addCallEntry(txt, f, line, fct);
}

void WFXException::printStackTrace() {
    cerr << "An Exception occured: " << std::endl;
    
    UInt c = 0;
    list<CallEntry>::iterator it;
    for (it=CallStack->begin(); it!=CallStack->end(); it++) {
        cerr << "[" << c << "] \"" << it->Text << "\"" << std::endl;
        cerr << " ->  in File     : "  << it->File << std::endl;
        cerr << " ->  on Line     : "  << it->Line << std::endl;
        cerr << " ->  in Function : "  << it->Function << std::endl;
        c++;
    }
}

