log4tango  5.0.1
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 
36 #include <log4tango/Portability.hh>
39 #include <log4tango/Level.hh>
40 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
41 # include <log4tango/LoggerStream.hh>
42 #endif
43 
44 namespace log4tango {
45 
46 #ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
47 //-----------------------------------------------------------------------------
48 // FORWARD DECL.
49 //-----------------------------------------------------------------------------
50 class LogStream;
51 #endif
52 
53 //-----------------------------------------------------------------------------
54 // class : Logger
55 //-----------------------------------------------------------------------------
57 {
58 public:
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
169 
173  inline LoggerStream debug_stream (void) {
174  return LoggerStream(*this, Level::DEBUG, true);
175  }
176 #else
177 
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
212 
216  inline LoggerStream info_stream (void) {
217  return LoggerStream(*this, Level::INFO, true);
218  }
219 #else
220 
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
255 
259  inline LoggerStream warn_stream (void) {
260  return LoggerStream(*this, Level::WARN, true);
261  };
262 #else
263 
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
298 
302  inline LoggerStream error_stream (void) {
303  return LoggerStream(*this, Level::ERROR, true);
304  };
305 #else
306 
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
341 
345  inline LoggerStream fatal_stream (void) {
346  return LoggerStream(*this, Level::FATAL, true);
347  };
348 #else
349 
353  inline LogStream& fatal_stream (void) {
354  return *_log_streams[_FATAL_STREAM_ID];
355  }
356 #endif
357 
358 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
359 
365  inline LoggerStream get_stream (Level::Value level, bool filter = true) {
366  return LoggerStream(*this, level, filter);
367  }
368 #endif
369 
370 protected:
371 
377  void call_appenders (const LoggingEvent& event);
378 
379 
380 private:
381 
382 #ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
383 
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
400 
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
LoggerStream.hh
log4tango::Logger::is_debug_enabled
bool is_debug_enabled(void) const
Return true if the Logger will log messages with level DEBUG.
Definition: Logger.hh:164
Portability.hh
log4tango::Logger::warn
void warn(const std::string &message)
Log a message with warn level.
Definition: Logger.hh:240
log4tango::Logger::is_error_enabled
bool is_error_enabled(void) const
Return true if the Logger will log messages with level ERROR.
Definition: Logger.hh:293
log4tango::Level::DEBUG
@ DEBUG
Definition: Level.hh:92
log4tango::Level::INFO
@ INFO
Definition: Level.hh:91
log4tango::Logger::fatal_stream
LoggerStream fatal_stream(void)
Return a LoggerStream with level FATAL.
Definition: Logger.hh:345
log4tango::Logger::is_info_enabled
bool is_info_enabled(void) const
Return true if the Logger will log messages with level INFO.
Definition: Logger.hh:207
log4tango::Logger::warn_stream
LoggerStream warn_stream(void)
Return a LoggerStream with level WARN.
Definition: Logger.hh:259
log4tango::Logger::get_name
const std::string & get_name() const
Return the logger name.
Definition: Logger.hh:77
log4tango::Logger::fatal
void fatal(const std::string &message)
Log a message with fatal level.
Definition: Logger.hh:326
log4tango::Logger::debug
void debug(const std::string &message)
Log a message with debug level.
Definition: Logger.hh:154
log4tango::Logger::is_warn_enabled
bool is_warn_enabled(void) const
Return true if the Logger will log messages with level WARN.
Definition: Logger.hh:250
log4tango::Logger::info_stream
LoggerStream info_stream(void)
Return a LoggerStream with level INFO.
Definition: Logger.hh:216
log4tango::LogStream
Definition: LogStream.hh:46
log4tango::LoggingEvent
The internal representation of logging events.
Definition: LoggingEvent.hh:50
log4tango::Logger::info
void info(const std::string &message)
Log a message with info level.
Definition: Logger.hh:197
log4tango
Definition: Appender.hh:40
AppenderAttachable.hh
log4tango::AppenderAttachable
Definition: AppenderAttachable.hh:58
log4tango::Logger::is_fatal_enabled
bool is_fatal_enabled(void) const
Return true if the Logger will log messages with level FATAL.
Definition: Logger.hh:336
log4tango::LoggerStream
Definition: LoggerStream.hh:58
LOG4TANGO_EXPORT
#define LOG4TANGO_EXPORT
Definition: Export.hh:38
log4tango::Level::WARN
@ WARN
Definition: Level.hh:90
log4tango::Logger::log
void log(Level::Value level, const std::string &message)
Log a message with the specified level.
Definition: Logger.hh:119
log4tango::Level::OFF
@ OFF
Definition: Level.hh:87
LoggingEvent.hh
log4tango::Logger::get_stream
LoggerStream get_stream(Level::Value level, bool filter=true)
Return a LoggerStream with given Level.
Definition: Logger.hh:365
log4tango::Logger::error_stream
LoggerStream error_stream(void)
Return a LoggerStream with level ERROR.
Definition: Logger.hh:302
log4tango::Level::Value
int Value
The type of Level Values.
Definition: Level.hh:98
log4tango::Logger
Definition: Logger.hh:56
log4tango::Level::FATAL
@ FATAL
Definition: Level.hh:88
log4tango::Logger::error
void error(const std::string &message)
Log a message with error level.
Definition: Logger.hh:283
log4tango::Logger
class LOG4TANGO_EXPORT Logger
Definition: LoggerStream.hh:43
Level.hh
log4tango::Logger::is_level_enabled
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
log4tango::Logger::debug_stream
LoggerStream debug_stream(void)
Return a LoggerStream with level DEBUG.
Definition: Logger.hh:173
log4tango::Level::ERROR
@ ERROR
Definition: Level.hh:89
log4tango::Logger::get_level
Level::Value get_level() const
Returns the assigned Level, if any, for this Logger.
Definition: Logger.hh:91