#pragma once /******************************************************************** created: 2014/05/12 created: 12:5:2014 20:27 filename: d:\MFCUI\Log\Log\Log.h file path: d:\MFCUI\Log\Log file base: Log file ext: h author: BJ.y purpose: *********************************************************************/ #ifndef LOG_H__ #define LOG_H__ //#ifndef _WINDOWS_ //#include <windows.h> // //#endif #include <afxwin.h> #include <assert.h> #include <stdio.h> #define CONTENT_TITLE 32 #define LOG_INFO(format, ...) do \ { CLog::GetInstance().Logf(""format" file:"__FILE__", function:"__FUNCTION__",line: %05d", ##__VA_ARGS__, __LINE__); } while (0) class CLog { private: // constructor CLog(void); //destructor ~CLog(void); // copy constructor CLog(const CLog &); //overloaded const CLog & operator = (const CLog &); private: CRITICAL_SECTION m_Lock; // file descriptor FILE * m_File; //create time SYSTEMTIME m_Time; // content title char szContentTitle[CONTENT_TITLE]; private: // get file handle FILE * GetFileFp(); public: // get instance static CLog & GetInstance(); // log a formate string void Logf(const char * lpFormat, ...); }; #endif // Log_h__
#include "Log.h" #include <stdarg.h> //************************************ // Method: CLog // FullName: CLog::CLog // Access: private // Returns: // Qualifier: // Parameter: void //************************************ CLog::CLog(void) { m_File = NULL; ::InitializeCriticalSection(&m_Lock); } //************************************ // Method: ~CLog // FullName: CLog::~CLog // Access: private // Returns: // Qualifier: // Parameter: void //************************************ CLog::~CLog(void) { ::DeleteCriticalSection(&m_Lock); } //************************************ // Method: GetInstance // FullName: CLog::GetInstance // Access: public // Returns: CLog & // Qualifier: //************************************ CLog & CLog::GetInstance() { static CLog m_Log; return m_Log; } //************************************ // Method: Logf // FullName: CLog::Logf // Access: public // Returns: void // Qualifier: // Parameter: const char * lpFormat // Parameter: ... //************************************ void CLog::Logf(const char * lpFormat, ...) { __try { //enter the lock ::EnterCriticalSection(&m_Lock); GetFileFp(); assert(m_File != NULL); DWORD dwWrite = 0; // write the time fwrite(szContentTitle, sizeof(char), strlen(szContentTitle), m_File); //::WriteFile(m_File, szContentTitle, strlen(szContentTitle), &dwWrite, NULL); // write the content va_list args; va_start(args, lpFormat); vfprintf(m_File, lpFormat, args); va_end(args); // write the enter under windows fwrite("\n\r", sizeof(char), 1, m_File); //::WriteFile(m_File, "\r\n", 2, &dwWrite, NULL); return; } __finally { ::LeaveCriticalSection(&m_Lock); } } FILE * CLog::GetFileFp() { SYSTEMTIME sys; GetLocalTime(&sys); sprintf_s(szContentTitle, CONTENT_TITLE, "%02d:%02d:%02d %02d:%02d:%02d:%d->", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds); if (sys.wDay == m_Time.wDay) { return m_File; } if (m_File != NULL) { fclose(m_File); } m_Time = sys; char szFileName[32] = { 0 }; sprintf_s(szFileName, sizeof(szFileName), "%02d-%02d-%02d.txt", m_Time.wYear, m_Time.wMonth, m_Time.wDay); fopen_s(&m_File, szFileName, "a+"); //m_File = CreateFile(szFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS , NULL, NULL); //SetFilePointer(m_File, 0, NULL, FILE_END); return m_File; }
时间: 2024-10-17 07:52:38