#include "Gos.pch"
#include "Debug.h"
#include <stdio.h>

int (*GosDebug::HandleError)( int, TCHAR * ) = Debug::HandleError;
bool *(*GosDebug::Cat)( _TCHAR *pCategoryName ) = Debug::Cat;
void (*GosDebug::EnableCat)( bool *pCat ) = Debug::EnableCat;
void (*GosDebug::DisableCat)( bool *pCat ) = Debug::DisableCat;
void (*GosDebug::Output)( _TCHAR *pFmt,... ) = Debug::Output;
void (*GosDebug::OutputV)( _TCHAR *pFmt, va_list arglist ) = Debug::OutputV;


DWORD GosDebug::s_ErrorLine;
TCHAR *GosDebug::s_pErrorFileName;

struct SCat
{
	bool	        enabled;
	SCat	        *pNext;
	_TCHAR	        *pName;
};

static SCat *pFirstCat = NULL;


int Debug::HandleError( int Flags, TCHAR *Text )
{
	_asm int 3;
	return 0;
}


bool *Debug::Cat( _TCHAR *pCategoryName )
{
	SCat *pCat;
	for( pCat = pFirstCat; pCat; pCat = pCat->pNext )
	{
		if( _tcscmp( pCategoryName, pCat->pName ) == 0 )
			return (bool *)pCat;
	}
	pCat = (SCat *)malloc( sizeof(SCat) );
	pCat->pNext = pFirstCat;
	pCat->pName = pCategoryName;
	pCat->enabled = false;
	pFirstCat = pCat;
	return (bool *)pCat;
}

void Debug::EnableCat( bool *pCat )
{
	*pCat = true;
}

void Debug::DisableCat( bool *pCat )
{
	*pCat = false;
}


void Debug::Output( _TCHAR *pFmt,... )
{
	static _TCHAR msg[200];
	va_list arglist;
	va_start( arglist, pFmt );
	_vsntprintf( msg, 200, pFmt, arglist );
	va_end( arglist );

	OutputDebugString( msg );
}

void Debug::OutputV( _TCHAR *pFmt, va_list arglist )
{
	static _TCHAR msg[200];
	_vsntprintf( msg, 200, pFmt, arglist );

	OutputDebugString( msg );
}