-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
78 lines (69 loc) · 2.47 KB
/
Logger.cpp
File metadata and controls
78 lines (69 loc) · 2.47 KB
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
#include <iostream>
#include "Logger.h"
#include "TimeStamp.h"
/*
- LOG_INFO 等宏能不能优化
- 使用 MUDUO_DEBUG 宏控制 DEBUG 日志信息的输出,考虑使用配置文件
*/
#define LOG_INFO(logMsgFormat, ...) \
do { \
mudo::Logger &logger = Logger::getInstance(); \
logger.setLogLevel(INFO); \
char buf[1024] = {0}; \
snprintf(buf, 1024, logMsgFormat, ##__VA_ARGS__); \
logger.log(buf) \
} while (0);
#define LOG_ERROR(logMsgFormat, ...) \
do { \
mudo::Logger &logger = Logger::getInstance(); \
logger.setLogLevel(ERROR); \
char buf[1024] = {0}; \
snprintf(buf, 1024, logMsgFormat, ##__VA_ARGS__); \
logger.log(buf) \
} while (0);
#define LOG_FATAL(logMsgFormat, ...) \
do { \
mudo::Logger &logger = Logger::getInstance(); \
logger.setLogLevel(FATAL); \
char buf[1024] = {0}; \
snprintf(buf, 1024, logMsgFormat, ##__VA_ARGS__); \
logger.log(buf) \
} while (0);
#ifdef MUDUO_DEBUG
#define LOG_DEBUG(logMsgFormat, ...) \
do { \
mudo::Logger &logger = Logger::getInstance(); \
logger.setLogLevel(DEBUG); \
char buf[1024] = {0}; \
snprintf(buf, 1024, logMsgFormat, ##__VA_ARGS__); \
logger.log(buf) \
} while (0);
#endif
muduo::Logger& muduo::Logger::getInstance() {
// 这种写法还是线程安全的
static Logger logger;
return logger;
}
void muduo::Logger::setLogLevel(LogLevel level) {
m_loglevel = level;
}
// [日志级别] time: msg
void muduo::Logger::log(std::string msg) {
switch(m_loglevel) {
case LogLevel::INFO:
std::cout << "[INFO]";
break;
case LogLevel::FATAL:
std::cout << "[FATAL]";
break;
case LogLevel::ERROR:
std::cout << "[ERROR]";
break;
case LogLevel::DEBUG:
std::cout << "[DEBUG]";
break;
default:
break;
}
std::cout << muduo::TimeStamp::now().toString() << ": " << msg << std::endl;
}