C++日志記錄類實(shí)例解析
本文所述實(shí)例是從一個(gè)Red Hat開源項(xiàng)目里面扒出來的,非常實(shí)用!讀者還可以根據(jù)自身需求加以修改!完整源碼如下:
Log.h文件部分:
#ifndef __LOG_H__ #define __LOG_H__ #include <stdio.h> #include <tchar.h> #include <crtdbg.h> #include <windows.h> #include <time.h> #include <sys/timeb.h> class CLog { public: ~CLog(); static CLog* get(TCHAR* path = NULL); void printf(const char* format, ...); private: CLog(FILE* handle); private: static CLog* _log; FILE* _handle; }; enum { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL }; #ifdef _DEBUG static unsigned int log_level = LOG_DEBUG; #else static unsigned int log_level = LOG_INFO; #endif #define PRINT_LINE(type, format, datetime, ms, ...) \ printf("%lu::%s::%s,%.3d::%s::" format "\n", GetCurrentThreadId(), type, datetime, ms, \ __FUNCTION__, ## __VA_ARGS__); #define LOG(type, format, ...) do { \ if (type >= log_level && type <= LOG_FATAL) { \ CLog* log = CLog::get(); \ const char *type_as_char[] = { "DEBUG", "INFO", "WARN", "ERROR", "FATAL" }; \ struct _timeb now; \ struct tm today; \ char datetime_str[20]; \ _ftime_s(&now); \ localtime_s(&today, &now.time); \ strftime(datetime_str, 20, "%Y-%m-%d %H:%M:%S", &today); \ if (log) { \ log->PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \ } else { \ PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \ } \ } \ } while(0) #define log_printf(format, ...) LOG(LOG_INFO, format, ## __VA_ARGS__) #define LOG_INFO(format, ...) LOG(LOG_INFO, format, ## __VA_ARGS__) #define LOG_WARN(format, ...) LOG(LOG_WARN, format, ## __VA_ARGS__) #define LOG_ERROR(format, ...) LOG(LOG_ERROR, format, ## __VA_ARGS__) #define DBGLEVEL 1000 #define DBG(level, format, ...) do { \ if (level <= DBGLEVEL) { \ LOG(LOG_DEBUG, format, ## __VA_ARGS__); \ } \ } while(0) #define ASSERT(x) _ASSERTE(x) #endif
Log.cpp文件部分:
#include "Log.h" #include <stdio.h> #include <stdarg.h> #include <share.h> #define LOG_ROLL_SIZE (1024 * 1024) CLog* CLog::_log = NULL; CLog::CLog(FILE* handle) : _handle(handle) { _log = this; } CLog::~CLog() { if (_log && _handle) { fclose(_handle); _log = NULL; } } CLog* CLog::get(char* path) { if (_log) { return _log; } if(!path) { path = "dll.log"; } DWORD size = 0; HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (file != INVALID_HANDLE_VALUE) { size = GetFileSize(file, NULL); CloseHandle(file); } if (size != INVALID_FILE_SIZE && size > LOG_ROLL_SIZE) { TCHAR roll_path[MAX_PATH]; sprintf(roll_path, "%s.1", path); if (!MoveFileEx(path, roll_path, MOVEFILE_REPLACE_EXISTING)) { return NULL; } } FILE* handle = fopen(path, "a+"); if (!handle) { return NULL; } _log = new CLog(handle); return _log; } void CLog::printf(const char* format, ...) { va_list args; va_start(args, format); vfprintf(_handle, format, args); va_end(args); fflush(_handle); }
相關(guān)文章
c/c++獲取系統(tǒng)時(shí)間函數(shù)的方法示例
這篇文章主要介紹了c/c++獲取系統(tǒng)時(shí)間函數(shù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Easyx實(shí)現(xiàn)窗口自動(dòng)碰撞的小球
這篇文章主要為大家詳細(xì)介紹了Easyx實(shí)現(xiàn)窗口自動(dòng)碰撞的小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)中求解迷宮問題實(shí)現(xiàn)方法
這篇文章主要介紹了C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)中求解迷宮問題實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-03-03使用QGraphicsView實(shí)現(xiàn)氣泡聊天窗口+排雷功能
這篇文章主要介紹了使用QGraphicsView實(shí)現(xiàn)氣泡聊天窗口+排雷,重點(diǎn)給大家介紹使用QWebEngineView控件內(nèi)嵌html+CSS的實(shí)現(xiàn)方式,需要的朋友可以參考下2022-04-04利用C語(yǔ)言玩轉(zhuǎn)魔方陣實(shí)例教程
這篇文章主要給大家介紹了關(guān)于利用C語(yǔ)言玩轉(zhuǎn)魔方陣的相關(guān)資料,文中詳細(xì)介紹了關(guān)于奇數(shù)魔方陣和4N 魔方陣的實(shí)現(xiàn)方法,通過示例代碼讓大家更好的參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11C/C++ 獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼
這篇文章主要介紹了C/C++ 獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10