-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.h
71 lines (62 loc) · 1.82 KB
/
Logger.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
#pragma once
#include<assert.h>
#include<string>
#include"noncopyable.h"
// LOG_INFO("%s %d", arg1, arg2)
#define LOG_INFO(LogmsgFormat, ...) \
do { \
char buf[1024] = {0}; \
Logger &logger = Logger::instance(); \
logger.setLogLevel(INFO); \
snprintf(buf, 1024, LogmsgFormat, ##__VA_ARGS__); \
logger.log(buf); \
} while(0)
#define LOG_ERROR(LogmsgFormat, ...) \
do { \
char buf[1024] = {0}; \
Logger &logger = Logger::instance(); \
logger.setLogLevel(ERROR); \
snprintf(buf, 1024, LogmsgFormat, ##__VA_ARGS__); \
logger.log(buf); \
} while(0)
#define LOG_FATAL(LogmsgFormat, ...) \
do { \
char buf[1024] = {0}; \
Logger &logger = Logger::instance(); \
logger.setLogLevel(FATAL); \
snprintf(buf, 1024, LogmsgFormat, ##__VA_ARGS__); \
logger.log(buf); \
exit(-1); \
} while(0)
#ifdef MUDEGUB
#define LOG_DEBUG(LogmsgFormat, ...) \
do { \
char buf[1024] = {0}; \
Logger &logger = Logger::instance(); \
logger.setLogLevel(DEBUG); \
snprintf(buf, 1024, LogmsgFormat, ##__VA_ARGS__); \
logger.log(buf); \
} while(0)
#else
#define LOG_DEBUG(LogmsgFormat, ...)
#endif
// 定义日志级别 INFO ERROR FATAL DEBUG
enum LogLevel {
INFO, // 普通信息
ERROR, // 错误信息
FATAL, // core信息
DEBUG, // 调试信息
};
// 输出一个日志类
class Logger : noncopyable {
public:
// 获取日志类唯一的实例对象(懒汉单例模式)
static Logger& instance();
// 设置日志级别
void setLogLevel(int level);
// 写日志
void log(std::string msg);
private:
int logLevel_;
Logger() {}
};