presage 0.9.1
logger.h
Go to the documentation of this file.
1
2/******************************************************
3 * Presage, an extensible predictive text entry system
4 * ---------------------------------------------------
5 *
6 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 **********(*)*/
23
24
25#ifndef PRESAGE_LOGGER
26#define PRESAGE_LOGGER
27
28#if HAVE_CONFIG_H
29# include <config.h>
30#endif
31
32#include <iostream>
33
34
35// manipulators
36struct _SetLevel { std::string _level; };
37
45inline _SetLevel
46setlevel(std::string __l)
47{
48 _SetLevel __x;
49 __x._level = __l;
50 return __x;
51}
52
53
54
55template <class _charT, class _Traits=std::char_traits<_charT> >
56class Logger
57{
58public:
59 // type definitions
60 enum Level
61 {
62 EMERG = 0,
63 FATAL = 0,
64 ALERT = 100,
65 CRIT = 200,
66 ERROR = 300,
67 WARN = 400,
68 NOTICE = 500,
69 INFO = 600,
70 DEBUG = 700,
71 ALL = 800
72 };
73
74 // constructors
75 inline
76 Logger (std::string logger_name,
77 std::basic_ostream<_charT,_Traits>& ostr)
79 {
80 init(logger_name, "ERROR");
81 }
82
83 inline
84 Logger (std::string logger_name,
85 std::basic_ostream<_charT,_Traits>& ostr,
86 const std::string& lvl)
88 {
90 }
91
92 inline
93 void init(const std::string& name, const std::string& lvl)
94 {
96 state = new LoggerState();
99 state->line_beginning = true;
100 }
101
102 // destructor
103 inline
105 {
106 outstream.flush();
107 delete state;
108 }
109
110
111 // level getters/setters
112 inline
113 void
114 setLevel(const std::string& lvl) const
115 {
117 }
118
119 inline
120 void
122 {
124 }
125
126 inline
127 Level
128 getLevel () const
129 {
130 return state->loggerLevel;
131 }
132
133 inline
134 void
136 {
138 }
139
140 inline
141 Level
143 {
144 return state->currentLevel;
145 }
146
147 inline
148 bool
149 shouldLog() const
150 {
151 return (state->loggerLevel >= state->currentLevel);
152 }
153
154 // logging method
155 template<typename T>
156 friend inline
157 const Logger&
158 operator<< (const Logger& lgr, const T& msg)
159 {
160 //if (lgr.state->loggerLevel >= lgr.state->currentLevel)
161 if (lgr.shouldLog())
162 {
163 if (lgr.state->line_beginning) {
164 lgr.outstream << lgr.name;
165 lgr.state->line_beginning = false;
166 }
167 lgr.outstream << msg;
168 }
169 return lgr;
170 }
171
172 // this method is needed by the functions defined by macro
173 // define_logger_level_manipulator(LEVEL)
174 //
175 friend inline
176 const Logger&
177 operator<< (const Logger& lgr, const Logger& (*fp)(const Logger&))
178 {
179 (*fp)(lgr);
180 return lgr;
181 }
182
183 inline
184 const Logger&
186 {
187 setLevel(__l._level);
188 return *this;
189 }
190
191 inline
192 void endl() const
193 {
194 //if (state->loggerLevel >= state->currentLevel)
195 if (shouldLog())
196 {
197 outstream << std::endl;
198 state->line_beginning = true;
199 }
200 }
201
202private:
203 inline
204 void
205 set(Level& level, const std::string& lvl) const
206 {
207 if (lvl == "EMERG") {
208 level = EMERG;
209 } else if (lvl == "FATAL") {
210 level = FATAL;
211 } else if (lvl == "ALERT") {
212 level = ALERT;
213 } else if (lvl == "CRIT") {
214 level = CRIT;
215 } else if (lvl == "ERROR") {
216 level = ERROR;
217 } else if (lvl == "WARN") {
218 level = WARN;
219 } else if (lvl == "NOTICE") {
220 level = NOTICE;
221 } else if (lvl == "INFO") {
222 level = INFO;
223 } else if (lvl == "DEBUG") {
224 level = DEBUG;
225 } else if (lvl == "ALL") {
226 level = ALL;
227 } else {
228 level = ERROR;
229 }
230 }
231
232 inline
233 void
234 set_name(const std::string& logger_name)
235 {
236 name = "[" + logger_name + "] ";
237 }
238
239 std::string name;
240 std::basic_ostream <_charT, _Traits>& outstream;
241
249
251};
252
253
254
255#define define_logger_level_manipulator(LEVEL) \
256 template <typename _charT, typename _Traits> \
257 inline const Logger<_charT, _Traits>& \
258 LEVEL (const Logger<_charT, _Traits>& lgr) \
259 { \
260 lgr.setCurrentLevel(Logger<_charT, _Traits>::LEVEL); \
261 return lgr; \
262 }
263
274
275
276template <typename _charT, typename _Traits>
277inline const Logger<_charT, _Traits>&
279{
280 lgr.endl();
281 return lgr;
282}
283
284
285#endif // PRESAGE_LOGGER
void setLevel(Level lvl) const
Definition logger.h:121
std::string name
Definition logger.h:239
LoggerState * state
Definition logger.h:250
Level getLevel() const
Definition logger.h:128
std::basic_ostream< _charT, _Traits > & outstream
Definition logger.h:240
void setLevel(const std::string &lvl) const
Definition logger.h:114
void set(Level &level, const std::string &lvl) const
Definition logger.h:205
void setCurrentLevel(Level lvl) const
Definition logger.h:135
bool shouldLog() const
Definition logger.h:149
void init(const std::string &name, const std::string &lvl)
Definition logger.h:93
void endl() const
Definition logger.h:192
Logger(std::string logger_name, std::basic_ostream< _charT, _Traits > &ostr)
Definition logger.h:76
Level getCurrentLevel() const
Definition logger.h:142
~Logger()
Definition logger.h:104
Logger(std::string logger_name, std::basic_ostream< _charT, _Traits > &ostr, const std::string &lvl)
Definition logger.h:84
Level
Definition logger.h:61
@ DEBUG
Definition logger.h:70
@ ERROR
Definition logger.h:66
@ INFO
Definition logger.h:69
@ NOTICE
Definition logger.h:68
@ WARN
Definition logger.h:67
@ ALL
Definition logger.h:71
@ CRIT
Definition logger.h:65
@ FATAL
Definition logger.h:63
@ ALERT
Definition logger.h:64
@ EMERG
Definition logger.h:62
void set_name(const std::string &logger_name)
Definition logger.h:234
friend const Logger & operator<<(const Logger &lgr, const T &msg)
Definition logger.h:158
_SetLevel setlevel(std::string __l)
Manipulator for level.
Definition logger.h:46
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition logger.h:278
#define define_logger_level_manipulator(LEVEL)
Definition logger.h:255
std::string _level
Definition logger.h:36