LoggerImpl.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2011 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
00025  *
00026  **************************************************************************/
00027 
00035 #ifndef KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_
00036 #define KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_
00037 
00038 #include "keyvalue/bridge/Bridge.h"
00039 #include "keyvalue/sys/logger/Logger.h"
00040 #include "keyvalue/sys/logger/policy/AddPrefix.h"
00041 #include "keyvalue/sys/logger/policy/ForwardToGlobalLogger.h"
00042 #include "keyvalue/sys/logger/policy/IgnoreColour.h"
00043 #include "keyvalue/sys/message/MessageImpl.h"
00044 #include "keyvalue/util/Global.h"
00045 
00046 namespace keyvalue {
00047 namespace logger {
00048 
00062 template <typename PrefixPolicy = AddPrefix,
00063   typename ColourPolicy = IgnoreColour,
00064   typename SafetyPolicy = ForwardToGlobalLogger>
00065 
00066 class LoggerImpl :
00067 
00068   public Logger,
00069   private PrefixPolicy,
00070   private ColourPolicy,
00071   private SafetyPolicy {
00072 
00073 public:
00074 
00075   typedef LoggerImpl<PrefixPolicy, ColourPolicy, SafetyPolicy> LoggerImpl_;
00076 
00077   unsigned int
00078   getLevel() const;
00079 
00080   void
00081   setLevel(unsigned int level);
00082 
00083   bool
00084   log(const Message& message);
00085 
00086 protected:
00087 
00088   LoggerImpl(unsigned int level);
00089 
00098   bool
00099   sendHeader();
00100 
00101 private:
00102 
00103   unsigned int level_;
00104 
00120   bool
00121   process(const Message& message);
00122 
00132   virtual bool
00133   send(const string& message) = 0;
00134 
00135 }; // class LoggerImpl
00136 
00137 /*--------------------------------------------------------------------------
00138  * getLevel()
00139  *------------------------------------------------------------------------*/
00140 
00141 template <typename PrefixPolicy, typename ColourPolicy,
00142   typename SafetyPolicy>
00143 unsigned int
00144 LoggerImpl<PrefixPolicy, ColourPolicy, SafetyPolicy>::getLevel() const {
00145   return level_;
00146 }
00147 
00148 /*--------------------------------------------------------------------------
00149  * setLevel()
00150  *------------------------------------------------------------------------*/
00151 
00152 template <typename PrefixPolicy, typename ColourPolicy,
00153   typename SafetyPolicy>
00154 void
00155 LoggerImpl<PrefixPolicy, ColourPolicy, SafetyPolicy>::setLevel
00156   (const unsigned int level) {
00157 
00158   level_ = level;
00159 }
00160 
00161 /*--------------------------------------------------------------------------
00162  * log()
00163  *------------------------------------------------------------------------*/
00164 
00165 template <typename PrefixPolicy, typename ColourPolicy,
00166   typename SafetyPolicy>
00167 bool
00168 LoggerImpl<PrefixPolicy, ColourPolicy, SafetyPolicy>::log
00169   (const Message& message) {
00170 
00171   if (!dynamic_cast<const Error *>(&message) && message.getLevel() >=level_)
00172     return true;
00173   return process(message);
00174 }
00175 
00176 /*--------------------------------------------------------------------------
00177  * LoggerImpl()
00178  *------------------------------------------------------------------------*/
00179 
00180 template <typename PrefixPolicy, typename ColourPolicy,
00181   typename SafetyPolicy>
00182 LoggerImpl<PrefixPolicy, ColourPolicy, SafetyPolicy>::LoggerImpl
00183   (const unsigned int level) : level_(level) {
00184 }
00185 
00186 /*--------------------------------------------------------------------------
00187  * sendHeader()
00188  *------------------------------------------------------------------------*/
00189 
00190 template <typename PrefixPolicy, typename ColourPolicy,
00191   typename SafetyPolicy>
00192 bool
00193 LoggerImpl<PrefixPolicy, ColourPolicy, SafetyPolicy>::sendHeader() {
00194   return send(util::Global<Bridge>::get()->getCompleteInfo()) &&
00195     send("\n\n");
00196 }
00197 
00198 /*--------------------------------------------------------------------------
00199  * process()
00200  *------------------------------------------------------------------------*/
00201 
00202 template <typename PrefixPolicy, typename ColourPolicy,
00203   typename SafetyPolicy>
00204 bool
00205 LoggerImpl<PrefixPolicy, ColourPolicy, SafetyPolicy>::process
00206   (const Message& message) {
00207 
00208   if (ColourPolicy::apply(message) && PrefixPolicy::apply(message) &&
00209     send(message.getString() + '\n'))
00210     return true;
00211 
00212   return SafetyPolicy::apply(this, message);
00213 }
00214 
00215 } // namespace logger
00216 } // namespace keyvalue
00217 
00218 #endif // KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_