forked from nafur/l3pp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.h
201 lines (166 loc) · 5.15 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* @file logger.h
*
* Defines the base Logger class
*/
#pragma once
#include <vector>
#include <sstream>
namespace l3pp {
/**
* LogStream is a logger object that can be streamed into, writing an entry
* to the logger associated upon destruction. Instances of this classer are
* returned by Logger log() functions, so they can be used as such:
* logger->debug() << "Message";
*/
class LogStream {
friend class Logger;
Logger& logger;
LogLevel level;
EntryContext context;
mutable std::ostringstream stream;
LogStream(Logger& logger, LogLevel level, EntryContext context) :
logger(logger), level(level), context(context)
{
}
LogStream(const LogStream&) = delete;
LogStream& operator=(const LogStream&) = delete;
public:
LogStream(LogStream&& other) :
logger(other.logger), level(other.level), context(std::move(other.context))/*,
stream(std::move(other.stream))*/
{
stream.str(other.stream.str());
}
~LogStream();
template<typename T>
friend LogStream const& operator<<(LogStream const& stream, T const& val);
friend LogStream const& operator<<(LogStream const& stream, std::ostream& (*F)(std::ostream&));
};
/**
* Main logger class. Keeps track of all Logger instances, and can be used to
* log various messages. Before the logging library is used, make sure to
* call Logger::initialize(). Loggers are hierarchically nested, by means of
* names separated by a period. All loggers are a (indirect) child of the root
* logger, see Logger::getRootLogger() and Logger::getLogger().
* A logger is associated with a LogLevel. Any entry with a level below this
* level will be filtered out. A LogLevel of INHERIT means the parent log
* level will be compared against instead.
* A logger can be associated with 1 or more Sinks. A log entry is printed to
* each associated sink. If the Logger is set additive (see getAdditive(),
* setAdditive()) parent sinks are logged to as well (by default true).
* Logging can be performed either as a single string message, or by using a
* stream. The latter requires the end() method to be called before the entry
* is logged. For convenience, various logging macros are defined at the end
* of this header.
*/
class Logger {
friend class Formatter;
typedef std::shared_ptr<Logger> LogPtr;
LogPtr parent;
std::string name;
LogLevel level;
std::vector<SinkPtr> sinks;
bool additive;
// Logger constructors are private
Logger() : parent(nullptr), name(""), level(LogLevel::DEFAULT),
additive(true)
{
}
Logger(std::string const& name, LogPtr parent) : parent(parent), name(name),
level(LogLevel::INHERIT), additive(true)
{
}
void logEntry(EntryContext const& context, std::string const& msg);
public:
void addSink(SinkPtr sink) {
sinks.push_back(sink);
}
void removeSink(SinkPtr sink);
void setLevel(LogLevel level) {
if (level == LogLevel::INHERIT && !parent) {
return;
}
this->level = level;
}
LogLevel getLevel() const {
if (level == LogLevel::INHERIT) {
return parent->getLevel();
}
return level;
}
std::string const& getName() const {
return name;
}
bool getAdditive() const {
return additive;
}
void setAdditive(bool additive) {
this->additive = additive;
}
void log(LogLevel level, std::string const& msg, EntryContext context = EntryContext());
void trace(std::string const& msg, EntryContext context = EntryContext()) {
log(LogLevel::TRACE, msg, context);
}
void debug(std::string const& msg, EntryContext context = EntryContext()) {
log(LogLevel::DEBUG, msg, context);
}
void info(std::string const& msg, EntryContext context = EntryContext()) {
log(LogLevel::INFO, msg, context);
}
void warn(std::string const& msg, EntryContext context = EntryContext()) {
log(LogLevel::WARN, msg, context);
}
void error(std::string const& msg, EntryContext context = EntryContext()) {
log(LogLevel::ERR, msg, context);
}
void fatal(std::string const& msg, EntryContext context = EntryContext()) {
log(LogLevel::FATAL, msg, context);
}
LogStream log(LogLevel level, EntryContext context = EntryContext());
LogStream trace(EntryContext context = EntryContext()) {
return log(LogLevel::TRACE, context);
}
LogStream debug(EntryContext context = EntryContext()) {
return log(LogLevel::DEBUG, context);
}
LogStream info(EntryContext context = EntryContext()) {
return log(LogLevel::INFO, context);
}
LogStream warn(EntryContext context = EntryContext()) {
return log(LogLevel::WARN, context);
}
LogStream error(EntryContext context = EntryContext()) {
return log(LogLevel::ERR, context);
}
LogStream fatal(EntryContext context = EntryContext()) {
return log(LogLevel::FATAL, context);
}
static void initialize();
static void deinitialize();
static LogPtr getRootLogger();
static LogPtr getLogger(LogPtr logger) {
return logger;
}
static LogPtr getLogger(std::string name);
};
typedef std::shared_ptr<Logger> LogPtr;
/**
* Helper class to initialize l3pp. Call get() will
* retrieve the singleton, which will initialize the
* library.
*/
class Initializer {
Initializer() {
Logger::initialize();
}
public:
~Initializer() {
Logger::deinitialize();
}
static Initializer const& get(){
static Initializer instance;
return instance;
}
};
}