一、实现log4cpp的封装,使其可以像printf一样使用,测试用例如下:

 思路:使用可变模板参数,最终达到的效果是在使用 LogInfo、LogError、LogWarn、LogDebug时候可以传递任意类型、任意个数的参数进行, 按照自己的需要进行日志记录。

1 void test() 
2 {
3     int number = 100;
4     const char *pstr = "hello, log4cpp";
5     LogInfo("This is an info message. number = %d, str = %s\n", number, pstr);
6     LogError("This is an error message. number = %d, str = %s\n", number, pstr);
7     LogWarn("This is a warn message. number = %d, str = %s\n", number, pstr);
8     LogDebug("This is a debug message. number = %d, str = %s\n", number, pstr);
9 }

二、实现(第一版)

 1 #include <iostream>
 2 #include<log4cpp/PatternLayout.hh>
 3 #include<log4cpp/OstreamAppender.hh>
 4 #include<log4cpp/FileAppender.hh>
 5 #include<log4cpp/Priority.hh>
 6 #include<log4cpp/Category.hh>
 7 
 8 using std::cout;
 9 using std::endl;
10 using namespace log4cpp;
11 
12 class MyLogger{
13 public:
14     static MyLogger* getInstance(){
15         if(m_pInstance == nullptr){
16             m_pInstance = new MyLogger();
17         }
18         return m_pInstance;
19     }
20 
21     static void destroy(){
22         if(m_pInstance){
23             delete  m_pInstance;
24             m_pInstance = nullptr;
25         }
26     }
27 
28     template <typename ...T>
29     void warn(const char* msg, const T& ...args){
30             mycat.warn(msg, args...);
31     }
32 
33     template <typename ...T>
34     void debug(const char* msg, const T& ...args){
35             mycat.debug(msg, args...);
36     }
37 
38     template <typename ...T>
39     void info(const char* msg, const T& ...args){
40             mycat.info(msg, args...);
41     }
42 
43     template <typename ...T>
44     void error(const char* msg, const T& ...args){
45             mycat.error(msg, args...);
46     }
47 
48 private:
49     MyLogger():mycat(Category::getRoot().getInstance("myCat")){
50         cout << "Mylogger()" << endl;
51 
52         PatternLayout *ppl1 = new PatternLayout();
53         ppl1->setConversionPattern("%d %c [%p] %m%n");
54 
55         PatternLayout *ppl2 = new PatternLayout();
56         ppl2->setConversionPattern("%d %c [%p] %m%n");
57 
58         OstreamAppender *poa = new OstreamAppender("OstreamAppender", &cout);
59         poa->setLayout(ppl1);
60 
61         FileAppender *pfa = new FileAppender("FileAppender", "wd.txt");
62         pfa->setLayout(ppl2);
63 
64          mycat.setPriority(Priority::DEBUG);
65          mycat.addAppender(poa);
66          mycat.addAppender(pfa);
67     }
68 
69     ~MyLogger(){
70          cout << "~Mylogger()" << endl;
71          Category::shutdown();
72     }
73 
74 
75 
76 private:
77     static MyLogger *m_pInstance;
78     Category &mycat;
79 };
80 
81 MyLogger *MyLogger::m_pInstance = nullptr;
82 
83 void test(){
84     int number = 10;
85     const char *pstr = "hello, log4cpp";
86     MyLogger::getInstance()->warn("This is a warn message. number = %d, str = %s\n", number, pstr);
87     MyLogger::getInstance()->error("This is a error message. number = %d, str = %s\n", number, pstr);
88     MyLogger::getInstance()->debug("This is a debug message. number = %d, str = %s\n", number, pstr);
89     MyLogger::getInstance()->info("This is a info message. number = %d, str = %s\n", number, pstr);
90 }
91 
92 int main(int argc, char **argv)
93 {
94 
95     test();
96     return 0;
97 }

结果:

2023-02-01 19:32:05,040 myCat [WARN] This is a warn message. number = 10, str = hello, log4cpp

2023-02-01 19:32:05,040 myCat [ERROR] This is a error message. number = 10, str = hello, log4cpp

2023-02-01 19:32:05,040 myCat [DEBUG] This is a debug message. number = 10, str = hello, log4cpp

2023-02-01 19:32:05,040 myCat [INFO] This is a info message. number = 10, str = hello, log4cpp