123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #ifndef _LOG4CPP_H_
- #define _LOG4CPP_H_
- #include "Log4CPP/Category.hh"
- #include "Log4CPP/FileAppender.hh"
- #include "Log4CPP/PatternLayout.hh"
- #include "Log4CPP/PropertyConfigurator.hh"
- #include "Log4CPP/OstreamAppender.hh"
- #include "Log4CPP/RollingFileAppender.hh"
- #include <string>
- #include <cstdarg>
- #include <vector>
- #include <sstream>
- #include <iomanip> // 添加头文件
- // 优先级
- #define COUNT_PRITY log4cpp::Priority::INFO; // 控制台
- #define LOG_PRITY log4cpp::Priority::DEBUG; // 本地文件
- struct LogConfig {
- std::string root_level = "DEBUG";
- std::string log_dir = "logs";
- std::string history_dir = "logs/history";
- size_t max_file_size = 10; // MB
- int max_backups = 5;
- // 简化后的默认格式
- std::string file_pattern = "%d{%Y-%m-%d %H:%M:%S} [%p] %m%n";
- std::string console_pattern = "%d{%H:%M:%S} [%p] %m%n";
- };
- /*采用单例模式设计,包含两个category对象,一个负责输出到屏幕的信息,一个负责记录到日志的信息,
- 通过设置优先级差别,可以实现所有信息都记录在日志中,遇到error及以上的信息时打印到屏幕上*/
- class Log4CPP {
- private:
- Log4CPP(bool b) {
- outToScreen = b;
- }
- ~Log4CPP() {}
- static Log4CPP* instance;
- // 类型转换函数
- template <typename T>
- static std::string toString(const T& value);
- static std::string toString(const void* value);
- static std::string toString(bool value);
- bool outToScreen;//是否输出日志信息到屏幕
- static std::string _screenInfo;//屏幕日志信息
- static std::string _logName;//文件日志名称
- static log4cpp::Category& logCat;
- static log4cpp::Category& coutCat;
- static log4cpp::FileAppender* logFile;//文件日志输入
- static log4cpp::OstreamAppender* logScreen;//屏幕日志输入
- static log4cpp::RollingFileAppender* rollLogFile; /* 回卷用这个 */
- static log4cpp::Priority::PriorityLevel logPri;//文件日志优先级
- static log4cpp::Priority::PriorityLevel coutPri;//屏幕日志优先级
- static log4cpp::PatternLayout* logLayout;//日志布局
- static log4cpp::PatternLayout* screenLayout;//屏幕布局
- static log4cpp::PatternLayout* logLayout2; /* 回卷用这个 */
- // 日志配置
- static LogConfig config;
- private:
- // 返回当前年月日时分秒
- static std::string getCurrentTime(std::string& year, std::string& month, std::string& day, std::string& hour, std::string& min, std::string& sec);
- static std::string formatMessage(const char* format, va_list args);
- // 创建目录
- static bool createDirectory(const std::string& path);
- // 解析日志级别
- static log4cpp::Priority::PriorityLevel parseLogLevel(const std::string& level);
- public:
- // 初始化日志配置信息
- static bool init(const std::string& configFile = "log_config.xml",
- std::string logName = "time",
- bool toScreen = false);
- // 加载配置文件
- static bool loadConfig(const std::string& configFile);
- //获取日志函数,默认参数选择是否输出到屏幕
- static Log4CPP* getLog(bool toScreen = false);
- //销毁日志对象
- static void destoryLog();
- //设置日志记录优先级
- static void setPri(log4cpp::Priority::PriorityLevel coutLevel,
- log4cpp::Priority::PriorityLevel logLevel);
- //记录日志,调用参数 __LINE__ ,__FUNCTION__
- void warn(int line, const char* function, const char* format, ...);
- void error(int line, const char* function, const char* format, ...);
- void debug(int line, const char* function, const char* format, ...);
- void info(int line, const char* function, const char* format, ...);
- };
- //为避免每次调用都要填写参数__LINE__和__FUNCTION__,可以使用带参数的宏定义
- // 修改宏定义支持多个参数
- #define LogWARN(tmpl, ...) Log4CPP::getLog()->warn(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
- #define LogINFO(tmpl, ...) Log4CPP::getLog()->info(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
- #define LogERROR(tmpl, ...) Log4CPP::getLog()->error(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
- #define LogDEBUG(tmpl, ...) Log4CPP::getLog()->debug(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
- #endif // MY_LOG_H
|