#include "pch.h"
|
#include "Log.h"
|
|
|
static const char* pszLevel[] = {" [Debug] ", " [Info] ", " [Warn] ", " [Error] "};
|
|
CLog::CLog()
|
{
|
m_nLevel = 0;
|
m_nOutputTarget = OT_TRACE;
|
m_bAutoAppendTime = TRUE;
|
m_strEquipmentId = _T("Unknown");
|
m_nDay = 0;
|
m_funOnLog = nullptr;
|
InitializeCriticalSection(&m_criticalSection);
|
}
|
|
|
CLog::~CLog()
|
{
|
DeleteCriticalSection(&m_criticalSection);
|
}
|
|
CLog *CLog::GetLog(void)
|
{
|
static CLog* pLog = NULL;
|
if (pLog == NULL) {
|
static CLog log;
|
pLog = &log;
|
}
|
|
return pLog;
|
}
|
|
void CLog::SetOnLogCallback(ONLOG funOnLog)
|
{
|
m_funOnLog = funOnLog;
|
}
|
|
void CLog::SetOutputTarget(int flag)
|
{
|
m_nOutputTarget = flag;
|
}
|
|
void CLog::SetEquipmentId(const char* pszEquipmentId)
|
{
|
m_strEquipmentId = pszEquipmentId;
|
}
|
|
void CLog::SetAutoAppendTimeString(BOOL bAutoAppendTime)
|
{
|
m_bAutoAppendTime = bAutoAppendTime;
|
}
|
|
void CLog::Batch()
|
{
|
if (m_file.m_hFile != CFile::hFileNull) {
|
m_file.Close();
|
}
|
}
|
|
BOOL CLog::BatchAndNew(int& nDay)
|
{
|
Batch();
|
if ( (m_nOutputTarget & OT_FILE) && m_file.m_hFile == CFile::hFileNull) {
|
CString strFilepath;
|
BOOL bRet = m_file.Open(MakeFilepathD(strFilepath, nDay), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::shareDenyWrite);
|
if (bRet) {
|
m_file.SeekToEnd();
|
}
|
return bRet;
|
}
|
|
return FALSE;
|
}
|
|
#define BUFFERSIZE 1024*10
|
void CLog::LogFormat(int nLevel, const char* pszTag, char* szMessage, ...)
|
{
|
// ¼ì²éÈÕÆÚÊÇ·ñÓб仯£¬Óб仯Ôò½áÅú
|
Lock();
|
_SYSTEMTIME sysTime;
|
GetLocalTime(&sysTime);
|
if(m_nDay != sysTime.wDay) {
|
int nDay = 0;
|
if (BatchAndNew(nDay)) {
|
m_nDay = nDay;
|
}
|
}
|
Unlock();
|
|
if (nLevel < m_nLevel) {
|
return;
|
}
|
|
char szFullMessage[BUFFERSIZE];
|
char szFormatMessage[BUFFERSIZE];
|
|
// format message
|
va_list ap;
|
va_start(ap, szMessage);
|
_vsnprintf_s(szFormatMessage, BUFFERSIZE, szMessage, ap);
|
va_end(ap);
|
|
if (m_bAutoAppendTime) {
|
CString strTime;
|
strcpy_s(szFullMessage, BUFFERSIZE, (LPTSTR)(LPCTSTR)GetCurTime(strTime));
|
}
|
strcat_s(szFullMessage, BUFFERSIZE, pszLevel[nLevel]);
|
strcat_s(szFullMessage, szFormatMessage);
|
strcat_s(szFullMessage, BUFFERSIZE, "\n");
|
|
if (m_nOutputTarget & OT_FILE) {
|
Lock();
|
if (m_file.m_hFile != CFile::hFileNull) {
|
m_file.WriteString(szFullMessage);
|
}
|
Unlock();
|
}
|
if (m_nOutputTarget & OT_ODSTRING) {
|
OutputDebugStringA(szFullMessage);
|
}
|
else if(m_nOutputTarget & OT_TRACE) {
|
TRACE(szFormatMessage);
|
}
|
|
if (m_funOnLog != nullptr) {
|
m_funOnLog(nLevel, szFullMessage);
|
}
|
}
|
|
void CLog::Log(int nLevel, const char* pszTag, const char* szMessage)
|
{
|
// ¼ì²éÈÕÆÚÊÇ·ñÓб仯£¬Óб仯Ôò½áÅú
|
Lock();
|
_SYSTEMTIME sysTime;
|
GetLocalTime(&sysTime);
|
if (m_nDay != sysTime.wDay) {
|
int nDay = 0;
|
if (BatchAndNew(nDay)) {
|
m_nDay = nDay;
|
}
|
}
|
Unlock();
|
|
if (nLevel < m_nLevel) {
|
return;
|
}
|
|
CString strMsg;
|
if (m_bAutoAppendTime) {
|
CString strTime;
|
GetCurTime(strTime);
|
strMsg.Append(strTime);
|
}
|
strMsg.Append(pszTag);
|
strMsg.Append(szMessage);
|
strMsg.Append("\n");
|
|
if (m_nOutputTarget & OT_FILE) {
|
Lock();
|
if (m_file.m_hFile != CFile::hFileNull) {
|
m_file.WriteString(strMsg);
|
}
|
Unlock();
|
}
|
if (m_nOutputTarget & OT_ODSTRING) {
|
OutputDebugStringA(strMsg);
|
}
|
else if (m_nOutputTarget & OT_TRACE) {
|
TRACE(strMsg);
|
}
|
|
if (m_funOnLog != nullptr) {
|
m_funOnLog(nLevel, strMsg);
|
}
|
}
|
|
CString& CLog::GetCurTime(CString& strTime)
|
{
|
_SYSTEMTIME sysTime;
|
GetLocalTime(&sysTime);
|
strTime.Format(_T("%d/%02d/%02d %02d:%02d:%02d.%03d"), sysTime.wYear, sysTime.wMonth, sysTime.wDay,
|
sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
|
return strTime;
|
}
|
|
CString& CLog::MakeFilepath(CString& strFilepath)
|
{
|
_SYSTEMTIME sysTime;
|
GetLocalTime(&sysTime);
|
strFilepath.Format(_T("%s\\Log(%s)_%d_%02d_%02d.log"), (LPTSTR)(LPCTSTR)m_strLogsDir,
|
(LPTSTR)(LPCTSTR)m_strEquipmentId,
|
sysTime.wYear, sysTime.wMonth, sysTime.wDay);
|
|
return strFilepath;
|
}
|
|
CString& CLog::MakeFilepathD(CString& strFilepath, int& day)
|
{
|
_SYSTEMTIME sysTime;
|
GetLocalTime(&sysTime);
|
strFilepath.Format(_T("%s\\Log(%s)_%d_%02d_%02d.log"), (LPTSTR)(LPCTSTR)m_strLogsDir,
|
(LPTSTR)(LPCTSTR)m_strEquipmentId,
|
sysTime.wYear, sysTime.wMonth, sysTime.wDay);
|
day = sysTime.wDay;
|
|
return strFilepath;
|
}
|
|
void CLog::SetLogsDir(CString strDir)
|
{
|
m_strLogsDir = strDir;
|
}
|