solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/configuration/ILogger.h
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  solarpowerlog
00003  Copyright (C) 2009  Tobias Frost
00004 
00005  This file is part of solarpowerlog.
00006 
00007  Solarpowerlog is free software; However, it is dual-licenced
00008  as described in the file "COPYING".
00009 
00010  For this file (ILogger.h), the license terms are:
00011 
00012  You can redistribute it and/or  modify it under the terms of the GNU Lesser
00013  General Public License (LGPL) as published by the Free Software Foundation;
00014  either version 3 of the License, or (at your option) any later version.
00015 
00016  This program is distributed in the hope that it will be useful, but
00017  WITHOUT ANY WARRANTY; without even the implied warranty of
00018  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  Lesser General Public License for more details.
00020 
00021  You should have received a copy of the GNU Library General Public
00022  License along with this proramm; if not, see
00023  <http://www.gnu.org/licenses/>.
00024  ----------------------------------------------------------------------------
00025  */
00026 
00058 #ifndef ILOGGER_H_
00059 #define ILOGGER_H_
00060 
00061 #ifdef HAVE_CONFIG_H
00062 #include "config.h"
00063 #endif
00064 
00065 #include <string>
00066 #include <ostream>
00067 
00068 
00069 #ifdef HAVE_LIBLOG4CXX
00070 #include <log4cxx/logger.h>
00071 #endif
00072 
00073 
00074 #ifdef HAVE_LIBLOG4CXX
00075 
00076 #define LOGFATAL(logger, message)  do \
00077      {\
00078           if (logger.IsEnabled(ILogger::LL_FATAL)) { \
00079                std::stringstream ss;\
00080                ss << message;\
00081                logger << ss;\
00082           }\
00083      } while(0)
00084 
00085 #define LOGERROR(logger, message)  do \
00086      {\
00087           if (logger.IsEnabled(ILogger::LL_ERROR)) { \
00088                std::stringstream ss;\
00089                ss << message;\
00090                logger << ss;\
00091           }\
00092      } while(0)
00093 
00094 #define LOGWARN(logger, message)   do \
00095      {\
00096           if (logger.IsEnabled(ILogger::LL_WARN)) { \
00097                std::stringstream ss;\
00098                ss << message;\
00099                logger << ss;\
00100           }\
00101      } while(0)
00102 
00103 #define LOGINFO(logger, message)   do \
00104      {\
00105           if (logger.IsEnabled(ILogger::LL_INFO)) { \
00106                std::stringstream ss;\
00107                ss << message;\
00108                logger << ss;\
00109           }\
00110      } while(0)
00111 
00112 #define LOGDEBUG(logger, message)   do \
00113      {\
00114           if (logger.IsEnabled(ILogger::LL_DEBUG)) { \
00115                std::stringstream ss;\
00116                ss << message;\
00117                logger << ss;\
00118           }\
00119      } while(0)
00120 
00121 #define LOGTRACE(logger, message)   do \
00122      {\
00123           if (logger.IsEnabled(ILogger::LL_TRACE)) { \
00124                std::stringstream ss;\
00125                ss << message;\
00126                logger << ss;\
00127           }\
00128      } while(0)
00129 
00130 #define LOGALL(logger, message)   do \
00131      {\
00132           if (logger.IsEnabled(ILogger::LL_ALL)) { \
00133                std::stringstream ss;\
00134                ss << message;\
00135                logger << ss;\
00136           }\
00137      } while(0)
00138 
00139 #else
00140 
00141 #define LOGFATAL(logger, message) do { \
00142      cerr << message << endl;\
00143 } while(0)
00144 
00145 #define LOGERROR(logger, message) do { \
00146      cerr << message << endl;\
00147 } while(0)
00148 
00149 #define LOGWARN(logger, message)
00150 
00151 #define LOGINFO(logger, message)
00152 
00153 #define LOGDEBUG(logger, message)
00154 
00155 #define LOGTRACE(logger, message)
00156 
00157 #define LOGALL(logger, message)
00158 
00159 #endif
00160 
00161 
00162 #ifdef HAVE_LIBLOG4CXX
00163 
00179 class ILogger /*: public std::ostream*/
00180 {
00181 public:
00182 
00183      enum level
00184      {
00185           LL_OFF = log4cxx::Level::OFF_INT,
00186           LL_FATAL = log4cxx::Level::FATAL_INT,
00187           LL_ERROR = log4cxx::Level::ERROR_INT,
00188           LL_WARN = log4cxx::Level::WARN_INT,
00189           LL_INFO = log4cxx::Level::INFO_INT,
00190           LL_DEBUG = log4cxx::Level::DEBUG_INT,
00191           LL_TRACE = log4cxx::Level::TRACE_INT,
00192           LL_ALL = log4cxx::Level::ALL_INT
00193      };
00194 
00204      void Setup( const std::string &name, const std::string &configuration,
00205           const std::string& section );
00206 
00213      void Setup( const std::string &parent,
00214           const std::string &specialization );
00215 
00217      ILogger();
00218 
00219      virtual ~ILogger();
00220 
00222      inline std::string getLoggername() const
00223      {
00224           return loggername_;
00225      }
00226 
00241      inline bool IsEnabled( int loglevel )
00242      {
00243           if (loglevel >= currentloggerlevel_) {
00244                currentlevel = loglevel;
00245                return true;
00246           } else
00247                return false;
00248      }
00249 
00256      inline void SetLogLevel( int loglevel )
00257      {
00258           currentlevel = loglevel;
00259      }
00260 
00267      inline void Log( int loglevel, const std::string &log )
00268      {
00269           if (IsEnabled(loglevel))
00270                loggerptr_->log(log4cxx::Level::toLevel(loglevel),log);
00271      }
00272 
00279      std::string & operator <<( std::string &os )
00280      {
00281           if (currentlevel >= currentloggerlevel_) {
00282                loggerptr_->forcedLog(log4cxx::Level::toLevel(
00283                     currentlevel), os);
00284           }
00285           return os;
00286      }
00287 
00294      std::stringstream & operator <<( std::stringstream &os )
00295      {
00296           if (currentlevel >= currentloggerlevel_) {
00297                std::string s = os.str();
00298                loggerptr_->forcedLog(log4cxx::Level::toLevel(
00299                     currentlevel), s);
00300           }
00301           return os;
00302      }
00303 
00304 private:
00305 
00307      std::string config_;
00308 
00310      std::string loggername_;
00311 
00313      log4cxx::LoggerPtr loggerptr_;
00314 
00316      int currentlevel;
00317 
00321      int currentloggerlevel_;
00322 
00323 
00324 };
00325 
00326 #endif // HAVE_LIBLOG4CXX
00327 #ifndef HAVE_LIBLOG4CXX
00328 
00344 class ILogger /*: public std::ostream*/
00345 {
00346 public:
00347 
00348      enum level
00349      {
00350           LL_OFF = 0,
00351           LL_FATAL ,
00352           LL_ERROR ,
00353           LL_WARN ,
00354           LL_INFO ,
00355           LL_DEBUG ,
00356           LL_TRACE ,
00357           LL_ALL
00358      };
00368      void Setup( const std::string &, const std::string &,
00369           const std::string&  ) { };
00370 
00377      void Setup( const std::string &,
00378                const std::string & ) {};
00379 
00381      ILogger() {};
00382 
00383      virtual ~ILogger() {};
00384 
00385      std::string getLoggername() const
00386      {
00387           return "";
00388      }
00389 
00400      inline bool IsEnabled( int )
00401      {
00402           return false;
00403      }
00404 
00405      inline void SetLogLevel( int )
00406      {
00407      }
00408 
00409      inline void Log( int , std::string )
00410      {
00411      }
00412 
00413      std::string & operator <<( std::string &os )
00414      {
00415           return os;
00416      }
00417 
00418      std::stringstream & operator <<( std::stringstream &os )
00419      {
00420           return os;
00421      }
00422 
00423 };
00424 #endif // !HAVE_LIBLOG4CXX
00425 
00426 
00427 
00428 #endif /* ILOGGER_H_ */