123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- // Log.cpp: implementation of the CLog class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Log.h"
- #include <string>
- #include <tchar.h>
- #include <atlconv.h>
- using namespace std;
- inline void string_trim(std::string &str, char* pszMask = " \t\r\n")
- {
- std::string::size_type st;
- if ((st = str.find_last_not_of(pszMask)) == std::string::npos){
- str.erase();
- return;
- }
- str.erase(++st);
- if ((st = str.find_first_not_of(pszMask)) != 0)
- str.erase(0, st);
- }
- string CLog::logDescripe[] = { "Fatal", "Failed", "Error", "Warn", "Info", "Trace", "Debug", "All" };
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLog::CLog()
- : m_hFile(INVALID_HANDLE_VALUE)
- , m_nMinLevel(LogLvlAll)
- , m_nMaxLogSize(10*1024*1024)
- {
- m_FileName = "";
- InitializeCriticalSection(&m_cs);
- m_bufSize = 1024 * 1024;
- m_pBuf = new char[m_bufSize];
- }
- CLog::~CLog()
- {
- if(m_hFile!=INVALID_HANDLE_VALUE)
- CloseHandle(m_hFile);
- DeleteCriticalSection(&m_cs);
- delete[] m_pBuf;
- }
- BOOL CLog::Init(const char* pszFileName, UINT nMinLevel, int nMaxSize)
- {
- string fulllogPath = pszFileName;
- pszFileName = fulllogPath.c_str();
- m_nMinLevel=nMinLevel;
- EnterCriticalSection(&m_cs);
- if (nMaxSize <= 0)
- nMaxSize = 1*1024*1024;
- m_nMaxLogSize = nMaxSize;
- if(m_hFile!=INVALID_HANDLE_VALUE)
- {
- if(CloseHandle(m_hFile))
- m_hFile = INVALID_HANDLE_VALUE;
- }
- m_hFile = CreateFileA(pszFileName,GENERIC_WRITE,FILE_SHARE_READ,
- NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
- if (m_hFile == INVALID_HANDLE_VALUE)
- {
- DWORD err = GetLastError();
- LeaveCriticalSection(&m_cs);
- return FALSE;
- }
- string str(pszFileName);
- string::size_type pos = str.rfind(_T('.'));
- if (pos != string::npos)
- str=str.substr(0,pos);
- m_FileName = str;
- DWORD dwLen=GetFileSize(m_hFile,NULL);
- m_idx.dwLen=dwLen;
- m_idx.dwPos=0;
- SetFilePointer(m_hFile,m_idx.dwPos+m_idx.dwLen,NULL,FILE_BEGIN);
- SetEndOfFile(m_hFile);
- LeaveCriticalSection(&m_cs);
-
- return TRUE;
- }
- void CLog::Put(wstring msg,UINT nLevel)
- {
- if(nLevel>m_nMinLevel || msg.length()==0 )
- return;
- if( m_hFile==INVALID_HANDLE_VALUE )
- return;
- const wchar_t *pszMsg=msg.c_str();
- USES_CONVERSION;
- Put(W2A(pszMsg), nLevel);
- }
- void CLog::Put(string msg, UINT nLevel)
- {
- if(nLevel>m_nMinLevel || msg.length()==0 )
- return;
- const char *pszMsg=msg.c_str();
- EnterCriticalSection(&m_cs);
- if( m_hFile==INVALID_HANDLE_VALUE )
- {
- LeaveCriticalSection(&m_cs);
- return;
- }
- //MessageBoxA(0,msg.c_str(),"",0);
- SYSTEMTIME st;
- GetLocalTime(&st);
- static char chBuf[64]={0};
- // _stprintf(chBuf,_T("%02d-%02d-%02d %02d:%02d:%02d "),
- // st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
- sprintf(chBuf, "\r\n\r\n%02d-%02d %02d:%02d:%02d %s ",st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond, logDescripe[nLevel].c_str());
- DWORD dw;
- WriteFile(m_hFile, chBuf, strlen(chBuf), &dw, NULL);
- WriteFile(m_hFile, msg.c_str(), msg.length(), &dw, NULL);
- FlushFileBuffers(m_hFile);
- // while(pszMsg!=NULL && strlen(pszMsg)>0){
- // string str,str2;
- //// TCHAR *pch=_tcschr(pszMsg,_T('\n'));
- // char *pch=strchr((char*)pszMsg,'\n');
- // if(pch>0){
- // str2.assign(pszMsg,strlen(pch));
- // pszMsg=pch+1;
- // }
- // else{
- // str2=pszMsg;
- // pszMsg=NULL;
- // }
- // string_trim(str2);
- // str.reserve(1024);
- // str=chBuf;
- // str += " ";
- // str += logDescripe[nLevel];
- // str += " ";
- // str+=str2;
- // str+="\r\n\r\n";
- // DWORD dw;
- // SetFilePointer(m_hFile,m_idx.dwPos+m_idx.dwLen,NULL,FILE_BEGIN);
- // size_t nLen=str.length();
- //
- //
- // if(WriteFile(m_hFile,str.c_str(),nLen,&dw,NULL) && dw==nLen){
- // m_idx.dwPos+=m_idx.dwLen;
- // m_idx.dwLen=nLen;
- // m_idx.nLevel=nLevel;
- // }
- //#ifdef _DEBUG
- // printf(str.c_str());
- // FlushFileBuffers(m_hFile);
- //#endif
- // }
- if(GetFileSize(m_hFile,NULL)>m_nMaxLogSize){
- //н¨LogÎļþ
- CloseHandle(m_hFile);
- m_hFile=INVALID_HANDLE_VALUE;
- string str=m_FileName;
- string strSrc, strDest;
- strSrc = str + string(".log");
- strDest = str + string("_bak.log");
- BOOL res=MoveFileExA(strSrc.c_str(),strDest.c_str(),MOVEFILE_REPLACE_EXISTING);
- str+=".log";
- m_idx.dwPos=0L;
- Init(str.c_str(), m_nMinLevel, m_nMaxLogSize);
- }
- LeaveCriticalSection(&m_cs);
- }
- void CLog::PutMsg(UINT nLevel,const char *szFmt, ...)
- {
- if (!szFmt)
- return;
- va_list ap;
- va_start(ap, szFmt);
- _vsnprintf(m_pBuf, m_bufSize, szFmt, ap);
- va_end(ap);
- Put(m_pBuf,nLevel);
- }
- void CLog::PutErrMsg( const char *szFmt, ...)
- {
- if (!szFmt)
- return;
- va_list ap;
- va_start(ap, szFmt);
- _vsnprintf(m_pBuf, m_bufSize, szFmt, ap);
- va_end(ap);
- Put(m_pBuf, LogLvlError);
- }
|