forked from funkey/util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.h
executable file
·322 lines (202 loc) · 5.79 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* $URL$
* $Rev$
* $Author: s2946182 $
* $Date: 2007/11/12 15:03:42 $
* $Id: Logger.hh,v 1.1 2007/11/12 15:03:42 s2946182 Exp $
*/
#ifndef __LOGGER_HH
#define __LOGGER_HH
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <config.h>
#define LOG_ERROR(channel) if (channel.getLogLevel() >= logger::Error) channel(logger::error)
#define LOG_USER(channel) if (channel.getLogLevel() >= logger::User) channel(logger::user)
#ifdef ENABLE_DEBUG_LOGGING
#define LOG_DEBUG(channel) if (channel.getLogLevel() >= logger::Debug) channel(logger::debug)
#define LOG_ALL(channel) if (channel.getLogLevel() >= logger::All) channel(logger::all)
#else
#define LOG_DEBUG(channel) if (false) channel(logger::debug)
#define LOG_ALL(channel) if (false) channel(logger::all)
#endif
namespace logger {
enum LogLevel
{
Global = 0,
Quiet, // no output at all - no errors either
Error, // print errors only - no user output
User, // output for the user - most suitable setting
Debug, // output for developer - give detailed information
// about what's going on
All // maximum verbosity - everything that's to much
// for debug-output
};
/**
* Stream manipulators.
*/
enum Manip {
delline // delete the content of the current line
};
// forward declaration
class LogChannel;
class Logger : public std::ostream {
public:
Logger(std::streambuf* streamBuffer, const std::string& prefix);
Logger(Logger& logger, const std::string& prefix);
Logger& operator=(const Logger& logger);
static void showChannelPrefix(bool show) {
_showChannelPrefix = show;
}
static void showThreadId(bool show) {
_showThreadId = show;
}
template <typename T>
Logger& operator<<(T* t) {
getBuffer() << t;
return *this;
}
template <typename T>
Logger& operator<<(const T& t) {
getBuffer() << t;
return *this;
}
Logger& operator<<(Manip op) {
if (op == delline) {
{
boost::mutex::scoped_lock lock(FlushMutex);
std::ostream& s = *this;
// send cursor back
s << "\33[2K\r";
clearBuffer();
}
}
return *this;
}
Logger& operator<<(std::ostream&(*fp)(std::ostream&)) {
getBuffer() << fp;
// the next operator<< will cause the prefix to be printed after a
// newline
if (fp == &std::endl<std::ostream::char_type, std::ostream::traits_type>) {
{
boost::mutex::scoped_lock lock(FlushMutex);
std::ostream& s = *this;
s << getBuffer().str();
s << std::flush;
}
clearBuffer();
}
// flush the buffer content
if (fp == &std::flush<std::ostream::char_type, std::ostream::traits_type>) {
{
boost::mutex::scoped_lock lock(FlushMutex);
std::ostream& s = *this;
s << getBuffer().str();
s << std::flush;
}
// clear the current buffer
getBuffer().str("");
}
return *this;
}
Logger& operator<<(std::ios&(*fp)(std::ios&)) {
getBuffer() << fp;
return *this;
}
Logger& operator<<(std::ios_base&(*fp)(std::ios_base&)) {
getBuffer() << fp;
return *this;
}
private:
std::stringstream& getBuffer() {
if (_buffer.get() == 0) {
_buffer.reset(new std::stringstream());
clearBuffer();
}
return (*_buffer);
}
void clearBuffer() {
getBuffer().str("");
if (_showChannelPrefix) {
// fill the buffer with the prefix
getBuffer() << getPrefix();
}
}
std::string getPrefix() {
if (_showThreadId)
return boost::lexical_cast<std::string>(boost::this_thread::get_id()) + " " + _prefix;
return _prefix;
}
// reference to the owning LogChannel's prefix
const std::string& _prefix;
// show the prefix for all channels
static bool _showChannelPrefix;
static bool _showThreadId;
// thread local buffer
boost::thread_specific_ptr<std::stringstream> _buffer;
static boost::mutex FlushMutex;
};
class LogFileManager {
public:
~LogFileManager();
static std::filebuf* openFile(std::string filename);
private:
static std::map<std::string, std::filebuf*> filebuffers;
static LogLevel getLogLevel(std::string loglevel);
static LogChannel* getChannel(std::string name);
static void printChains();
static LogLevel globalLogLevel;
};
class LogChannel {
public:
static std::set<LogChannel*>* getChannels();
LogChannel(std::string channelName, std::string prefix = "");
Logger& operator()(LogLevel level);
std::string getName() { return _channelName; };
void setLogLevel(LogLevel level);
const LogLevel& getLogLevel();
void redirectToFile(std::string filename);
private:
friend struct LoggerCleanup;
static std::set<LogChannel*>* logChannels;
static LogFileManager logFileManager;
std::string _channelName;
std::string _prefix;
Logger _error;
Logger _user;
Logger _debug;
Logger _all;
LogLevel _level;
};
class LogManager {
public:
static void init();
static void setGlobalLogLevel(LogLevel logLevel);
static const LogLevel& getGlobalLogLevel();
private:
static void setChainLogLevel(LogChannel* chain, LogLevel level);
static void redirectChainToFile(LogChannel* chain, std::string filename);
static LogLevel getLogLevel(std::string level);
static std::set<LogChannel*> getChannels(std::string channelName);
static void printChannels();
static LogLevel globalLogLevel;
};
struct LoggerCleanup {
~LoggerCleanup() {
if (LogChannel::logChannels != 0)
delete LogChannel::logChannels;
}
};
extern LoggerCleanup loggerCleanup;
typedef std::set<LogChannel*>::iterator channel_it;
extern LogChannel out;
extern LogLevel error;
extern LogLevel user;
extern LogLevel debug;
extern LogLevel all;
} // namespace logger
#endif // #ifndef __LOGGER_HH