📄 logfile.h
字号:
#ifndef _ATA_LOGFILE_
#define _ATA_LOGFILE_
#include <afxmt.h>
/////////////////////////////////////////////////////////////////
//features: not depends on MFC ore ATL.
// file name could use absolute path or just the name, in which case the
// file will be created at the same place with the binary module, no concern
// with curret directory, which always bring me truble.
// every log line has a time stamp attached, with million seconds.
// uses printf like format to write log lines
// uses a preprocessor definition _DEBUG_LOG to switch if write log file
// multi thread safe, finally added:)
////////////////////////////////////////////////////
// CLogFile, a debug log file wrapper
class CLogFile
{
public:
//////////////////////////////
// Constructor, open the logfile
CLogFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 1024*1024*10)
{
m_lTruncate = lTruncate;
TCHAR szFile[MAX_PATH+1];
if (_tcslen(strFile)>3 && strFile[1]!=':') //no absolute path designated
{
::GetModuleFileName(NULL, szFile, MAX_PATH);
long llength = _tcslen(szFile);
TCHAR* pcat = szFile+(llength - 1); //point to the last char
while (llength--)
{
pcat--;
if (*pcat == '\\')
break;
}
if (*pcat == '\\')
{
pcat++;
_tcscpy(pcat, strFile);
}
else //something wrong, use the original filename, ignore path problem
_tcscpy(szFile, strFile);
}
else
_tcscpy(szFile, strFile);
m_pLogFile = fopen(szFile, bAppend ? "a" : "w");
setvbuf( m_pLogFile, "", _IONBF, 0);
}
/////////////////////////////////
// Destructor, close if logfile if opened
~CLogFile()
{
if (m_pLogFile)
{
fputs("\n===============Finish Loging Cmpp-Gateway================\n\n", m_pLogFile);
fclose(m_pLogFile);
}
}
/////////////////////////////////
// Write log info into the logfile, with printf like parameters support
void Write(LPCTSTR pszFormat, ...)
{
if (!m_pLogFile)
return;
CSingleLock singleLock(&m_cs);
try{
singleLock.Lock();
//write the formated log string to szLog
TCHAR szLog[1024*10];
va_list argList;
va_start( argList, pszFormat );
vsprintf( szLog, pszFormat, argList );
va_end( argList );
//Trancate if the file grow too large
long lLength = ftell(m_pLogFile);
if (lLength > m_lTruncate)
rewind(m_pLogFile);
//Get current time
SYSTEMTIME time;
::GetLocalTime(&time);
TCHAR szLine[1024*10];
sprintf(szLine, "%02d:%02d:%02d:%03d \t%s\n",
time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
szLog);
fputs(szLine, m_pLogFile);
singleLock.Unlock();
}
catch(...){
return;
}
if (singleLock.IsLocked()) singleLock.Unlock();
}
void WriteEx(LPCTSTR pszFormat, ...)
{
if (!m_pLogFile)
return;
CSingleLock singleLock(&m_cs);
try{
singleLock.Lock();
//write the formated log string to szLog
TCHAR szLog[1024*10];
va_list argList;
va_start( argList, pszFormat );
vsprintf( szLog, pszFormat, argList );
va_end( argList );
//Trancate if the file grow too large
long lLength = ftell(m_pLogFile);
if (lLength > m_lTruncate)
rewind(m_pLogFile);
//Get current time
SYSTEMTIME time;
::GetLocalTime(&time);
TCHAR szLine[1024*10];
sprintf(szLine, "%02d:%02d:%02d:%03d \t%s\n",
time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
szLog);
fputs(szLine, m_pLogFile);
singleLock.Unlock();
}
catch(...){
return;
}
if (singleLock.IsLocked()) singleLock.Unlock();
}
private:
FILE* m_pLogFile;
long m_lTruncate;
CCriticalSection m_cs;
};
#endif //_ATA_LOGFILE_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -