From 2568bad3cc1ae70350f5ad31e97b4c13194e437e Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Wed, 1 Feb 2012 13:43:47 +0000 Subject: sprintf() begone! Replaced with StringUtils' Printf() git-svn-id: http://mc-server.googlecode.com/svn/trunk@216 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cLog.cpp | 95 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 39 deletions(-) (limited to 'source/cLog.cpp') diff --git a/source/cLog.cpp b/source/cLog.cpp index 8f24b24ad..b83d3cc37 100644 --- a/source/cLog.cpp +++ b/source/cLog.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "cMakeDir.h" @@ -13,35 +14,31 @@ cLog* cLog::s_Log = NULL; -cLog::cLog( const char* a_FileName ) +cLog::cLog(const AString & a_FileName ) : m_File(NULL) { s_Log = this; // create logs directory -#ifdef _WIN32 - { - SECURITY_ATTRIBUTES Attrib; - Attrib.nLength = sizeof(SECURITY_ATTRIBUTES); - Attrib.lpSecurityDescriptor = NULL; - Attrib.bInheritHandle = false; - ::CreateDirectory("logs", &Attrib); - } -#else - { - mkdir("logs", S_IRWXU | S_IRWXG | S_IRWXO); - } -#endif + cMakeDir::MakeDir("logs"); - OpenLog( (std::string("logs/") + std::string(a_FileName)).c_str() ); + OpenLog( (std::string("logs/") + a_FileName).c_str() ); } + + + + cLog::~cLog() { CloseLog(); s_Log = NULL; } + + + + cLog* cLog::GetInstance() { if(s_Log) @@ -51,6 +48,10 @@ cLog* cLog::GetInstance() return s_Log; } + + + + void cLog::CloseLog() { if( m_File ) @@ -58,6 +59,10 @@ void cLog::CloseLog() m_File = 0; } + + + + void cLog::OpenLog( const char* a_FileName ) { if(m_File) fclose (m_File); @@ -68,9 +73,13 @@ void cLog::OpenLog( const char* a_FileName ) #endif } + + + + void cLog::ClearLog() { - #ifdef _WIN32 + #ifdef _WIN32 if( fopen_s( &m_File, "log.txt", "w" ) == 0) fclose (m_File); #else @@ -81,43 +90,43 @@ void cLog::ClearLog() m_File = 0; } -void cLog::Log(const char* a_Format, va_list argList ) -{ - char c_Buffer[1024]; - if( argList != 0 ) - { - vsnprintf_s(c_Buffer, 1024, 1024, a_Format, argList ); - } - else - { - sprintf_s( c_Buffer, 1024, "%s", a_Format ); - } + + + +void cLog::Log(const char* a_Format, va_list argList) +{ + AString Message; + AppendVPrintf(Message, a_Format, argList); time_t rawtime; time ( &rawtime ); -#ifdef _WIN32 - struct tm timeinfo; - localtime_s( &timeinfo, &rawtime ); -#else + struct tm* timeinfo; - timeinfo = localtime( &rawtime ); -#endif - char c_Buffer2[1024]; #ifdef _WIN32 - sprintf_s(c_Buffer2, 1024, "[%02d:%02d:%02d] %s\n", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, c_Buffer); + struct tm timeinforeal; + timeinfo = &timeinforeal; + localtime_s(timeinfo, &rawtime ); #else - sprintf(c_Buffer2, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, c_Buffer); + timeinfo = localtime( &rawtime ); #endif - if(m_File){ - fputs(c_Buffer2, m_File); + + 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); fflush(m_File); } - printf("%s", c_Buffer2 ); + printf("%s", Line.c_str()); } + + + + void cLog::Log(const char* a_Format, ...) { va_list argList; @@ -126,7 +135,15 @@ void cLog::Log(const char* a_Format, ...) va_end(argList); } + + + + void cLog::SimpleLog(const char* a_String) { Log("%s", a_String ); } + + + + -- cgit v1.2.3