Log.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Log.cpp: implementation of the CLog class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Log.h"
  6. #include <string>
  7. #include <tchar.h>
  8. #include <atlconv.h>
  9. using namespace std;
  10. inline void string_trim(std::string &str, char* pszMask = " \t\r\n")
  11. {
  12. std::string::size_type st;
  13. if ((st = str.find_last_not_of(pszMask)) == std::string::npos){
  14. str.erase();
  15. return;
  16. }
  17. str.erase(++st);
  18. if ((st = str.find_first_not_of(pszMask)) != 0)
  19. str.erase(0, st);
  20. }
  21. string CLog::logDescripe[] = { "Fatal", "Failed", "Error", "Warn", "Info", "Trace", "Debug", "All" };
  22. //////////////////////////////////////////////////////////////////////
  23. // Construction/Destruction
  24. //////////////////////////////////////////////////////////////////////
  25. CLog::CLog()
  26. : m_hFile(INVALID_HANDLE_VALUE)
  27. , m_nMinLevel(LogLvlAll)
  28. , m_nMaxLogSize(10*1024*1024)
  29. {
  30. m_FileName = "";
  31. InitializeCriticalSection(&m_cs);
  32. m_bufSize = 1024 * 1024;
  33. m_pBuf = new char[m_bufSize];
  34. }
  35. CLog::~CLog()
  36. {
  37. if(m_hFile!=INVALID_HANDLE_VALUE)
  38. CloseHandle(m_hFile);
  39. DeleteCriticalSection(&m_cs);
  40. delete[] m_pBuf;
  41. }
  42. BOOL CLog::Init(const char* pszFileName, UINT nMinLevel, int nMaxSize)
  43. {
  44. string fulllogPath = pszFileName;
  45. pszFileName = fulllogPath.c_str();
  46. m_nMinLevel=nMinLevel;
  47. EnterCriticalSection(&m_cs);
  48. if (nMaxSize <= 0)
  49. nMaxSize = 1*1024*1024;
  50. m_nMaxLogSize = nMaxSize;
  51. if(m_hFile!=INVALID_HANDLE_VALUE)
  52. {
  53. if(CloseHandle(m_hFile))
  54. m_hFile = INVALID_HANDLE_VALUE;
  55. }
  56. m_hFile = CreateFileA(pszFileName,GENERIC_WRITE,FILE_SHARE_READ,
  57. NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  58. if (m_hFile == INVALID_HANDLE_VALUE)
  59. {
  60. DWORD err = GetLastError();
  61. LeaveCriticalSection(&m_cs);
  62. return FALSE;
  63. }
  64. string str(pszFileName);
  65. string::size_type pos = str.rfind(_T('.'));
  66. if (pos != string::npos)
  67. str=str.substr(0,pos);
  68. m_FileName = str;
  69. DWORD dwLen=GetFileSize(m_hFile,NULL);
  70. m_idx.dwLen=dwLen;
  71. m_idx.dwPos=0;
  72. SetFilePointer(m_hFile,m_idx.dwPos+m_idx.dwLen,NULL,FILE_BEGIN);
  73. SetEndOfFile(m_hFile);
  74. LeaveCriticalSection(&m_cs);
  75. return TRUE;
  76. }
  77. void CLog::Put(wstring msg,UINT nLevel)
  78. {
  79. if(nLevel>m_nMinLevel || msg.length()==0 )
  80. return;
  81. if( m_hFile==INVALID_HANDLE_VALUE )
  82. return;
  83. const wchar_t *pszMsg=msg.c_str();
  84. USES_CONVERSION;
  85. Put(W2A(pszMsg), nLevel);
  86. }
  87. void CLog::Put(string msg, UINT nLevel)
  88. {
  89. if(nLevel>m_nMinLevel || msg.length()==0 )
  90. return;
  91. const char *pszMsg=msg.c_str();
  92. EnterCriticalSection(&m_cs);
  93. if( m_hFile==INVALID_HANDLE_VALUE )
  94. {
  95. LeaveCriticalSection(&m_cs);
  96. return;
  97. }
  98. //MessageBoxA(0,msg.c_str(),"",0);
  99. SYSTEMTIME st;
  100. GetLocalTime(&st);
  101. static char chBuf[64]={0};
  102. // _stprintf(chBuf,_T("%02d-%02d-%02d %02d:%02d:%02d "),
  103. // st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
  104. 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());
  105. DWORD dw;
  106. WriteFile(m_hFile, chBuf, strlen(chBuf), &dw, NULL);
  107. WriteFile(m_hFile, msg.c_str(), msg.length(), &dw, NULL);
  108. FlushFileBuffers(m_hFile);
  109. // while(pszMsg!=NULL && strlen(pszMsg)>0){
  110. // string str,str2;
  111. //// TCHAR *pch=_tcschr(pszMsg,_T('\n'));
  112. // char *pch=strchr((char*)pszMsg,'\n');
  113. // if(pch>0){
  114. // str2.assign(pszMsg,strlen(pch));
  115. // pszMsg=pch+1;
  116. // }
  117. // else{
  118. // str2=pszMsg;
  119. // pszMsg=NULL;
  120. // }
  121. // string_trim(str2);
  122. // str.reserve(1024);
  123. // str=chBuf;
  124. // str += " ";
  125. // str += logDescripe[nLevel];
  126. // str += " ";
  127. // str+=str2;
  128. // str+="\r\n\r\n";
  129. // DWORD dw;
  130. // SetFilePointer(m_hFile,m_idx.dwPos+m_idx.dwLen,NULL,FILE_BEGIN);
  131. // size_t nLen=str.length();
  132. //
  133. //
  134. // if(WriteFile(m_hFile,str.c_str(),nLen,&dw,NULL) && dw==nLen){
  135. // m_idx.dwPos+=m_idx.dwLen;
  136. // m_idx.dwLen=nLen;
  137. // m_idx.nLevel=nLevel;
  138. // }
  139. //#ifdef _DEBUG
  140. // printf(str.c_str());
  141. // FlushFileBuffers(m_hFile);
  142. //#endif
  143. // }
  144. if(GetFileSize(m_hFile,NULL)>m_nMaxLogSize){
  145. //н¨LogÎļþ
  146. CloseHandle(m_hFile);
  147. m_hFile=INVALID_HANDLE_VALUE;
  148. string str=m_FileName;
  149. string strSrc, strDest;
  150. strSrc = str + string(".log");
  151. strDest = str + string("_bak.log");
  152. BOOL res=MoveFileExA(strSrc.c_str(),strDest.c_str(),MOVEFILE_REPLACE_EXISTING);
  153. str+=".log";
  154. m_idx.dwPos=0L;
  155. Init(str.c_str(), m_nMinLevel, m_nMaxLogSize);
  156. }
  157. LeaveCriticalSection(&m_cs);
  158. }
  159. void CLog::PutMsg(UINT nLevel,const char *szFmt, ...)
  160. {
  161. if (!szFmt)
  162. return;
  163. va_list ap;
  164. va_start(ap, szFmt);
  165. _vsnprintf(m_pBuf, m_bufSize, szFmt, ap);
  166. va_end(ap);
  167. Put(m_pBuf,nLevel);
  168. }
  169. void CLog::PutErrMsg( const char *szFmt, ...)
  170. {
  171. if (!szFmt)
  172. return;
  173. va_list ap;
  174. va_start(ap, szFmt);
  175. _vsnprintf(m_pBuf, m_bufSize, szFmt, ap);
  176. va_end(ap);
  177. Put(m_pBuf, LogLvlError);
  178. }