#include "GameException.h"

#include "SDL.h"

std::vector<GameException> GameException::m_svLog;
unsigned int GameException::m_sTotalErrors = 0;

GameException::GameException(const eErrorSeverity &severity, const std::string &str)
: m_str(str), m_severity(severity)
{
	m_time = SDL_GetTicks();
	m_sTotalErrors++;
	m_svLog.push_back(*this);
}

GameException::GameException(const eErrorSeverity &severity, const char *str, ...)
: m_severity(severity)
{
	char* pBuf = 0;

	va_list args;
	va_start(args,str);

#ifdef _WIN32
	size_t len = _vscprintf( str, args ) + 1; 
	pBuf = new char[len];
	vsprintf_s( pBuf, len, str, args );
#else
	pBuf = new char[2048];
	vsprintf(pBuf,str,args);
#endif


	m_str = std::string(pBuf);
	delete[] pBuf;

	m_time = SDL_GetTicks();
	m_sTotalErrors++;
	m_svLog.push_back(*this);
}

void GameException::Output(std::ostream &stream)
{
	stream << "Description : " << GetDescription() << std::endl;
	stream << "Severity	   : " << GetSeverity() << std::endl;
	stream << "Time        : " << GetTime() << std::endl;
}

void GameException::OutputLog(std::ostream &stream)
{
	stream << "Exception Log :" << std::endl << std::endl;

	for(size_t i = 0; i < m_svLog.size(); i++)
	{
		stream << std::endl;
		stream << "Error No.   : " << i << std::endl;
		stream << "Description : " << m_svLog[i].GetDescription() << std::endl;
		stream << "Severity	   : " << m_svLog[i].GetSeverity() << std::endl;
		stream << "Time        : " << m_svLog[i].GetTime() << std::endl;
	}
}

std::string GameException::ConvertSeverityToString(const eErrorSeverity &severity)
{
	switch(severity)
	{
		case eMinorError:
			return std::string("Minor");
		case eMajorError:
			return std::string("Major");
		case eFatalError:
			return std::string("Fatal");
	}

	return std::string("Unrecognised Severity");
}
