123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- // Log.cpp: implementation of the CLog class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "pch.h"
- #include "Log.h"
- #include <string>
- #include <tchar.h>
- #include <atlconv.h>
- #include "Buffers.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- using namespace std;
- typedef std::basic_string<char> TString;
- 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);
- }
- #ifdef _LOGCONSOLE
- #pragma comment( lib, "libPrintLogd.lib" )
- #endif // _LOGCONSOLE
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLog::CLog()
- : m_hFile(INVALID_HANDLE_VALUE)
- , m_nMinLevel(LogLvlAll)
- , m_nMaxLogSize(10*1024*1024)
- {
- InitializeCriticalSection(&m_cs);
- Init("pdf.log", LogLvlAll, 60);
- }
- CLog::~CLog()
- {
- if(m_hFile!=INVALID_HANDLE_VALUE)
- CloseHandle(m_hFile);
- DeleteCriticalSection(&m_cs);
- }
- BOOL CLog::Init(const char* pszFileName,UINT nMinLevel,UINT nMaxSizeMB)
- {
- EnterCriticalSection(&m_cs);
- m_nMinLevel=nMinLevel;
- if(nMaxSizeMB<=0)
- nMaxSizeMB=10;
- m_nMaxLogSize=nMaxSizeMB*1024*1024;
- 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)
- {
- LeaveCriticalSection(&m_cs);
- return FALSE;
- }
- TString str(pszFileName);
- TString::size_type pos= str.rfind(_T('.'));
- if(pos!=TString::npos)
- str=str.substr(0,pos);
- strcpy(m_chFileName,str.c_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(const wchar_t* pszMsg,UINT nLevel)
- {
- if(nLevel>m_nMinLevel || pszMsg==NULL )
- return;
- if( m_hFile==INVALID_HANDLE_VALUE )
- return;
- USES_CONVERSION;
- Put(W2A(pszMsg), nLevel);
- }
- void CLog::Put(char* pszMsg, UINT nLevel)
- {
- if(nLevel>m_nMinLevel || pszMsg==NULL )
- return;
- EnterCriticalSection(&m_cs);
- if( m_hFile==INVALID_HANDLE_VALUE )
- {
- LeaveCriticalSection(&m_cs);
- return;
- }
- SYSTEMTIME st;
- GetLocalTime(&st);
- char chBuf[24]={0};
- // _stprintf(chBuf,_T("%02d-%02d-%02d %02d:%02d:%02d "),
- // st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
- sprintf(chBuf,"%02d-%02d-%02d %02d:%02d:%02d ",
- st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
-
- while(pszMsg!=NULL && strlen(pszMsg)>0){
- string str,str2;
- // TCHAR *pch=_tcschr(pszMsg,_T('\n'));
- char *pch=strchr(pszMsg,'\n');
- if(pch>0){
- str2.assign(pszMsg,pch);
- pszMsg=pch+1;
- }
- else{
- str2=pszMsg;
- pszMsg=NULL;
- }
- string_trim(str2);
- str.reserve(1024);
- str=chBuf;
- str+=str2;
- str+="\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;
- }
- }
- if(GetFileSize(m_hFile,NULL)>m_nMaxLogSize){
- //н¨LogÎļþ
- CloseHandle(m_hFile);
- m_hFile=INVALID_HANDLE_VALUE;
- TString str=m_chFileName;
- TString strSrc,strDest;
- strSrc=str+TString(".log");
- strDest=str+TString("_bak.log");
- MoveFileExA(strSrc.c_str(),strDest.c_str(),MOVEFILE_REPLACE_EXISTING);
- str+=".log";
- m_idx.dwPos=0L;
- Init(str.c_str(),m_nMinLevel);
- }
- LeaveCriticalSection(&m_cs);
- }
- void CLog::PutMsg(const char *szFmt, ...)
- {
- if (!szFmt)
- return;
- va_list ap;
- va_start(ap, szFmt);
- size_t nLength = _vsnprintf(nullptr, 0, szFmt, ap) + 1;
- CBuffers buffer;
- char* szLogMsg = buffer.GetBuf(nLength);
- _vsnprintf(szLogMsg, nLength, szFmt, ap);
- va_end(ap);
- Put(szLogMsg);
- }
- void CLog::PutMsg(const wchar_t *szFmt, ...)
- {
- if (!szFmt)
- return;
- va_list ap;
- va_start(ap, szFmt);
- size_t nLength = _vsntprintf(nullptr, 0, szFmt, ap) + 1;
- CBuffers buffer;
- TCHAR* szLogMsg = (TCHAR*)buffer.GetBuf(nLength*2);
- _vsntprintf(szLogMsg, nLength, szFmt, ap);
- va_end(ap);
- Put((char*)UnicodeToGB2312(szLogMsg).c_str());
- }
|