Log.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. LeaveCriticalSection(&m_cs);
  61. return FALSE;
  62. }
  63. string str(pszFileName);
  64. string::size_type pos = str.rfind(_T('.'));
  65. if (pos != string::npos)
  66. str=str.substr(0,pos);
  67. m_FileName = str;
  68. DWORD dwLen=GetFileSize(m_hFile,NULL);
  69. m_idx.dwLen=dwLen;
  70. m_idx.dwPos=0;
  71. SetFilePointer(m_hFile,m_idx.dwPos+m_idx.dwLen,NULL,FILE_BEGIN);
  72. SetEndOfFile(m_hFile);
  73. LeaveCriticalSection(&m_cs);
  74. return TRUE;
  75. }
  76. void CLog::Put(wstring msg,UINT nLevel)
  77. {
  78. if(nLevel>m_nMinLevel || msg.length()==0 )
  79. return;
  80. if( m_hFile==INVALID_HANDLE_VALUE )
  81. return;
  82. const wchar_t *pszMsg=msg.c_str();
  83. USES_CONVERSION;
  84. Put(W2A(pszMsg), nLevel);
  85. }
  86. void CLog::Put(string msg, UINT nLevel)
  87. {
  88. if(nLevel>m_nMinLevel || msg.length()==0 )
  89. return;
  90. const char *pszMsg=msg.c_str();
  91. EnterCriticalSection(&m_cs);
  92. if( m_hFile==INVALID_HANDLE_VALUE )
  93. {
  94. LeaveCriticalSection(&m_cs);
  95. return;
  96. }
  97. SYSTEMTIME st;
  98. GetLocalTime(&st);
  99. static char chBuf[64]={0};
  100. // _stprintf(chBuf,_T("%02d-%02d-%02d %02d:%02d:%02d "),
  101. // st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
  102. 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());
  103. DWORD dw;
  104. WriteFile(m_hFile, chBuf, strlen(chBuf), &dw, NULL);
  105. WriteFile(m_hFile, msg.c_str(), msg.length(), &dw, NULL);
  106. FlushFileBuffers(m_hFile);
  107. // while(pszMsg!=NULL && strlen(pszMsg)>0){
  108. // string str,str2;
  109. //// TCHAR *pch=_tcschr(pszMsg,_T('\n'));
  110. // char *pch=strchr((char*)pszMsg,'\n');
  111. // if(pch>0){
  112. // str2.assign(pszMsg,strlen(pch));
  113. // pszMsg=pch+1;
  114. // }
  115. // else{
  116. // str2=pszMsg;
  117. // pszMsg=NULL;
  118. // }
  119. // string_trim(str2);
  120. // str.reserve(1024);
  121. // str=chBuf;
  122. // str += " ";
  123. // str += logDescripe[nLevel];
  124. // str += " ";
  125. // str+=str2;
  126. // str+="\r\n\r\n";
  127. // DWORD dw;
  128. // SetFilePointer(m_hFile,m_idx.dwPos+m_idx.dwLen,NULL,FILE_BEGIN);
  129. // size_t nLen=str.length();
  130. //
  131. //
  132. // if(WriteFile(m_hFile,str.c_str(),nLen,&dw,NULL) && dw==nLen){
  133. // m_idx.dwPos+=m_idx.dwLen;
  134. // m_idx.dwLen=nLen;
  135. // m_idx.nLevel=nLevel;
  136. // }
  137. //#ifdef _DEBUG
  138. // printf(str.c_str());
  139. // FlushFileBuffers(m_hFile);
  140. //#endif
  141. // }
  142. if(GetFileSize(m_hFile,NULL)>m_nMaxLogSize){
  143. //н¨LogÎļþ
  144. CloseHandle(m_hFile);
  145. m_hFile=INVALID_HANDLE_VALUE;
  146. string str=m_FileName;
  147. string strSrc, strDest;
  148. strSrc = str + string(".log");
  149. strDest = str + string("_bak.log");
  150. BOOL res=MoveFileExA(strSrc.c_str(),strDest.c_str(),MOVEFILE_REPLACE_EXISTING);
  151. str+=".log";
  152. m_idx.dwPos=0L;
  153. Init(str.c_str(), m_nMinLevel, m_nMaxLogSize);
  154. }
  155. LeaveCriticalSection(&m_cs);
  156. }
  157. void CLog::PutMsg(UINT nLevel,const char *szFmt, ...)
  158. {
  159. if (!szFmt)
  160. return;
  161. va_list ap;
  162. va_start(ap, szFmt);
  163. _vsnprintf(m_pBuf, m_bufSize, szFmt, ap);
  164. va_end(ap);
  165. Put(m_pBuf,nLevel);
  166. }