/*=============================================================================
log.cpp: Basic OpenGL Framework logging function definitions
Revision history:
* Created by Zack Smith
* Inspired by code written by NeHe (http://nehe.gamedev.net/)
* Updated October 10, 2000
=============================================================================*/
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include "log.h"
CLog::CLog(const char *szPath, const char* szMode)
{
// if nothing was put use the default
if(szPath == NULL)
{
CreateDirectory("logs", NULL);
m_szPath = "logs/log.txt";
}
else
m_szPath = szPath;
m_fLog = fopen(m_szPath.c_str(), "w+"); // destroy anything existing
if(m_fLog)
{
fprintf(m_fLog, "Logging System Initialized \n");
fclose(m_fLog);
} else
MessageBox(NULL,"Error Starting Logging System","ERROR",MB_OK);
m_fLog = '\0';
}
CLog::~CLog()
{
Log("\nShutting Down Logging System");
Close();
OutputDebugString("CLog destructor\n");
}
void CLog::Open()
{
// if it isnt already opened, open the file for adding on to
if(!m_fLog)
{
m_fLog = fopen(m_szPath.c_str(),"a");
}
}
void CLog::Close()
{
// if the file is open, close it
if(m_fLog)
{
fclose(m_fLog);
m_fLog = '\0';
}
}
void CLog::Log(char *szText, ...)
{
// open the file
Open();
if(!m_fLog) // error somewhere
return;
va_list arglist;
char Text[1024]; // more than 1024 causes a buffer overrun
memset(Text, '\0', sizeof(Text)); // clear out the memory
va_start(arglist,szText);
vsprintf(Text,szText,arglist);
va_end(arglist);
fprintf(m_fLog,"\n%s",Text); // copy to the file
Close(); // and close the file
}