Log.cpp 4.3 KB

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