-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogxx.hpp
272 lines (226 loc) · 7.47 KB
/
logxx.hpp
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
#ifndef LOGXX_5JKQSPQI
// vi:cin:et:sw=2 ts=2
#define LOGXX_5JKQSPQI
// Copyright Jiri Kratochvil (aka KLoK) 2010
// Distributed under the Boost Software License, Version 1.0.
// (see at http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string>
#include <map>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/format.hpp>
#ifndef LOGXX_DEFAULT_LOGGER
#define LOGXX_DEFAULT_LOGGER logxx::logger
#endif
#ifndef LOGXX_DEFAULT_LEVEL
#define LOGXX_DEFAULT_LEVEL logxx::debug
#endif
// IDEA: load dynamicaly LEVEL from ENVIRONMENT
namespace logxx {
class basic_channel {
public:
virtual std::ostream& stream() = 0;
virtual std::ostream& flush() = 0;
};
class console_channel : public basic_channel {
public:
std::ostream& stream() { return std::cerr; }
std::ostream& flush() { return std::cerr.flush(); }
};
class file_channel : public basic_channel {
public:
file_channel(const char* filename) : file_(filename, std::ios_base::app | std::ios_base::out ) {}
std::ostream& stream() { return file_; }
std::ostream& flush() { return file_.flush(); }
private:
std::ofstream file_;
};
class null_channel : public basic_channel {
public:
std::ostream& stream() { return null(); }
std::ostream& flush() { return null().flush(); }
struct nullstream : std::ostream { nullstream():std::ostream(0){} };
private:
std::ostream& null() {
static nullstream null_;
return null_;
}
};
//
// TODO:
// - tee_channel,
// - syslog channel,
// - anything other??
//
typedef enum {
fatal,
error,
warning,
info,
debug,
trace
} level;
/**
* basic logger
*
* policy based logger - idea:
*
* logger - there is root logger (without name), all messages are sent there
* you can create new one "on fly" just by send name into logger::log() static method
* if logger does not exists, new one is created
*
* all loggers inherit basic settings from "root logger" when created
*
*
* filter_policy - decide what will happens with message
* format_policy - add some envelope to message (e.g. time, level, etc)
* channel_policy - messages which goes through filter are sent into
*
* basic flow:
* message is sent to logger ->
* logger will sent message to filter ->
* filter will use formater to add message envelope
* and in next step will decide what to do with message (usualy send into channel)
*
*/
template <int default_level, class format_policy, class filter_policy, class default_channel = console_channel>
class basic_logger {
public:
typedef basic_logger<default_level, format_policy, filter_policy, default_channel> self_t;
public:
/**
* Heart of logger.
* return instance of logger from logger repository
* if logger doesn't exists create new one.
*
* logger instances are holds by boost::shared_ptr so you need no care about free instance
*/
static self_t& log(const std::string& name = "") {
// IDEA: move creating strategy into another policy
boost::shared_ptr<self_t> r = loggers_[name];
if(!r) {
r.reset(new self_t(name));
if(!name.empty()) { // as default - copy channel properties root logger
boost::shared_ptr<self_t> root = loggers_[""];
if(!root) root.reset(new self_t(name)); // if not root loger in repository create new one with default settings
r->channel_ = root->channel_;
r->filter_.level_ = root->filter_.level_;
}
loggers_[name] = r;
}
return *r;
}
const std::string& name() const { return name_; }
void channel(basic_channel* channel) { if(channel_) channel_->flush() ; channel_.reset(channel); }
basic_channel& channel() { if(!channel_) channel_.reset(new default_channel); return *channel_; }
/**
* return instance of filter to send log on
*/
std::ostream& get(int level) {
std::ostream& o = filter_(level,*this);
o << format_(level,*this);
return o;
}
#if 0
/**
* shortcut of ::get() method
*/
std::ostream& operator()(int level) {
return filter_.filter(level,*this);
}
#endif
virtual ~basic_logger() {
if (channel_) channel_->flush();
}
/**
* setup filter "at least" level for given channel
*
* usage:
* logxx::logger::log().level(logxx::debug)
*/
inline void level(int level) { filter_.level_ = level; }
/**
* return current level of filter
*/
inline int level() const { return filter_.level_; }
/**
* hex memory dump from @param addr and length @param len
*
* dump() is by default sent to debug level
*/
void dump(void* addr, int len) {
if(filter_.level_ < logxx::debug) return;
unsigned char* a = reinterpret_cast<unsigned char*>(addr);
get(logxx::debug) << boost::format("DUMP [%p(%d)]:") % addr % len << std::endl;
for(int i = 0 ; i < len ; ++i ) {
channel_->stream() << boost::format("%02x%c")
% (unsigned int)(a[i])
% ((i+1)%16?' ':'\n');
}
channel_->stream() << std::endl;
}
protected:
boost::shared_ptr<basic_channel> channel_;
format_policy format_;
filter_policy filter_;
std::string name_;
static std::map<std::string, boost::shared_ptr<self_t> > loggers_;
basic_logger(const std::string& name) : name_(name) {
filter_.level_ = default_level;
}
};
// init static member :)
template <int default_level, class format_policy, class filter_policy, class default_channel>
std::map<std::string, boost::shared_ptr<basic_logger<default_level, format_policy,filter_policy,default_channel> > >
basic_logger<default_level, format_policy,filter_policy,default_channel>::loggers_;
/**
* message formater
*/
struct std_format {
template<class Tlogger>
std::string operator()(int level, Tlogger& l) {
using boost::format;
return str(format("%s [%d] <%d>: ")
% boost::posix_time::second_clock::universal_time()
% getpid()
% level);
}
};
/**
* log filter -
* it takes care on filtering sent messages
* std_filter will send everything with higher level than level into null channel
*
* it is meant for extending eg.
* - abort program when level is lower than "critical"
* - sent critical events to different channel eg. syslog
* - assertion for debug version
*
* some of ideas will be added into logxx lib in future
*/
struct std_filter {
template<class Tlogger>
std::ostream& operator()(int level, Tlogger& l) {
static null_channel null;
return level <= level_
? l.channel().stream()
: null.stream();
}
int level_;
};
/**
* predefined logger with most basic policy to allow trivial log
*/
typedef class basic_logger<LOGXX_DEFAULT_LEVEL , std_format, std_filter> logger;
}
#define LOG_CHANNEL(ch) LOGXX_DEFAULT_LOGGER::log().channel(ch)
#define LOG_CHANNEL_(sink, ch) LOGXX_DEFAULT_LOGGER::log(sink).channel(ch)
#define LOG(lvl) if (LOGXX_DEFAULT_LOGGER::log().level() >= logxx::lvl) LOGXX_DEFAULT_LOGGER::log().get(logxx::lvl)
#define LOG_(sink, lvl) if (LOGXX_DEFAULT_LOGGER::log(sink).level() >= logxx::lvl) LOGXX_DEFAULT_LOGGER::log(sink).get(logxx::lvl)
#define DUMP(addr, len) if (LOGXX_DEFAULT_LOGGER::log().level() >= logxx::debug) LOGXX_DEFAULT_LOGGER::log().dump((void*)addr,len)
#define LOG_IF(condition, lvl) if ((condition) && LOGXX_DEFAULT_LOGGER::log().level() >= logxx::lvl) LOGXX_DEFAULT_LOGGER::log().get(logxx::lvl)
#define LOG_IF_(condition, sink, lvl) if ((condition) && LOGXX_DEFAULT_LOGGER::log(sink).level() >= logxx::lvl) LOGXX_DEFAULT_LOGGER::log(sink).get(logxx::lvl)
#endif /* end of include guard: LOGXX_5JKQSPQI */