实现功能:每次写入内容到log文件中,且都是从后面追加的,如果文件大小超过500k,则清空内容重新写入
1.新建MFC工程
1)添加互斥文件
添加semaphoreclass.h文件
里面加入:
#ifndef _SEMAPHORECLASS_H_ #define _SEMAPHORECLASS_H_ class CSemaphoreClass { public: CSemaphoreClass(); ~CSemaphoreClass(); void Lock(); void Unlock(); private: CRITICAL_SECTION m_hSem; }; class CAutoLock { public: CAutoLock(CSemaphoreClass *pCSem); ~CAutoLock(); private: CSemaphoreClass *m_pAutoSem; }; #endif//_SEMAPHORECLASS_H_
#include "stdafx.h" #include "semaphoreclass.h" CSemaphoreClass::CSemaphoreClass() { InitializeCriticalSection(&m_hSem); } CSemaphoreClass::~CSemaphoreClass() { DeleteCriticalSection(&m_hSem); } void CSemaphoreClass::Lock() { EnterCriticalSection(&m_hSem); } void CSemaphoreClass::Unlock() { LeaveCriticalSection(&m_hSem); } CAutoLock::CAutoLock( CSemaphoreClass *pCSem ) { ASSERT(pCSem); m_pAutoSem = pCSem; m_pAutoSem->Lock(); } CAutoLock::~CAutoLock() { ASSERT(m_pAutoSem); m_pAutoSem->Unlock(); m_pAutoSem = NULL; }
2.日志文件功能添加
添加log.h,里面加入
#ifndef _LOG_H_ #define _LOG_H_ #include <Windows.h> #include <iostream> using namespace std; #define LOG_DIR _T("Log\\") #define LOG_FILE _T("my.log") #define LOG_SIZE (500*1024) class CLog { public: CLog(); ~CLog();
static CString GetModuleFullPath(void); static void WriteLog(const char *pTemp);};#define LOG CLog::WriteLog#endif//_LOG_H_ log.cpp中添加:
#include "stdafx.h" #include "semaphoreclass.h"
static CSemaphoreClass g_tSemaphore; static CString g_strLogFile;
CString CLog::GetModuleFullPath( void ) { TCHAR achDllName[32768] = _T(""); CString csPath = _T(""); //AfxGetInstanceHandle()可能获取失败 // GetModuleFileName( AfxGetInstanceHandle(), achDllName, MAX_PATH); MEMORY_BASIC_INFORMATION mbi; static int dummy; UINT dwLength = sizeof(mbi); if ( dwLength == VirtualQuery( &dummy, &mbi, dwLength ) ) { GetModuleFileName( reinterpret_cast<HMODULE>(mbi.AllocationBase), achDllName, _countof(achDllName) ); size_t nBuffLen = GetLongPathName( achDllName, 0, 0 ); if ( nBuffLen == _tcslen( achDllName ) ) { csPath = achDllName; } else { TCHAR* pChar = new TCHAR[nBuffLen + 1]; ZeroMemory( pChar, sizeof(TCHAR) * ( nBuffLen + 1 ) ); GetLongPathName( achDllName, pChar, nBuffLen ); csPath = pChar; delete[] pChar; } } csPath = csPath.Left( csPath.ReverseFind(_T('\\')) + 1 ); return csPath; }
void CLog::WriteLog( const char *pTemp ) { if (g_strLogFile.IsEmpty()) { CString strPath = GetModuleFullPath(); CString strLogDir = strPath + LOG_DIR; if(!PathFileExists(strLogDir)) { CreateDirectory(strLogDir, NULL); } g_strLogFile = strLogDir + LOG_FILE; } CAutoLock cAuto(&g_tSemaphore); CFile cfile; //打开 if (!PathFileExists(g_strLogFile)) { BOOL bFlag = cfile.Open(g_strLogFile, CFile::modeCreate | CFile::modeWrite); if (!bFlag) { return; } } else { CFileStatus fileStaus; if (CFile::GetStatus(g_strLogFile, fileStaus)) { UINT nOpenFlag = 0; if (fileStaus.m_size > LOG_SIZE) { nOpenFlag = CFile::modeCreate | CFile::modeWrite; } else { nOpenFlag = CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate; } BOOL bFlag = cfile.Open(g_strLogFile, nOpenFlag); if (!bFlag) { return; } } } // if (cfile.m_hFile != CFile::hFileNull) { cfile.SeekToEnd(); cfile.Write(pTemp, strlen(pTemp)+1); cfile.Close(); } }
3.调用
for (int i=0;i<100;i++) { LOG( "开始运行\r\n" ); }