C++:单例封装log4cpp

C++:单例封装log4cpp

log4cpp是一个开源C++日志类库,可以方便使用日志和调试。

官方主页

安装

1
2
3
4
5
6
7
8
9
#1.官网下载压缩包
#2.解压
tar zxvf log4cpp-x.x.x.tar.gz
cd log4cpp/
#安装
./configure
make
make check
sudo make install

默认lib库路径:/usr/local/lib/

默认头文件路径:/usr/local/include/log4cpp

编译参数:-llog4cpp -lpthread

若提示找不到库文件:

1
2
sudo echo 'include /usr/local/lib' >> /etc/ld.so.conf
sudo ldconfig #更新库文件的缓存

封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef __LOGGER_H__
#define __LOGGER_H__

#include <log4cpp/Category.hh>

#include <string>
using std::string;


class Mylogger
{
public:
static Mylogger * getInstance();
static void destroy();

template <class... Args>
void error(Args... args)
{ _cat.error(args...); }

void error(const char * msg);

template <class... Args>
void info(const char * msg, Args... args)
{ _cat.info(msg, args...); }

void info(const char * msg);

template <class... Args>
void warn(const char * msg, Args... args)
{ _cat.warn(msg, args...); }

void warn(const char * msg);

template <class... Args>
void debug(const char * msg, Args... args)
{ _cat.debug(msg, args...); }

void debug(const char * msg);
log4cpp::Category & getCategory(){ return _cat;}

//void setPriority();

private:
Mylogger();
~Mylogger();

private:
static Mylogger * _pInstance;
log4cpp::Category & _cat;
};


#define prefix(msg) string("[").append(__FILE__)\
.append(":").append(__FUNCTION__)\
.append(":").append(std::to_string(__LINE__))\
.append("] ").append(msg).c_str()

#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "include/logger.h"

#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/RollingFileAppender.hh>

#include <iostream>
using std::cout;
using std::endl;

Mylogger * Mylogger::_pInstance = nullptr;

Mylogger * Mylogger::getInstance()
{
if(!_pInstance) {
_pInstance = new Mylogger();
}
return _pInstance;
}

void Mylogger::destroy()
{
if(_pInstance)
delete _pInstance;
}

Mylogger::Mylogger()
: _cat(log4cpp::Category::getRoot().getInstance("logger"))
{
using namespace log4cpp;
PatternLayout * ptn1 = new PatternLayout();
ptn1->setConversionPattern("%d:%p %c %x: %m%n");
PatternLayout * ptn2 = new PatternLayout();
ptn2->setConversionPattern("%d:%p %c %x: %m%n");

OstreamAppender * os = new OstreamAppender("os", &cout);
os->setLayout(ptn1);

RollingFileAppender * rollfile = new RollingFileAppender(
"rollfile",
"wd.log",
10 * 1024 * 1024,
10);
rollfile->setLayout(ptn2);

_cat.addAppender(os);
_cat.addAppender(rollfile);
_cat.setPriority(Priority::INFO);
}

Mylogger::~Mylogger()
{
log4cpp::Category::shutdown();
}


void Mylogger::error(const char * msg)
{
_cat.error(msg);
}

void Mylogger::info(const char * msg)
{
_cat.info(msg);
}

void Mylogger::warn(const char * msg)
{
_cat.warn(msg);
}

void Mylogger::debug(const char * msg)
{
_cat.debug(msg);
}

参考资料