2012-01-29 11:28:19 -08:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
#include "cLog.h"
|
2012-01-29 11:28:19 -08:00
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
#include <fstream>
|
|
|
|
#include <ctime>
|
|
|
|
#include <stdarg.h>
|
2012-02-01 05:43:47 -08:00
|
|
|
#include "cMakeDir.h"
|
2011-10-03 11:41:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-29 11:28:19 -08:00
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
|
|
|
|
cLog* cLog::s_Log = NULL;
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
cLog::cLog(const AString & a_FileName )
|
2011-10-03 11:41:19 -07:00
|
|
|
: m_File(NULL)
|
|
|
|
{
|
|
|
|
s_Log = this;
|
|
|
|
|
|
|
|
// create logs directory
|
2012-02-01 05:43:47 -08:00
|
|
|
cMakeDir::MakeDir("logs");
|
2011-10-03 11:41:19 -07:00
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
OpenLog( (std::string("logs/") + a_FileName).c_str() );
|
2011-10-03 11:41:19 -07:00
|
|
|
}
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
cLog::~cLog()
|
|
|
|
{
|
|
|
|
CloseLog();
|
|
|
|
s_Log = NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
cLog* cLog::GetInstance()
|
|
|
|
{
|
|
|
|
if(s_Log)
|
|
|
|
return s_Log;
|
|
|
|
|
|
|
|
new cLog("log.txt");
|
|
|
|
return s_Log;
|
|
|
|
}
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
void cLog::CloseLog()
|
|
|
|
{
|
|
|
|
if( m_File )
|
|
|
|
fclose (m_File);
|
|
|
|
m_File = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
void cLog::OpenLog( const char* a_FileName )
|
|
|
|
{
|
|
|
|
if(m_File) fclose (m_File);
|
|
|
|
#ifdef _WIN32
|
|
|
|
fopen_s( &m_File, a_FileName, "a+" );
|
|
|
|
#else
|
2012-02-01 06:08:12 -08:00
|
|
|
m_File = fopen(a_FileName, "a+" );
|
2011-10-03 11:41:19 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
void cLog::ClearLog()
|
|
|
|
{
|
2012-02-01 05:43:47 -08:00
|
|
|
#ifdef _WIN32
|
2011-10-03 11:41:19 -07:00
|
|
|
if( fopen_s( &m_File, "log.txt", "w" ) == 0)
|
|
|
|
fclose (m_File);
|
|
|
|
#else
|
|
|
|
m_File = fopen("log.txt", "w" );
|
|
|
|
if( m_File )
|
|
|
|
fclose (m_File);
|
|
|
|
#endif
|
|
|
|
m_File = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 06:08:12 -08:00
|
|
|
void cLog::Log(const char * a_Format, va_list argList)
|
2012-02-01 05:43:47 -08:00
|
|
|
{
|
|
|
|
AString Message;
|
2012-02-01 06:08:12 -08:00
|
|
|
if (argList != NULL)
|
|
|
|
{
|
|
|
|
AppendVPrintf(Message, a_Format, argList);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This branch needs to be here because of *nix crashing in vsnprintf() when argList is NULL
|
|
|
|
Message.assign(a_Format);
|
|
|
|
}
|
2011-10-03 11:41:19 -07:00
|
|
|
|
|
|
|
time_t rawtime;
|
|
|
|
time ( &rawtime );
|
2012-02-01 05:43:47 -08:00
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
struct tm* timeinfo;
|
|
|
|
#ifdef _WIN32
|
2012-02-01 05:43:47 -08:00
|
|
|
struct tm timeinforeal;
|
|
|
|
timeinfo = &timeinforeal;
|
|
|
|
localtime_s(timeinfo, &rawtime );
|
2011-10-03 11:41:19 -07:00
|
|
|
#else
|
2012-02-01 05:43:47 -08:00
|
|
|
timeinfo = localtime( &rawtime );
|
2011-10-03 11:41:19 -07:00
|
|
|
#endif
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
AString Line;
|
|
|
|
Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str());
|
|
|
|
if (m_File)
|
|
|
|
{
|
|
|
|
fputs(Line.c_str(), m_File);
|
2011-10-03 11:41:19 -07:00
|
|
|
fflush(m_File);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
printf("%s", Line.c_str());
|
2011-10-03 11:41:19 -07:00
|
|
|
}
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
void cLog::Log(const char* a_Format, ...)
|
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
Log( a_Format, argList );
|
|
|
|
va_end(argList);
|
|
|
|
}
|
|
|
|
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 11:41:19 -07:00
|
|
|
void cLog::SimpleLog(const char* a_String)
|
|
|
|
{
|
|
|
|
Log("%s", a_String );
|
|
|
|
}
|
2012-02-01 05:43:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|