简单的日志类

    技术2022-05-20  51

    头文件:

     

    #ifndef __Log_h__ #define __Log_h__ #include <Windows.h> #include <tchar.h> #define LOG_PATH_LEN 256 typedef enum _log_level { LL_INF, LL_WAR, LL_ERR, LL_NUM } LogLevel; class CLog { public: CLog(); CLog( const TCHAR *pName ); ~CLog(); void SetName( const TCHAR *pName ) { if( NULL != pName ) { memset( _Name, 0, sizeof(TCHAR) * LOG_PATH_LEN ); _tcscpy_s( _Name, pName ); } } const TCHAR* GetName() const { return _Name; } BOOL Open(); BOOL IsOpen() { return _IsOpen; } void Close(); void Write( const TCHAR *pszText, LogLevel level = LL_INF ); void WriteInf( const TCHAR *pszText ); void WriteWar( const TCHAR *pszText ); void WriteErr( const TCHAR *pszText ); private: static char* _LevelText[LL_NUM]; CRITICAL_SECTION _CS; HANDLE _File; TCHAR _Name[LOG_PATH_LEN]; BOOL _IsOpen; CLog( const CLog& ); CLog& operator = ( const CLog& ); }; #endif // __Log_h__

     

    源文件:

     

    #include "Log.h" #include <time.h> #include <stdio.h> char* CLog::_LevelText[LL_NUM] = { "Information", "Warning ", "Error " }; CLog::CLog() : _File(NULL), _IsOpen(FALSE) { memset( _Name, 0, sizeof(TCHAR) * LOG_PATH_LEN ); InitializeCriticalSection( &_CS ); } CLog::CLog( const TCHAR *pName ) : _File(NULL), _IsOpen(FALSE) { memset( _Name, 0, sizeof(TCHAR) * LOG_PATH_LEN ); _tcscpy_s( _Name, pName ); InitializeCriticalSection( &_CS ); } CLog::~CLog() { DeleteCriticalSection( &_CS ); } BOOL CLog::Open() { if( _IsOpen ) Close(); _File = CreateFile( _Name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if( INVALID_HANDLE_VALUE == _File ) return FALSE; _IsOpen = TRUE; char p[] = "/r/n/r/n"; DWORD len; EnterCriticalSection( &_CS ); SetFilePointer( _File, 0, NULL, FILE_END ); WriteFile( _File, p, strlen(p), &len, NULL ); LeaveCriticalSection( &_CS ); return TRUE; } void CLog::Close() { if( _IsOpen ) { CloseHandle( _File ); _IsOpen = FALSE; _File = NULL; } } void CLog::Write( const TCHAR *pszText, LogLevel level /* = LL_INF */ ) { if( NULL == pszText || !_IsOpen ) return; DWORD len; // date and time time_t now = time( NULL ); struct tm datetime = { 0 }; localtime_s( &datetime, &now ); char pszTime[32] = { 0 }; sprintf( pszTime, "%d-d-d d:d:d ", datetime.tm_year + 1990, datetime.tm_mon + 1, datetime.tm_mday, datetime.tm_hour, datetime.tm_min, datetime.tm_sec ); EnterCriticalSection( &_CS ); WriteFile( _File, pszTime, strlen(pszTime), &len, NULL ); // level WriteFile( _File, _LevelText[level], strlen(_LevelText[level]), &len, NULL ); WriteFile( _File, " ", strlen(" "), &len, NULL ); // text #ifdef UNICODE char *p = NULL; len = WideCharToMultiByte( CP_ACP, 0, pszText, -1, NULL, 0, NULL, NULL ); p = new char[len]; WideCharToMultiByte( CP_ACP, 0, pszText, -1, p, len, NULL, NULL ); WriteFile( _File, p, strlen(p), &len, NULL ); delete []p; p = NULL; #else WriteFile( _File, pszText, strlen(pszText), &len, NULL ); #endif WriteFile( _File, "/r/n", strlen("/r/n"), &len, NULL ); LeaveCriticalSection( &_CS ); } void CLog::WriteInf( const TCHAR *pszText ) { if( NULL == pszText || !_IsOpen ) return; Write( pszText, LL_INF ); } void CLog::WriteWar( const TCHAR *pszText ) { if( NULL == pszText || !_IsOpen ) return; Write( pszText, LL_WAR ); } void CLog::WriteErr( const TCHAR *pszText ) { if( NULL == pszText || !_IsOpen ) return; Write( pszText, LL_ERR ); }

     

    测试代码:

     

    #include "Log.h" int main( void ) { CLog log( _T("test.log") ); log.Open(); log.Write( _T("this is writen by 'write' !") ); log.WriteInf( _T("this is writen by 'WriteInf' !") ); log.WriteWar( _T("this is writen by 'WriteWar' !") ); log.WriteErr( _T("this is writen by 'WriteErr' !") ); log.Close(); return 0; }


    最新回复(0)