Log4CPP.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef _LOG4CPP_H_
  2. #define _LOG4CPP_H_
  3. #include "Log4CPP/Category.hh"
  4. #include "Log4CPP/FileAppender.hh"
  5. #include "Log4CPP/PatternLayout.hh"
  6. #include "Log4CPP/PropertyConfigurator.hh"
  7. #include "Log4CPP/OstreamAppender.hh"
  8. #include "Log4CPP/RollingFileAppender.hh"
  9. #include <string>
  10. #include <cstdarg>
  11. #include <vector>
  12. #include <sstream>
  13. #include <iomanip> // 添加头文件
  14. // 优先级
  15. #define COUNT_PRITY log4cpp::Priority::INFO; // 控制台
  16. #define LOG_PRITY log4cpp::Priority::DEBUG; // 本地文件
  17. struct LogConfig {
  18. std::string root_level = "DEBUG";
  19. std::string log_dir = "logs";
  20. std::string history_dir = "logs/history";
  21. size_t max_file_size = 10; // MB
  22. int max_backups = 5;
  23. // 简化后的默认格式
  24. std::string file_pattern = "%d{%Y-%m-%d %H:%M:%S} [%p] %m%n";
  25. std::string console_pattern = "%d{%H:%M:%S} [%p] %m%n";
  26. };
  27. /*采用单例模式设计,包含两个category对象,一个负责输出到屏幕的信息,一个负责记录到日志的信息,
  28. 通过设置优先级差别,可以实现所有信息都记录在日志中,遇到error及以上的信息时打印到屏幕上*/
  29. class Log4CPP {
  30. private:
  31. Log4CPP(bool b) {
  32. outToScreen = b;
  33. }
  34. ~Log4CPP() {}
  35. static Log4CPP* instance;
  36. // 类型转换函数
  37. template <typename T>
  38. static std::string toString(const T& value);
  39. static std::string toString(const void* value);
  40. static std::string toString(bool value);
  41. bool outToScreen;//是否输出日志信息到屏幕
  42. static std::string _screenInfo;//屏幕日志信息
  43. static std::string _logName;//文件日志名称
  44. static log4cpp::Category& logCat;
  45. static log4cpp::Category& coutCat;
  46. static log4cpp::FileAppender* logFile;//文件日志输入
  47. static log4cpp::OstreamAppender* logScreen;//屏幕日志输入
  48. static log4cpp::RollingFileAppender* rollLogFile; /* 回卷用这个 */
  49. static log4cpp::Priority::PriorityLevel logPri;//文件日志优先级
  50. static log4cpp::Priority::PriorityLevel coutPri;//屏幕日志优先级
  51. static log4cpp::PatternLayout* logLayout;//日志布局
  52. static log4cpp::PatternLayout* screenLayout;//屏幕布局
  53. static log4cpp::PatternLayout* logLayout2; /* 回卷用这个 */
  54. // 日志配置
  55. static LogConfig config;
  56. private:
  57. // 返回当前年月日时分秒
  58. static std::string getCurrentTime(std::string& year, std::string& month, std::string& day, std::string& hour, std::string& min, std::string& sec);
  59. static std::string formatMessage(const char* format, va_list args);
  60. // 创建目录
  61. static bool createDirectory(const std::string& path);
  62. // 解析日志级别
  63. static log4cpp::Priority::PriorityLevel parseLogLevel(const std::string& level);
  64. public:
  65. // 初始化日志配置信息
  66. static bool init(const std::string& configFile = "log_config.xml",
  67. std::string logName = "time",
  68. bool toScreen = false);
  69. // 加载配置文件
  70. static bool loadConfig(const std::string& configFile);
  71. //获取日志函数,默认参数选择是否输出到屏幕
  72. static Log4CPP* getLog(bool toScreen = false);
  73. //销毁日志对象
  74. static void destoryLog();
  75. //设置日志记录优先级
  76. static void setPri(log4cpp::Priority::PriorityLevel coutLevel,
  77. log4cpp::Priority::PriorityLevel logLevel);
  78. //记录日志,调用参数 __LINE__ ,__FUNCTION__
  79. void warn(int line, const char* function, const char* format, ...);
  80. void error(int line, const char* function, const char* format, ...);
  81. void debug(int line, const char* function, const char* format, ...);
  82. void info(int line, const char* function, const char* format, ...);
  83. };
  84. //为避免每次调用都要填写参数__LINE__和__FUNCTION__,可以使用带参数的宏定义
  85. // 修改宏定义支持多个参数
  86. #define LogWARN(tmpl, ...) Log4CPP::getLog()->warn(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
  87. #define LogINFO(tmpl, ...) Log4CPP::getLog()->info(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
  88. #define LogERROR(tmpl, ...) Log4CPP::getLog()->error(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
  89. #define LogDEBUG(tmpl, ...) Log4CPP::getLog()->debug(__LINE__, __FUNCTION__, tmpl, ##__VA_ARGS__)
  90. #endif // MY_LOG_H