log4tango 5.0.2
Logger.hh
Go to the documentation of this file.
1//
2// Logger.hh
3//
4// Copyright (C) : 2000 - 2002
5// LifeLine Networks BV (www.lifeline.nl). All rights reserved.
6// Bastiaan Bakker. All rights reserved.
7//
8// 2004,2005,2006,2007,2008,2009,2010,2011,2012
9// Synchrotron SOLEIL
10// L'Orme des Merisiers
11// Saint-Aubin - BP 48 - France
12//
13// This file is part of log4tango.
14//
15// Log4ango is free software: you can redistribute it and/or modify
16// it under the terms of the GNU Lesser General Public License as published by
17// the Free Software Foundation, either version 3 of the License, or
18// (at your option) any later version.
19//
20// Log4tango is distributed in the hope that it will be useful,
21// but WITHOUT ANY WARRANTY; without even the implied warranty of
22// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23// GNU Lesser General Public License for more details.
24//
25// You should have received a copy of the GNU Lesser General Public License
26// along with Log4Tango. If not, see <http://www.gnu.org/licenses/>.
27
28#ifndef _LOG4TANGO_LOGGER_H
29#define _LOG4TANGO_LOGGER_H
30
31//-----------------------------------------------------------------------------
32// IMPL. OPTION
33//-----------------------------------------------------------------------------
34//#define LOG4TANGO_LOGGERS_USE_LOGSTREAM
35
39#include <log4tango/Level.hh>
40#ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
42#endif
43
44namespace log4tango {
45
46#ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
47//-----------------------------------------------------------------------------
48// FORWARD DECL.
49//-----------------------------------------------------------------------------
50class LogStream;
51#endif
52
53//-----------------------------------------------------------------------------
54// class : Logger
55//-----------------------------------------------------------------------------
57{
58public:
59
65 Logger(const std::string& name,
66 Level::Value level = Level::OFF);
67
71 virtual ~Logger();
72
77 inline const std::string& get_name() const {
78 return _name;
79 }
80
85 void set_level (Level::Value level);
86
91 inline Level::Value get_level() const {
92 return _level;
93 }
94
101 bool is_level_enabled (Level::Value level) const {
102 return _level >= level;
103 }
104
111 void log (Level::Value level,
112 const char* string_format, ...);
113
119 inline void log (Level::Value level, const std::string& message)
120 {
121 if (is_level_enabled(level)) {
122 log_unconditionally(level, message);
123 }
124 }
125
132 void log_unconditionally (Level::Value level,
133 const char* string_format, ...);
134
140 void log_unconditionally (Level::Value level,
141 const std::string& message);
142
148 void debug (const char* string_format, ...);
149
154 inline void debug (const std::string& message) {
155 if (is_level_enabled(Level::DEBUG)) {
156 log_unconditionally(Level::DEBUG, message);
157 }
158 }
159
164 inline bool is_debug_enabled (void) const {
165 return is_level_enabled (Level::DEBUG);
166 };
167
168#ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
174 return LoggerStream(*this, Level::DEBUG, true);
175 }
176#else
181 inline LogStream& debug_stream (void) {
182 return *_log_streams[_DEBUG_STREAM_ID];
183 }
184#endif
185
191 void info (const char* string_format, ...);
192
197 inline void info (const std::string& message) {
198 if (is_level_enabled(Level::INFO)) {
199 log_unconditionally(Level::INFO, message);
200 }
201 }
202
207 inline bool is_info_enabled (void) const {
208 return is_level_enabled(Level::INFO);
209 };
210
211#ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
216 inline LoggerStream info_stream (void) {
217 return LoggerStream(*this, Level::INFO, true);
218 }
219#else
224 inline LogStream& info_stream (void) {
225 return *_log_streams[_INFO_STREAM_ID];
226 }
227#endif
228
234 void warn (const char* string_format, ...);
235
240 inline void warn (const std::string& message) {
241 if (is_level_enabled(Level::WARN)) {
242 log_unconditionally(Level::WARN, message);
243 }
244 }
245
250 inline bool is_warn_enabled (void) const {
251 return is_level_enabled(Level::WARN);
252 };
253
254#ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
259 inline LoggerStream warn_stream (void) {
260 return LoggerStream(*this, Level::WARN, true);
261 };
262#else
267 inline LogStream& warn_stream (void) {
268 return *_log_streams[_WARN_STREAM_ID];
269 }
270#endif
271
277 void error (const char* string_format, ...);
278
283 inline void error (const std::string& message) {
284 if (is_level_enabled(Level::ERROR)) {
285 log_unconditionally(Level::ERROR, message);
286 }
287 }
288
293 inline bool is_error_enabled (void) const {
294 return is_level_enabled(Level::ERROR);
295 };
296
297#ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
303 return LoggerStream(*this, Level::ERROR, true);
304 };
305#else
310 inline LogStream& error_stream (void) {
311 return *_log_streams[_ERROR_STREAM_ID];
312 }
313#endif
314
320 void fatal(const char* string_format, ...);
321
326 inline void fatal (const std::string& message) {
327 if (is_level_enabled(Level::FATAL)) {
328 log_unconditionally(Level::FATAL, message);
329 }
330 }
331
336 inline bool is_fatal_enabled (void) const {
337 return is_level_enabled(Level::FATAL);
338 };
339
340#ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
346 return LoggerStream(*this, Level::FATAL, true);
347 };
348#else
353 inline LogStream& fatal_stream (void) {
354 return *_log_streams[_FATAL_STREAM_ID];
355 }
356#endif
357
358#ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
365 inline LoggerStream get_stream (Level::Value level, bool filter = true) {
366 return LoggerStream(*this, level, filter);
367 }
368#endif
369
370protected:
371
377 void call_appenders (const LoggingEvent& event);
378
379
380private:
381
382#ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
384 enum {
385 _FATAL_STREAM_ID = 0,
386 _ERROR_STREAM_ID = 1,
387 _WARN_STREAM_ID = 2,
388 _INFO_STREAM_ID = 3,
389 _DEBUG_STREAM_ID = 4
390 };
391#endif
392
394 const std::string _name;
395
397 Level::Value _level;
398
399#ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
401 LogStream *_log_streams[5];
402#endif
403
404 /* prevent copying and assignment */
405 Logger (const Logger&);
406 Logger& operator= (const Logger&);
407};
408
409} // namespace log4tango
410
411#endif // _LOG4TANGO_LOGGER_H
#define LOG4TANGO_EXPORT
Definition: Export.hh:38
Definition: AppenderAttachable.hh:59
int Value
The type of Level Values.
Definition: Level.hh:98
@ FATAL
Definition: Level.hh:88
@ OFF
Definition: Level.hh:87
@ DEBUG
Definition: Level.hh:92
@ WARN
Definition: Level.hh:90
@ INFO
Definition: Level.hh:91
@ ERROR
Definition: Level.hh:89
Definition: LogStream.hh:47
Definition: LoggerStream.hh:59
Definition: Logger.hh:57
const std::string & get_name() const
Return the logger name.
Definition: Logger.hh:77
LoggerStream error_stream(void)
Return a LoggerStream with level ERROR.
Definition: Logger.hh:302
LoggerStream warn_stream(void)
Return a LoggerStream with level WARN.
Definition: Logger.hh:259
void warn(const std::string &message)
Log a message with warn level.
Definition: Logger.hh:240
LoggerStream info_stream(void)
Return a LoggerStream with level INFO.
Definition: Logger.hh:216
LoggerStream get_stream(Level::Value level, bool filter=true)
Return a LoggerStream with given Level.
Definition: Logger.hh:365
bool is_info_enabled(void) const
Return true if the Logger will log messages with level INFO.
Definition: Logger.hh:207
bool is_error_enabled(void) const
Return true if the Logger will log messages with level ERROR.
Definition: Logger.hh:293
Level::Value get_level() const
Returns the assigned Level, if any, for this Logger.
Definition: Logger.hh:91
void fatal(const std::string &message)
Log a message with fatal level.
Definition: Logger.hh:326
LoggerStream debug_stream(void)
Return a LoggerStream with level DEBUG.
Definition: Logger.hh:173
void error(const std::string &message)
Log a message with error level.
Definition: Logger.hh:283
void info(const std::string &message)
Log a message with info level.
Definition: Logger.hh:197
void debug(const std::string &message)
Log a message with debug level.
Definition: Logger.hh:154
bool is_debug_enabled(void) const
Return true if the Logger will log messages with level DEBUG.
Definition: Logger.hh:164
bool is_level_enabled(Level::Value level) const
Returns true if the level of the Logger is equal to or higher than given level.
Definition: Logger.hh:101
LoggerStream fatal_stream(void)
Return a LoggerStream with level FATAL.
Definition: Logger.hh:345
bool is_fatal_enabled(void) const
Return true if the Logger will log messages with level FATAL.
Definition: Logger.hh:336
void log(Level::Value level, const std::string &message)
Log a message with the specified level.
Definition: Logger.hh:119
bool is_warn_enabled(void) const
Return true if the Logger will log messages with level WARN.
Definition: Logger.hh:250
Definition: Appender.hh:40
class LOG4TANGO_EXPORT Logger
Definition: LoggerStream.hh:43
The internal representation of logging events.
Definition: LoggingEvent.hh:51