#include "SLog.h" #include #include #include FILE* debugFP = NULL; #ifdef _DEV #include #include #include #include #include TCHAR szLogFileName[ MAX_PATH ]; char *GetLOGFILE_NAME() { SYSTEMTIME stSystemTime; GetLocalTime( &stSystemTime ); ZeroMemory( szLogFileName, sizeof(szLogFileName) ); _tcscpy( szLogFileName, "rappelz " ); wsprintf( szLogFileName + strlen( szLogFileName ), " %04d-%02d-%02d %02d-%02d-%02d.log", stSystemTime.wYear, stSystemTime.wMonth, stSystemTime.wDay, stSystemTime.wHour, stSystemTime.wMinute, stSystemTime.wSecond ); return szLogFileName; } #endif SLog::SLog() { } SLog::~SLog() { m_LogList.clear(); m_LogLevelList.clear(); if( debugFP != NULL ) { fclose( debugFP ); debugFP = NULL; } } std::string SLog::GetTimeStamp() { struct tm *newtime; char time[128]; __time64_t long_time; memset( time, 0, sizeof(time) ); _time64( &long_time ); /* Get time as long integer. */ newtime = _localtime64( &long_time ); /* Convert to local time. */ std::string ret; XStringUtil::Format(ret, "[%.2d:%.2d:%.2d] ", newtime->tm_hour, newtime->tm_min, newtime->tm_sec); return ret; } #ifdef _DEV_SOCKET extern void openSocketStream(); extern void closeSocketStream(); #endif #ifdef _DEV_MEMORY_LOG extern void openSocketStream(); extern void closeSocketStream(); #endif void SLog::LogStart() { /// 2012.01.27 log파일을 저장할 log 폴더 생성 - prodongi DWORD retAttri = GetFileAttributes("log"); if (FILE_ATTRIBUTE_DIRECTORY != retAttri) { CreateDirectory("log", NULL); } struct tm *newtime; char time[128]; __time64_t long_time; memset( time, 0, sizeof(time) ); _time64( &long_time ); /* Get time as long integer. */ newtime = _localtime64( &long_time ); /* Convert to local time. */ Log("===================== Log Start! ===================="); Log("Current Time : %s", asctime(newtime)); #ifdef _DEV Log("Dev Mode"); std::string logFileName = "log/"; logFileName += LOGFILE_NAME; if( debugFP != NULL ) { fclose( debugFP ); debugFP = NULL; } debugFP = fopen(logFileName.c_str(), "w"); //debugFP = fopen(LOGFILE_NAME, "w"); #endif #ifdef _DEV_SOCKET openSocketStream(); #endif #ifdef _DEV_MEMORY_LOG openSocketStream(); #endif } void SLog::Log( const char * pLogStr, ... ) { va_list args; char buf[2048] = {0, }; va_start(args, pLogStr); vsprintf_s(buf, 2048, pLogStr, args); va_end(args); m_LogList.push_back(GetTimeStamp() + buf); m_LogLevelList.push_back(LOGLEVEL_SYSTEM); #ifdef _DEV if (debugFP != NULL) { fprintf(debugFP, "%s\n", m_LogList.back().c_str()); fflush(debugFP); } #endif } void SLog::DebugLog( const char * pLogStr, ... ) { va_list args; char buf[2048] = {0, }; va_start(args, pLogStr); vsprintf_s(buf, 2048, pLogStr, args); va_end(args); m_LogList.push_back(GetTimeStamp() + buf); m_LogLevelList.push_back(LOGLEVEL_DEBUG); #ifdef _DEV OutputDebugString(buf); OutputDebugString("\n"); if (debugFP != NULL) { fprintf(debugFP, "%s\n", m_LogList.back().c_str()); fflush(debugFP); } #endif } void SLog::OutputFile(const char* fileName) { #ifdef _DEV if (debugFP != NULL) { fclose(debugFP); debugFP = NULL; } #else FILE* fp = fopen(fileName, "w"); if (fp != NULL) { for (size_t i=0; i= m_LogLevelList[i]) { fprintf(fp, "%s\n", m_LogList[i].c_str()); } } fclose(fp); } #endif #ifdef _DEV_SOCKET closeSocketStream(); #endif #ifdef _DEV_MEMORY_LOG closeSocketStream(); #endif } SLog & GetLog() { static SLog slog; return slog; }