首页 > 编程语言 >C++ Day11 使用单例模式封装log4cpp

C++ Day11 使用单例模式封装log4cpp

时间:2023-02-01 19:55:14浏览次数:47  
标签:pstr const 单例 number C++ Day11 str message log4cpp

一、实现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

 

标签:pstr,const,单例,number,C++,Day11,str,message,log4cpp
From: https://www.cnblogs.com/YongSir/p/17083999.html

相关文章

  • 闲散随笔的C++教程(一)——绪
    绪在猴子的世界中,编程语言是很多的。你不一定非要选择C++——没错,这并不是一门非常好学的语言。所以当你看到这个开头时就后悔,还是来得及的。但是看样子你是不准备后悔,或......
  • 递归先序输入构造一颗二叉树并输出并求从根结点出发的最大带权和 (c++)
    #include<iostream>#include<cstdio>usingnamespacestd;typedefstructBiTNode//一颗二叉树的结构体{intdata;structBiTNode*lchild,*rchiild;}BiTNode,......
  • C语言&C++
    C语言和C++中都有结构的概念,但是在C语言中结构只有成员变量,而没成员方法,而在C++中结构中,它可以有自己的成员变量和成员函数。但是在C语言中结构的成员是公共的,不管什么人想......
  • C++ 信奥赛 1077:统计满足条件的4位数
    1077:统计满足条件的4位数【题目描述】给定若干个四位数,求出其中满足以下条件的数的个数:个位数上的数字减去千位数上的数字,再减去百位数上的数字,再减去十位数上的数字......
  • C++ malloc下申请内存途径
    之前就因为glibc的free不一定会将内存归还OS,导致OOM,造成了上千万损失,当时心态差点崩了五年前的时候,因为OOM导致进程无故被kill掉,也没有coredump,分析了好久,最后发现是因为......
  • C/C++ 文件IO 拷贝文件,将二进制文件写为十六进制
    查看代码 #include<fstream>#include<iostream>#include<iomanip>//#include"flow.h"unsignedcharbuf[2048];unsignedcharflow[10];voidread_f(){......
  • 【八大数据排序法】选择排序法的图形理解和案例实现 | C++
    第十五章选择排序法:::hljs-center目录第十五章选择排序法●前言●认识排序●一、选择排序法是什么?1.简要介绍2.图形理解3.算法分析●二、案例实现1.......
  • [快速学]C/C++编译器
    编译器谁维护平台版权VisualC++Microsofthttps://visualstudio.microsoft.com/MicrosoftWindows有免费版GCCCGNUhttps://www.gnu.org/多平台GP......
  • C++ traits 萃取的一些简单理解
    摘取自<effectivec++>  ......
  • ua5.4源码剖析:三. C++与Lua相互调用
    概述从本质上来看,其实说是不存在所谓的C++与lua的相互调用。lua是运行在C上的,简单来说lua的代码会被编译成字节码在被C语言的语法运行。在C++调用lua时,其实是解释运行lua......