#include "CException.hpp"

void CException::makeFullMessage(void) {
    std::stringstream sstring;
    sstring << "Error : " << m_code << std::endl << m_message << std::endl;
    boost::stacktrace::stacktrace stacktrace = boost::stacktrace::stacktrace();
    std::size_t frames = stacktrace.size();

    for (size_t i = 4; i < frames; i++) {
        sstring << "    #" << i - 4 << " : " << stacktrace[i].name() << " : ";
        if (stacktrace[i].source_file().empty()) {
            sstring << "no source";
        }
        else {
            sstring << stacktrace[i].source_file() << " at line : " << stacktrace[i].source_line();
        }
        sstring << " (" << stacktrace[i].address() << ')' << std::endl;
    }

    m_fullMessage = sstring.str();
}

CException::CException(void) noexcept :
    std::exception(),
    m_message("A error occurred"),
    m_code(0),
    m_fullMessage() {
    makeFullMessage();
}

CException::CException(std::string message, unsigned int code) noexcept :
    std::exception(),
    m_message(message),
    m_code(code),
    m_fullMessage() {
    makeFullMessage();
}

CException::~CException(void) noexcept {
}

const char* CException::what() const noexcept {
    return m_fullMessage.c_str();
}

unsigned int CException::getCode() const noexcept {
    return m_code;
}

std::string CException::getMessage() const noexcept {
    return m_message;
}

void CException::setMessage(std::string newMessage) noexcept {
    m_message = newMessage;
}