log4tango 4.0.3
Logger.hh
Go to the documentation of this file.
00001 //
00002 // Logger.hh
00003 //
00004 // Copyright (C) :  2000 - 2002
00005 //                                      LifeLine Networks BV (www.lifeline.nl). All rights reserved.
00006 //                                      Bastiaan Bakker. All rights reserved.   
00007 //                                      
00008 //                                      2004,2005,2006,2007,2008,2009,2010
00009 //                                      Synchrotron SOLEIL
00010 //                      L'Orme des Merisiers
00011 //                      Saint-Aubin - BP 48 - France
00012 //
00013 // This file is part of log4tango.
00014 //
00015 // Log4ango is free software: you can redistribute it and/or modify
00016 // it under the terms of the GNU Lesser General Public License as published by
00017 // the Free Software Foundation, either version 3 of the License, or
00018 // (at your option) any later version.
00019 // 
00020 // Log4tango is distributed in the hope that it will be useful,
00021 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 // GNU Lesser General Public License for more details.
00024 // 
00025 // You should have received a copy of the GNU Lesser General Public License
00026 // along with Log4Tango.  If not, see <http://www.gnu.org/licenses/>.
00027 
00028 #ifndef _LOG4TANGO_LOGGER_H
00029 #define _LOG4TANGO_LOGGER_H
00030 
00031 //-----------------------------------------------------------------------------
00032 // IMPL. OPTION
00033 //----------------------------------------------------------------------------- 
00034 //#define LOG4TANGO_LOGGERS_USE_LOGSTREAM
00035  
00036 #include <log4tango/Portability.hh>
00037 #include <log4tango/AppenderAttachable.hh>
00038 #include <log4tango/LoggingEvent.hh>
00039 #include <log4tango/Level.hh>
00040 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM
00041 # include <log4tango/LoggerStream.hh>
00042 #endif
00043 
00044 namespace log4tango {
00045 
00046 #ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
00047 //-----------------------------------------------------------------------------
00048 // FORWARD DECL.
00049 //-----------------------------------------------------------------------------  
00050 class LogStream;
00051 #endif
00052 
00053 //-----------------------------------------------------------------------------
00054 // class : Logger
00055 //-----------------------------------------------------------------------------   
00056 class LOG4TANGO_EXPORT Logger : public AppenderAttachable
00057 {
00058 public:   
00059 
00065   Logger(const std::string& name, 
00066          Level::Value level = Level::OFF);
00067 
00071   virtual ~Logger();
00072 
00077   inline const std::string& get_name() const {
00078     return _name; 
00079   }
00080       
00085   void set_level (Level::Value level);
00086 
00091   inline Level::Value get_level() const {
00092     return _level;
00093   }
00094 
00101   bool is_level_enabled (Level::Value level) const {
00102     return _level >= level;
00103   }
00104 
00111   void log (Level::Value level, 
00112             const char* string_format, ...);
00113 
00119   inline void log (Level::Value level, const std::string& message) 
00120   { 
00121     if (is_level_enabled(level)) {
00122       log_unconditionally(level, message);
00123     }
00124   }
00125  
00132   void log_unconditionally (Level::Value level, 
00133                             const char* string_format, ...);
00134 
00140   void log_unconditionally (Level::Value level, 
00141                             const std::string& message);
00142                 
00148   void debug (const char* string_format, ...);
00149 
00154   inline void debug (const std::string& message) {
00155     if (is_level_enabled(Level::DEBUG)) {
00156       log_unconditionally(Level::DEBUG, message);
00157     }
00158   }
00159 
00164   inline bool is_debug_enabled (void) const { 
00165     return is_level_enabled (Level::DEBUG);
00166   };
00167      
00168 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM   
00169 
00173   inline LoggerStream debug_stream (void) {
00174     return LoggerStream(*this, Level::DEBUG, true);
00175   }
00176 #else
00177 
00181   inline LogStream& debug_stream (void) {
00182     return *_log_streams[_DEBUG_STREAM_ID];
00183   }
00184 #endif
00185 
00191   void info (const char* string_format, ...);
00192 
00197   inline void info (const std::string& message) {
00198     if (is_level_enabled(Level::INFO)) {
00199       log_unconditionally(Level::INFO, message);
00200     }
00201   }
00202 
00207   inline bool is_info_enabled (void) const { 
00208     return is_level_enabled(Level::INFO);
00209   };
00210 
00211 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM   
00212 
00216   inline LoggerStream info_stream (void) {
00217     return LoggerStream(*this, Level::INFO, true);
00218   }
00219 #else
00220 
00224   inline LogStream& info_stream (void) {
00225     return *_log_streams[_INFO_STREAM_ID];
00226   }
00227 #endif
00228       
00234   void warn (const char* string_format, ...);
00235 
00240   inline void warn (const std::string& message) {
00241     if (is_level_enabled(Level::WARN)) {
00242       log_unconditionally(Level::WARN, message);
00243     }
00244   }
00245 
00250   inline bool is_warn_enabled (void) const { 
00251     return is_level_enabled(Level::WARN);
00252   };
00253 
00254 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM   
00255 
00259   inline LoggerStream warn_stream (void) {
00260     return LoggerStream(*this, Level::WARN, true);
00261   };
00262 #else
00263 
00267   inline LogStream& warn_stream (void) {
00268     return *_log_streams[_WARN_STREAM_ID];
00269   }
00270 #endif
00271 
00277   void error (const char* string_format, ...);
00278 
00283   inline void error (const std::string& message) {
00284     if (is_level_enabled(Level::ERROR)) {
00285       log_unconditionally(Level::ERROR, message);
00286     }
00287   }
00288 
00293   inline bool is_error_enabled (void) const { 
00294     return is_level_enabled(Level::ERROR);
00295   };
00296 
00297 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM   
00298 
00302   inline LoggerStream error_stream (void) {
00303       return LoggerStream(*this, Level::ERROR, true);
00304   };
00305 #else
00306 
00310   inline LogStream& error_stream (void) {
00311     return *_log_streams[_ERROR_STREAM_ID];
00312   }
00313 #endif
00314 
00320   void fatal(const char* string_format, ...);
00321 
00326   inline void fatal (const std::string& message) {
00327     if (is_level_enabled(Level::FATAL)) {
00328       log_unconditionally(Level::FATAL, message);
00329     }
00330   }
00331 
00336   inline bool is_fatal_enabled (void) const { 
00337     return is_level_enabled(Level::FATAL);
00338   };
00339   
00340 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM   
00341 
00345   inline LoggerStream fatal_stream (void) {
00346     return LoggerStream(*this, Level::FATAL, true);
00347   };
00348 #else
00349 
00353   inline LogStream& fatal_stream (void) {
00354     return *_log_streams[_FATAL_STREAM_ID];
00355   }
00356 #endif
00357 
00358 #ifndef LOG4TANGO_LOGGERS_USE_LOGSTREAM   
00359 
00365   inline LoggerStream get_stream (Level::Value level, bool filter = true) {
00366     return LoggerStream(*this, level, filter);
00367   }
00368 #endif
00369 
00370 protected:
00371 
00377   void call_appenders (const LoggingEvent& event);
00378   
00379 
00380 private:
00381 
00382 #ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
00383 
00384   enum {
00385     _FATAL_STREAM_ID = 0,
00386     _ERROR_STREAM_ID = 1,
00387     _WARN_STREAM_ID  = 2,
00388     _INFO_STREAM_ID  = 3,
00389     _DEBUG_STREAM_ID = 4
00390   };
00391 #endif
00392 
00394   const std::string _name;
00395 
00397   Level::Value _level;
00398 
00399 #ifdef LOG4TANGO_LOGGERS_USE_LOGSTREAM
00400 
00401   LogStream *_log_streams[5];
00402 #endif
00403 
00404   /* prevent copying and assignment */
00405   Logger (const Logger&);
00406   Logger& operator= (const Logger&);
00407 };
00408 
00409 } // namespace log4tango
00410 
00411 #endif // _LOG4TANGO_LOGGER_H