/*------------------------------------------------------------------------------
	FILE	:	Error.c
	CREATED	:	98/09/02
	AUTHOR	:	Catalin Cocos
	CONTENTS:	LDT Error management
------------------------------------------------------------------------------*/

#include "StdInc.h"
#include "Error.h"
#include "Parser.h"

char LDT_ErrDesc[LDT_MAX_ERROR_DESCRIPTION +1];
FILE* pErrFile = NULL;

/*------------------------------------------------------------------------------
	DESC.	:	initializes the error log file.
	CREATED	:	98/09/02        
	AUTHOR	:	Catalin Cocos
------------------------------------------------------------------------------*/
void InitErrFile()
{
	static int count = 0;
	pErrFile = fopen("LDT_ERR.log", count?"a+":"w");
	count++;
	if(pErrFile)
	{
		time_t strTime;
		struct tm *newtime;
		time( &strTime );
		newtime = localtime( &strTime );
		fprintf(pErrFile, "LDT error log session %u >>> %s \n",  count, asctime( newtime ));
	}
}
/*------------------------------------------------------------------------------
	DESC.	:	Closes the eror log file
	CREATED	:	98/09/02        
	AUTHOR	:	Catalin Cocos
------------------------------------------------------------------------------*/
void CLoseErrFile()
{
	if(pErrFile)
	{
		fprintf(pErrFile, "\n\n\n");
		fclose(pErrFile);
	}
	pErrFile = NULL;
}

/*------------------------------------------------------------------------------
	DESC.	:	Error treatment
	CREATED	:	98/09/02        
	AUTHOR	:	Catalin Cocos
------------------------------------------------------------------------------*/
void SignalError(int level, LDT_tdst_Link * pLink, int errorcode, char* szErrDesc, char* szSourceFile, int line)
{
	char* buff = LDT_ErrDesc;
	if(level > LDT_WARNING_LEVEL && level > LDT_ERROR_LEVEL) return;
	
	buff += sprintf( buff,"\n%s\t(Level %u)\n" ,level>LDT_ERROR_LEVEL?"WARNING":"ERROR", level);
	buff += sprintf( buff,"\nCODE :\t\t%i (%s)", errorcode, errorcode?"USER":"SYSTEM");
	if(szErrDesc)
		buff += sprintf( buff,"\nDESCRIPTION:\t%s\n", szErrDesc);
	buff += sprintf( buff,"LOCATION:\t");
	if(pLink)
	{
		buff += sprintf( buff,"(%s) ", (pLink->dwFlags & LDT_LF_FILE)?"FILE":"SECTION");
		LDT_ComputeSectionName( pLink, buff);
		buff+= strlen(buff);
	}
	else
		if( g_FileDesc )
			buff += sprintf( buff, "(FILE) %s", g_FileDesc->_pInfoMemFile->pName );
		else
			buff += sprintf( buff, "No information available" );

	buff += sprintf( buff,"\nSOURCE FILE:\t %s\nLINE:\t\t %u\n",szSourceFile, line);

#ifdef LDT_LOG_ERRORS
	if(pErrFile )
	{
		fprintf(pErrFile, LDT_ErrDesc );
		fprintf(pErrFile, "\n");
	}
#endif
#ifdef LDT_DISPLAY_ERRORS
#ifndef LDT_DISPLAY_WARNINGS
	if(level > LDT_ERROR_LEVEL) return;
#endif
	sprintf( buff,"\nPress 'Abort' to exit the application, 'Retry' to debug or 'Ignore' to continue.");
	switch(	MessageBox(NULL, LDT_ErrDesc, "LDT MESSAGE", MB_ABORTRETRYIGNORE |MB_TASKMODAL |MB_TOPMOST |
			(level > LDT_ERROR_LEVEL?MB_ICONWARNING|MB_DEFBUTTON3:MB_ICONERROR|MB_DEFBUTTON2)))
	{
	case IDABORT: exit(errorcode);	/* quit the application */
	case IDRETRY:					/* debug */
		    __asm
		    {
			int 3h
			};
	}
#endif
}

