plugy/PlugY/Error.cpp

113 lines
2.3 KiB
C++
Raw Permalink Normal View History

/*=================================================================
File created by Yohann NICOLAS.
2021-01-04 07:07:29 +00:00
Error Logger implementation.
=================================================================*/
2017-01-23 19:34:12 +00:00
#include "parameters.h"
#include "error.h"
#include <windows.h>
#include <stdio.h> // fopen() fclose() fprintf() vfprintf()
#include <stdarg.h> // ...
#include <string.h> // strlen() strcat()
#include <direct.h> // _getcwd()
2021-01-04 07:07:29 +00:00
int active_logFile = 1;
static char log_file[MAX_PATH] = "";
static bool log_init = false;
2021-01-04 07:07:29 +00:00
static FILE* fLogFile = NULL;
void log_initfile()
{
if (log_init) return;
2021-01-04 07:07:29 +00:00
_getcwd( log_file, MAX_PATH );
if( log_file[strlen(log_file)] != '\\')
strcat(log_file, "\\");
2017-06-19 13:01:03 +00:00
strcat(log_file, "PlugY.log");
2021-01-04 07:07:29 +00:00
fLogFile = fopen( log_file, "w" );
if( fLogFile == NULL )
return;
log_init = true;
log_msg("<----------------------------------------------->\n"
"\n"
"\t\tA Plugin by Yohann\n"
"\t\tversion %s\n"
"\n"
"\n"
"<---------- Starting Diablo II Plugin ---------->\n\n\n"
,PLUGY_VERSION);
}
2021-01-04 07:07:29 +00:00
void log_close()
{
if (fLogFile)
{
fclose(fLogFile);
fLogFile = NULL;
}
}
void log_box( const char* pFormat, ... )
{
char buffer[300];
va_list lArgs;
va_start( lArgs, pFormat );
vsprintf( buffer, pFormat, lArgs );
va_end(lArgs);
log_msg(buffer);
MessageBox(NULL, buffer, "PlugY, The Survival Kit", MB_OK|MB_ICONEXCLAMATION);
}
void log_msg( const char* pFormat, ... )
{
2021-01-04 07:07:29 +00:00
if (!active_logFile)
return;
if( !log_init )
log_initfile();
if( log_init )
{
va_list lArgs;
va_start( lArgs, pFormat );
2021-01-04 07:07:29 +00:00
if ( fLogFile == NULL )
fLogFile = fopen( log_file, "a" );
2021-01-04 07:07:29 +00:00
if( fLogFile != NULL )
{
2021-01-04 07:07:29 +00:00
vfprintf( fLogFile, pFormat, lArgs );
fflush(fLogFile);
}
else
log_init = false;
va_end(lArgs);
}
}
void d2_assert( bool pCondition, char* pMessage, char* pLocation, int pLineNbr )
{
if( pCondition )
{
log_msg("\n"
"*-----------------------*\n"
"Assertion fail at line %d of %s :\n"
"%s\n"
"*-----------------------*\n",
pLineNbr, pLocation, pMessage);
MessageBox(NULL, pMessage, "Diablo 2 Error", MB_OK|MB_ICONEXCLAMATION);
exit(1);
}
}
/*================================= END OF FILE =================================*/