// Log.cpp: implementation of the CLog class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Log.h" #include #include #include #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 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("upservice.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()); }