CubeWorldMods/LocalizationMod/logger.cpp

271 lines
6.4 KiB
C++
Raw Normal View History

2020-12-23 10:03:54 +08:00
//logger.cpp
#include "logger.h"
#include <time.h>
#include <stdarg.h>
#include <direct.h>
#include <vector>
using std::string;
using std::vector;
namespace LOGGER
{
CLogger::CLogger(EnumLogLevel nLogLevel, const std::string strLogPath, const std::string strLogName)
:m_nLogLevel(nLogLevel),
m_strLogPath(strLogPath),
m_strLogName(strLogName)
{
//<2F><>ʼ<EFBFBD><CABC>
m_pFileStream = NULL;
if (m_strLogPath.empty())
{
m_strLogPath = GetAppPathA();
}
if (m_strLogPath[m_strLogPath.length()-1] != '\\')
{
m_strLogPath.append("\\");
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
//MakeSureDirectoryPathExists(m_strLogPath.c_str());
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD>
if (m_strLogName.empty())
{
time_t curTime;
time(&curTime);
tm tm1;
localtime_s(&tm1, &curTime);
//<2F><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>磺201601012130.log
m_strLogName = FormatString("%04d%02d%02d_%02d%02d%02d.log", tm1.tm_year + 1900, tm1.tm_mon + 1, tm1.tm_mday, tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
}
m_strLogFilePath = m_strLogPath.append(m_strLogName);
//<2F><>׷<EFBFBD>ӵķ<D3B5>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
fopen_s(&m_pFileStream, m_strLogFilePath.c_str(), "a+");
InitializeCriticalSection(&m_cs);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CLogger::~CLogger()
{
//<2F>ͷ<EFBFBD><CDB7>ٽ<EFBFBD><D9BD><EFBFBD>
DeleteCriticalSection(&m_cs);
//<2F>ر<EFBFBD><D8B1>ļ<EFBFBD><C4BC><EFBFBD>
if (m_pFileStream)
{
fclose(m_pFileStream);
m_pFileStream = NULL;
}
}
//<2F>ļ<EFBFBD>ȫ·<C8AB><C2B7><EFBFBD>õ<EFBFBD><C3B5>ļ<EFBFBD><C4BC><EFBFBD>
const char *CLogger::path_file(const char *path, char splitter)
{
return strrchr(path, splitter) ? strrchr(path, splitter) + 1 : path;
}
//д<><D0B4><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>Ϣ
void CLogger::TraceFatal(const char *lpcszFormat, ...)
{
//<2F>жϵ<D0B6>ǰ<EFBFBD><C7B0>д<EFBFBD><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
if (EnumLogLevel::LogLevel_Fatal > m_nLogLevel)
return;
string strResult;
if (NULL != lpcszFormat)
{
va_list marker = NULL;
va_start(marker, lpcszFormat); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //<2F><>ȡ<EFBFBD><C8A1>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::vector<char> vBuffer(nLength, '\0'); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ洢<DAB4><E6B4A2>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker);
if (nWritten > 0)
{
strResult = &vBuffer[0];
}
va_end(marker); //<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
if (strResult.empty())
{
return;
}
string strLog = strFatalPrefix;
strLog.append(GetTime()).append(strResult);
//д<><D0B4>־<EFBFBD>ļ<EFBFBD>
Trace(strLog);
}
//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
void CLogger::TraceError(const char *lpcszFormat, ...)
{
//<2F>жϵ<D0B6>ǰ<EFBFBD><C7B0>д<EFBFBD><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
if (EnumLogLevel::LogLevel_Error > m_nLogLevel)
return;
string strResult;
if (NULL != lpcszFormat)
{
va_list marker = NULL;
va_start(marker, lpcszFormat); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //<2F><>ȡ<EFBFBD><C8A1>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::vector<char> vBuffer(nLength, '\0'); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ洢<DAB4><E6B4A2>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker);
if (nWritten > 0)
{
strResult = &vBuffer[0];
}
va_end(marker); //<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
if (strResult.empty())
{
return;
}
string strLog = strErrorPrefix;
strLog.append(GetTime()).append(strResult);
//д<><D0B4>־<EFBFBD>ļ<EFBFBD>
Trace(strLog);
}
//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
void CLogger::TraceWarning(const char *lpcszFormat, ...)
{
//<2F>жϵ<D0B6>ǰ<EFBFBD><C7B0>д<EFBFBD><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
if (EnumLogLevel::LogLevel_Warning > m_nLogLevel)
return;
string strResult;
if (NULL != lpcszFormat)
{
va_list marker = NULL;
va_start(marker, lpcszFormat); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //<2F><>ȡ<EFBFBD><C8A1>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::vector<char> vBuffer(nLength, '\0'); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ洢<DAB4><E6B4A2>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker);
if (nWritten > 0)
{
strResult = &vBuffer[0];
}
va_end(marker); //<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
if (strResult.empty())
{
return;
}
string strLog = strWarningPrefix;
strLog.append(GetTime()).append(strResult);
//д<><D0B4>־<EFBFBD>ļ<EFBFBD>
Trace(strLog);
}
//дһ<D0B4><D2BB><EFBFBD><EFBFBD>Ϣ
void CLogger::TraceInfo(const char *lpcszFormat, ...)
{
//<2F>жϵ<D0B6>ǰ<EFBFBD><C7B0>д<EFBFBD><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
if (EnumLogLevel::LogLevel_Info > m_nLogLevel)
return;
string strResult;
if (NULL != lpcszFormat)
{
va_list marker = NULL;
va_start(marker, lpcszFormat); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //<2F><>ȡ<EFBFBD><C8A1>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::vector<char> vBuffer(nLength, '\0'); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ洢<DAB4><E6B4A2>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker);
if (nWritten > 0)
{
strResult = &vBuffer[0];
}
va_end(marker); //<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
if (strResult.empty())
{
return;
}
string strLog = strInfoPrefix;
strLog.append(GetTime()).append(strResult);
//д<><D0B4>־<EFBFBD>ļ<EFBFBD>
Trace(strLog);
}
//<2F><>ȡϵͳ<CFB5><CDB3>ǰʱ<C7B0><CAB1>
string CLogger::GetTime()
{
time_t curTime;
time(&curTime);
tm tm1;
localtime_s(&tm1, &curTime);
//2016-01-01 21:30:00
string strTime = FormatString("%04d-%02d-%02d %02d:%02d:%02d ", tm1.tm_year + 1900, tm1.tm_mon + 1, tm1.tm_mday, tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
return strTime;
}
//<2F>ı<EFBFBD>д<EFBFBD><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
void CLogger::ChangeLogLevel(EnumLogLevel nLevel)
{
m_nLogLevel = nLevel;
}
//д<>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
void CLogger::Trace(const string &strLog)
{
try
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD>
EnterCriticalSection(&m_cs);
//<2F><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>û<EFBFBD>д򿪣<D0B4><F2BFAAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD><C2B4><EFBFBD>
if (NULL == m_pFileStream)
{
fopen_s(&m_pFileStream, m_strLogFilePath.c_str(), "a+");
if (!m_pFileStream)
{
return;
}
}
//д<><D0B4>־<EFBFBD><D6BE>Ϣ<EFBFBD><CFA2><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
fprintf(m_pFileStream, "%s\n", strLog.c_str());
fflush(m_pFileStream);
//<2F><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD>
LeaveCriticalSection(&m_cs);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
catch (...)
{
LeaveCriticalSection(&m_cs);
}
}
string CLogger::GetAppPathA()
{
char szFilePath[MAX_PATH] = { 0 }, szDrive[MAX_PATH] = { 0 }, szDir[MAX_PATH] = { 0 }, szFileName[MAX_PATH] = { 0 }, szExt[MAX_PATH] = { 0 };
GetModuleFileNameA(NULL, szFilePath, sizeof(szFilePath));
_splitpath_s(szFilePath, szDrive, szDir, szFileName, szExt);
string str(szDrive);
str.append(szDir);
return str;
}
string CLogger::FormatString(const char *lpcszFormat, ...)
{
string strResult;
if (NULL != lpcszFormat)
{
va_list marker = NULL;
va_start(marker, lpcszFormat); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //<2F><>ȡ<EFBFBD><C8A1>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::vector<char> vBuffer(nLength, '\0'); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ洢<DAB4><E6B4A2>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker);
if (nWritten > 0)
{
strResult = &vBuffer[0];
}
va_end(marker); //<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
return strResult;
}
}