-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
142 lines (114 loc) · 2.84 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @file log.h
* @brief 日志模块封装
* @author shawn
* @date 2024-05-23
*/
#ifndef __CORO_LOG_H__
#define __CORO_LOG_H__
#include <list>
#include <memory>
#include <string>
#include <vector>
#include "mutex.h"
#include "singleton.h"
#define CORO_LOG_LEVEL(logger, level) \
if (logger->getLevel() <= level) \
coro::LogEventWrap( \
coro::LogEvent::ptr(new coro::LogEvent( \
logger, level, __FILE__, __LINE__, 0, coro::GetThreadId(), \
coro::GetFiberId(), time(0), coro::Thread::GetName()))) \
.getSS()
#define CORO_LOG_DEBUG(logger) CORO_LOG_LEVEL(logger, coro::LogLevel::DEBUG)
#define CORO_LOG_ERROR(logger) CORO_LOG_LEVEL(logger, coro::LogLevel::ERROR)
/**
* @brief 获取name的日志器
*/
#define CORO_LOG_NAME(name) coro::LoggerMgr::GetInstance()->getLogger(name)
/**
* @brief 获取主日志器
*/
#define CORO_LOG_ROOT() coro::LoggerMgr::GetInstance()->getRoot()
namespace coro {
class Logger;
class LoggerManager;
/**
* @brief 日志级别
*/
class LogLevel {
public:
enum Level {
UNKNOW = 0,
DEBUG = 1,
INFO = 2,
WARN = 3,
ERROR = 4,
FATAL = 5
};
/**
* @brief 将日志级别转成文本输出
* @param[in] level 日志级别
*/
static const char* ToString(LogLevel::Level level);
/**
* @brief 将文本转换成日志级别
* @param[in] str 日志级别文本
*/
static LogLevel::Level FromString(const std::string& str);
};
/**
* @brief 日志事件
*/
class LogEvent {
public:
typedef std::shared_ptr<LogEvent> ptr;
};
class LogFormatter {
public:
typedef std::shared_ptr<LogFormatter> ptr;
};
class LogAppender {
friend class Logger;
public:
typedef std::shared_ptr<LogAppender> ptr;
typedef SpinLock MutexType;
};
/**
* @brief 日志器
*/
class Logger {
friend class LoggerManager;
public:
typedef std::shared_ptr<Logger> ptr;
typedef SpinLock MutexType;
Logger(const std::string& name = "root");
void log(LogLevel::Level level, LogEvent::ptr event);
void debug(LogEvent::ptr event);
private:
// 日志名称
std::string m_name;
// 日志级别
LogLevel::Level m_level;
// Mutex
MutexType m_mutex;
// 目标日志集合
std::list<LogAppender::ptr> m_appenders;
// 日志格式器
LogFormatter::ptr m_formatter;
// 主日志器
Logger::ptr m_root;
};
class StdoutLogAppender {};
class FileLogAppender {};
/**
* @brief 日志器管理类
*/
class LoggerManager {
public:
typedef SpinLock MutexType;
LoggerManager();
};
// 日志器管理类单例模式
typedef coro::Singleton<LoggerManager> LoggerMgr;
} // namespace coro
#endif