-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
81 lines (68 loc) · 2.32 KB
/
log.h
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
77
78
79
80
81
/*=============================================================================
# Filename : log.h
# Description : 日志文件类
# Author : chenqingming [email protected]
# create : 2015-03-05 15:29
# Last modified : 2015-03-05 15:29
=============================================================================*/
#include <string>
#include <boost/lockfree/queue.hpp>
#include <boost/pool/pool.hpp>
#include <boost/atomic.hpp>
class SingletonLog
{
public:
#define MAX_MSG_LENGTH 1024 //单个消息最大长度
enum LOG_LEVEL
{
FATAL = 0,
ERROR = 1,
WARN = 2,
INFO = 3,
DEBUG = 4,
TRACE = 5
};
public:
static SingletonLog *get_instance();
//初始化日志,试用期前必须调用且只调用一次该函数。rotate_size 单位:M
bool open_log(const std::string& log_path, LOG_LEVEL level, uint32_t rotate_size = 0);
//关闭日志
void close_log();
//将日志消息写到缓冲队列,可能阻塞。
void write_log_to_queue(LOG_LEVEL log_level, const char *fmt, ...);
//取出缓冲队列中的消息,写到日志文件。
bool write_log_to_file();
private:
//关闭就文件,打开新的文件。
void rotate();
inline static const char *get_level_name(LOG_LEVEL level);
private:
SingletonLog();
~SingletonLog();
SingletonLog(const SingletonLog&);
SingletonLog& operator= (const SingletonLog&);
private:
static SingletonLog *_instance;
FILE *_fp;
LOG_LEVEL _log_level;
uint64_t _curr_size;
uint64_t _rotate_size;
std::string _base_file_path;
boost::lockfree::queue<char *> _msg_queue;
boost::pool<> _msg_pool;
boost::atomic<bool> is_done;
class garbo
{
public:
~garbo()
{
if (SingletonLog::_instance != NULL){
delete SingletonLog::_instance;
SingletonLog::_instance = NULL;
}
}
};
static garbo _garbo;
};
#define log_debug(fmt, args...) \
SingletonLog::get_instance()->write_log_to_queue(SingletonLog::DEBUG, fmt, ##args)