本文主要是介绍视频监控安防平台-基于log4cplus的log日志封装(linux版本),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
视频监控安防平台-基于log4cplus的log日志封装(linux版本)
目前使用的的日志相对而言比较稳定,目前应用于多个项目,稳定之后还没有出现过问题,log4cplus源码有所改动,可以根据日志目录、保存日志大小、保存天数、保存日志方式。
下面粘贴一下封装log4cplus的源码:
/****************************************************************************** * FILE: Log.h * Description: * Trace & Debug Tools Class. * * Modified Code History * Mark Date By Modification Reason ******************************************************************************* * 01 2007-1-29 songxw Initial creation. ******************************************************************************/ #ifndef __LOG_H__ #define __LOG_H__#include <log4cplus/logger.h> #include <log4cplus/consoleappender.h> #include <log4cplus/fileappender.h> #include <log4cplus/configurator.h> #include <log4cplus/layout.h> #include <log4cplus/appender.h> #include <log4cplus/helpers/loglog.h> #include <stdarg.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include "MyThread.h" #include "IPCS.h" #include "MyFifo.h"using namespace std; using namespace log4cplus; using namespace log4cplus::helpers;typedef enum {FATAL_LEVEL = 1 << 0, //output fatalERROR_LEVEL = 1 << 1, //output errorBREAK_LEVEL = 1 << 2, //output break pointMESSG_LEVEL = 1 << 3, //output messageDEBUG_LEVEL = 1 << 4, //output debugTRACE_LEVEL = 1 << 9 //output debug }LOG_LEVEL_E;class CLog { public:CLog();virtual ~CLog(); public:int StartSystem(const char *LogSavePath, const char *LogSaveMode, const int LogSaveDays, const int LogSaveSizes);void StopSystem();void DoLogProc(LOG_LEVEL_E level, const char* filename, const int fileline, const char* pFormat, ...);private:void CreateCatalog(const char *logcatalog);public:static inline CLog* getSingletonPtr(){return &getSingleton();}static inline CLog& getSingleton(){static CLog instance;return instance;}static bool m_bInited;FILE *logcfgfp; }; #define g_Logger CLog::getSingleton() #define g_pLogger CLog::getSingletonPtr() #define LOG_START(LogSavePath, LogSaveMode, LogSaveDays, LogSaveSizes) \g_Logger.StartSystem(LogSavePath, LogSaveMode, LogSaveDays, LogSaveSizes) #define LOG_STOP() g_Logger.StopSystem()/* Trace & Debug output function */ #define DBGPrint(module, level, string, args...) \g_Logger.DoLogProc(level, __FILE__, __LINE__, string, ##args)#define DO_LOGGER(logLevel, filename, fileline, pFormat, bufSize) \do \{ \if (g_Logger.m_bInited==true) \{ \log4cplus::Logger _logger = log4cplus::Logger::getRoot(); \if(_logger.isEnabledFor(logLevel)) \{ \va_list args; \va_start(args, pFormat); \char buf[bufSize] = {0}; \vsnprintf(buf, sizeof(buf), pFormat, args); \va_end(args); \_logger.forcedLog(logLevel, buf, filename, fileline); \} \} \}while(0)#endif
源码log.cpp
/****************************************************************************** * FILE: Log.cpp * Description: * Implementation of the Log class. * * Modified Code History * Mark Date By Modification Reason ******************************************************************************* * 01 2007-1-29 songxw Initial creation. ******************************************************************************/ #include "Log.h"//logger #define LOG4PLUS_LOG_CFG_LOGGER "log4cplus.rootLogger=TRACE, ConsoleAll, ConsoleMsg, AllLog, InfoLog, DebugLog, ErrorLog, FatalLog\r\n"//debug to fatal level #define LOG4PLUS_CONSOLE_ALL "log4cplus.appender.ConsoleAll=log4cplus::ConsoleAppender\r\n" \"log4cplus.appender.ConsoleAll.layout=log4cplus::PatternLayout\r\n" \"log4cplus.appender.ConsoleAll.layout.ConversionPattern=%s\r\n" \"log4cplus.appender.ConsoleAll.filters.1=log4cplus::spi::LogLevelRangeFilter\r\n" \"log4cplus.appender.ConsoleAll.filters.1.LogLevelMin=DEBUG\r\n" \"log4cplus.appender.ConsoleAll.filters.1.LogLevelMax=FATAL\r\n" //trace level #define LOG4PLUS_CONSOLE_TRACE "log4cplus.appender.ConsoleMsg=log4cplus::ConsoleAppender\r\n" \"log4cplus.appender.ConsoleMsg.layout=log4cplus::PatternLayout\r\n" \"log4cplus.appender.ConsoleMsg.layout.ConversionPattern=%s\r\n" \"log4cplus.appender.ConsoleMsg.filters.1=log4cplus::spi::LogLevelMatchFilter\r\n" \"log4cplus.appender.ConsoleMsg.filters.1.LogLevelToMatch=TRACE\r\n" \"log4cplus.appender.ConsoleMsg.filters.1.AcceptOnMatch=true\r\n" \"log4cplus.appender.ConsoleMsg.filters.2=log4cplus::spi::DenyAllFilter\r\n" //file log all #define LOG4PLUS_LOG_CFG_ALL "log4cplus.appender.AllLog=log4cplus::DailyRollingFileAppender\r\n" \"log4cplus.appender.AllLog.MaxFileSize=%dKB\r\n" \"log4cplus.appender.AllLog.File=%s/all/all.log\r\n" \"log4cplus.appender.AllLog.Schedule=%s\r\n" \"log4cplus.appender.AllLog.ImmediateFlush=true\r\n" \"log4cplus.appender.AllLog.MaxBackupIndex=10\r\n" \"log4cplus.appender.AllLog.MaxSaveDays=%d\r\n" \"log4cplus.appender.AllLog.layout=log4cplus::PatternLayout\r\n" \"log4cplus.appender.AllLog.layout.ConversionPattern=%s\r\n" \"log4cplus.appender.AllLog.filters.1=log4cplus::spi::LogLevelRangeFilter\r\n" \"log4cplus.appender.AllLog.filters.1.LogLevelMin=TRACE\r\n" \"log4cplus.appender.AllLog.filters.1.LogLevelMax=FATAL\r\n" //file log for info #define LOG4PLUS_LOG_CFG_INFO "log4cplus.appender.InfoLog=log4cplus::DailyRollingFileAppender\r\n" \"log4cplus.appender.InfoLog.MaxFileSize=%dKB\r\n" \"log4cplus.appender.InfoLog.File=%s/info/info.log\r\n" \"log4cplus.appender.InfoLog.Schedule=%s\r\n" \"log4cplus.appender.InfoLog.ImmediateFlush=true\r\n" \"log4cplus.appender.InfoLog.MaxBackupIndex=10\r\n" \"log4cplus.appender.InfoLog.MaxSaveDays=%d\r\n" \"log4cplus.appender.InfoLog.layout=log4cplus::PatternLayout\r\n" \"log4cplus.appender.InfoLog.layout.ConversionPattern=%s\r\n" \"log4cplus.appender.InfoLog.filters.1=log4cplus::spi::LogLevelMatchFilter\r\n" \"log4cplus.appender.InfoLog.filters.1.LogLevelToMatch=INFO\r\n" \"log4cplus.appender.InfoLog.filters.1.AcceptOnMatch=true\r\n" \"log4cplus.appender.InfoLog.filters.2=log4cplus::spi::DenyAllFilter\r\n" #define LOG4PLUS_LOG_CFG_DEBUG "log4cplus.appender.DebugLog=log4cplus::DailyRollingFileAppender\r\n" \"log4cplus.appender.DebugLog.MaxFileSize=%dKB\r\n" \"log4cplus.appender.DebugLog.File=%s/debug/debug.log\r\n" \"log4cplus.appender.DebugLog.Schedule=%s\r\n" \"log4cplus.appender.DebugLog.ImmediateFlush=true\r\n" \"log4cplus.appender.DebugLog.MaxBackupIndex=10\r\n" \"log4cplus.appender.DebugLog.MaxSaveDays=%d\r\n" \"log4cplus.appender.DebugLog.layout=log4cplus::PatternLayout\r\n" \"log4cplus.appender.DebugLog.layout.ConversionPattern=%s\r\n" \"log4cplus.appender.DebugLog.filters.1=log4cplus::spi::LogLevelMatchFilter\r\n" \"log4cplus.appender.DebugLog.filters.1.LogLevelToMatch=DEBUG\r\n" \"log4cplus.appender.DebugLog.filters.1.AcceptOnMatch=true\r\n" \"log4cplus.appender.DebugLog.filters.2=log4cplus::spi::DenyAllFilter\r\n" #define LOG4PLUS_LOG_CFG_ERROR "log4cplus.appender.ErrorLog=log4cplus::DailyRollingFileAppender\r\n" \"log4cplus.appender.ErrorLog.MaxFileSize=%dKB\r\n" \"log4cplus.appender.ErrorLog.File=%s/error/error.log\r\n" \"log4cplus.appender.ErrorLog.Schedule=%s\r\n" \"log4cplus.appender.ErrorLog.ImmediateFlush=true\r\n" \"log4cplus.appender.ErrorLog.MaxBackupIndex=10\r\n" \"log4cplus.appender.ErrorLog.MaxSaveDays=%d\r\n" \"log4cplus.appender.ErrorLog.layout=log4cplus::PatternLayout\r\n" \"log4cplus.appender.ErrorLog.layout.ConversionPattern=%s\r\n" \"log4cplus.appender.ErrorLog.filters.1=log4cplus::spi::LogLevelMatchFilter\r\n" \"log4cplus.appender.ErrorLog.filters.1.LogLevelToMatch=ERROR\r\n" \"log4cplus.appender.ErrorLog.filters.1.AcceptOnMatch=true\r\n" \"log4cplus.appender.ErrorLog.filters.2=log4cplus::spi::DenyAllFilter\r\n"#define LOG4PLUS_LOG_CFG_FATAL "log4cplus.appender.FatalLog=log4cplus::DailyRollingFileAppender\r\n" \"log4cplus.appender.FatalLog.MaxFileSize=%dKB\r\n" \"log4cplus.appender.FatalLog.File=%s/fatal/fatal.log\r\n" \"log4cplus.appender.FatalLog.Schedule=%s\r\n" \"log4cplus.appender.FatalLog.ImmediateFlush=true\r\n" \"log4cplus.appender.FatalLog.MaxBackupIndex=10\r\n" \"log4cplus.appender.FatalLog.MaxSaveDays=%d\r\n" \"log4cplus.appender.FatalLog.layout=log4cplus::PatternLayout\r\n" \"log4cplus.appender.FatalLog.layout.ConversionPattern=%s\r\n" \"log4cplus.appender.FatalLog.filters.1=log4cplus::spi::LogLevelMatchFilter\r\n" \"log4cplus.appender.FatalLog.filters.1.LogLevelToMatch=FATAL\r\n" \"log4cplus.appender.FatalLog.filters.1.AcceptOnMatch=true\r\n" \"log4cplus.appender.FatalLog.filters.2=log4cplus::spi::DenyAllFilter\r\n"//message pattern #define CONSOLE_TRACE_PATTERN "%m%n" //all console pattern #define CONSOLE_PATTERN "%D.%d{%q} <%-5p> at %l%n%m%n%n"//file log pattern #define SAVELOG_PATTERN "%D.%d{%q} <%-5p> at %l thread<%t>%n%m%n%n"CLog::CLog() {m_bInited = false; }CLog::~CLog() {}bool CLog::m_bInited = false; void CLog::DoLogProc(LOG_LEVEL_E level, const char* filename, const int fileline, const char* pFormat, ...) {switch (level){case FATAL_LEVEL:DO_LOGGER(log4cplus::FATAL_LOG_LEVEL, filename, fileline, pFormat, 10240);break;case ERROR_LEVEL:DO_LOGGER(log4cplus::ERROR_LOG_LEVEL, filename, fileline, pFormat, 10240);break;case BREAK_LEVEL:DO_LOGGER(log4cplus::INFO_LOG_LEVEL, filename, fileline, pFormat, 10240);break;case MESSG_LEVEL:case DEBUG_LEVEL:DO_LOGGER(log4cplus::DEBUG_LOG_LEVEL, filename, fileline, pFormat, 10240);break;case TRACE_LEVEL:DO_LOGGER(log4cplus::TRACE_LOG_LEVEL, filename, fileline, pFormat, 10240);break;default:return; }return; }void CLog::CreateCatalog(const char *logcatalog) {char cmd[256] = {0};if (access(logcatalog, F_OK) != 0){sprintf(cmd, "mkdir -p %s", logcatalog);int r = system(cmd);if (r != 0){printf("CreateCatalog system cmd:%s ret:%d fail!\n\n", cmd, r);}}char Catelog[256] = {0};//allsnprintf(Catelog, 256, "%s/all/", logcatalog);if (access(Catelog, F_OK) != 0){mkdir(Catelog, S_IRWXU|S_IRWXG|S_IRWXO);}//fatalsnprintf(Catelog, 256, "%s/fatal/", logcatalog);if (access(Catelog, F_OK) != 0){mkdir(Catelog, S_IRWXU|S_IRWXG|S_IRWXO);}//errorsnprintf(Catelog, 256, "%s/error/", logcatalog);if (access(Catelog, F_OK) != 0){mkdir(Catelog, S_IRWXU|S_IRWXG|S_IRWXO);}//breaksnprintf(Catelog, 256, "%s/info/", logcatalog);if (access(Catelog, F_OK) != 0){mkdir(Catelog, S_IRWXU|S_IRWXG|S_IRWXO);}//debugsnprintf(Catelog, 256, "%s/debug/", logcatalog);if (access(Catelog, F_OK) != 0){mkdir(Catelog, S_IRWXU|S_IRWXG|S_IRWXO);}return; }int CLog::StartSystem(const char *LogSavePath, const char *LogSaveMode, const int LogSaveDays, const int LogSaveSizes) {CreateCatalog(LogSavePath);char LogCfgConsoleAll[2048] = {0};snprintf(LogCfgConsoleAll, 2048, LOG4PLUS_CONSOLE_ALL, CONSOLE_PATTERN);char LogCfgConsoleMsg[2048] = {0};snprintf(LogCfgConsoleMsg, 2048, LOG4PLUS_CONSOLE_TRACE, CONSOLE_TRACE_PATTERN);//console all and msgchar LogCfgConsole[4096] = {0};snprintf(LogCfgConsole, 4096, "%s%s", LogCfgConsoleAll, LogCfgConsoleMsg);char LogCfgAll[2048] = {0};snprintf(LogCfgAll, 2048, LOG4PLUS_LOG_CFG_ALL, LogSaveSizes, LogSavePath, LogSaveMode, LogSaveDays, SAVELOG_PATTERN);char LogCfgInfo[2048] = {0};snprintf(LogCfgInfo, 2048, LOG4PLUS_LOG_CFG_INFO, LogSaveSizes, LogSavePath, LogSaveMode, LogSaveDays, SAVELOG_PATTERN);char LogCfgDebug[2048] = {0};snprintf(LogCfgDebug, 2048, LOG4PLUS_LOG_CFG_DEBUG, LogSaveSizes, LogSavePath, LogSaveMode, LogSaveDays, SAVELOG_PATTERN);char LogCfgError[2048] = {0};snprintf(LogCfgError, 2048, LOG4PLUS_LOG_CFG_ERROR, LogSaveSizes, LogSavePath, LogSaveMode, LogSaveDays, SAVELOG_PATTERN);char LogCfgFatal[2048] = {0};snprintf(LogCfgFatal, 2048, LOG4PLUS_LOG_CFG_FATAL, LogSaveSizes, LogSavePath, LogSaveMode, LogSaveDays, SAVELOG_PATTERN);char Log4LogCfg[8192] = {0};snprintf(Log4LogCfg, 8192, "%s%s%s%s%s%s%s", LOG4PLUS_LOG_CFG_LOGGER, LogCfgConsole, LogCfgAll, LogCfgInfo, LogCfgDebug, LogCfgError, LogCfgFatal);if (strlen(Log4LogCfg) <= 0){printf("Get Log4plus log config failed!\n");return -1;}log4cplus::PropertyConfigurator::doConfigure(Log4LogCfg);log4cplus::Logger _logger = log4cplus::Logger::getRoot();m_bInited = true;return 0; }void CLog::StopSystem() {m_bInited = false; }
使用方法:
//start log if (-1 == LOG_START("./log", "DAILY",30,20000)){printf("Log4plus start failed!\n");return -1;}DBGPrint(M_None, BREAK_LEVEL, "Main Process Start!");LOG_STOP();
这篇关于视频监控安防平台-基于log4cplus的log日志封装(linux版本)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!