LoggerImpl.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2010 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  **************************************************************************/
00021 
00029 #ifndef KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_
00030 #define KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_
00031 
00032 #include "keyvalue/bridge/Bridge.h"
00033 #include "keyvalue/sys/logger/Logger.h"
00034 #include "keyvalue/sys/logger/policy/AddPrefix.h"
00035 #include "keyvalue/sys/logger/policy/ForwardToGlobalLogger.h"
00036 #include "keyvalue/sys/logger/policy/IgnoreColor.h"
00037 #include "keyvalue/sys/message/MessageImpl.h"
00038 
00039 namespace keyvalue {
00040 namespace logger {
00041 
00055 template <typename PrefixPolicy = AddPrefix,
00056   typename ColorPolicy = IgnoreColor,
00057   typename SafetyPolicy = ForwardToGlobalLogger>
00058 
00059 class LoggerImpl : public Logger, private PrefixPolicy, private ColorPolicy,
00060   private SafetyPolicy {
00061 
00062 public:
00063 
00064   typedef LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy> LoggerImpl_;
00065 
00066   unsigned int
00067   getLevel() const;
00068 
00069   void
00070   setLevel(unsigned int level);
00071 
00072   bool
00073   log(const Message& message);
00074 
00075 protected:
00076 
00077   LoggerImpl(unsigned int level);
00078 
00087   bool
00088   sendHeader();
00089 
00090 private:
00091 
00092   unsigned int level_;
00093 
00109   bool
00110   process(const Message& message);
00111 
00121   virtual bool
00122   send(const string& message) = 0;
00123 
00124 }; // class LoggerImpl
00125 
00126 /*--------------------------------------------------------------------------
00127  * getLevel()
00128  *------------------------------------------------------------------------*/
00129 
00130 template <typename PrefixPolicy, typename ColorPolicy,
00131   typename SafetyPolicy>
00132 unsigned int
00133 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::getLevel() const {
00134   return level_;
00135 }
00136 
00137 /*--------------------------------------------------------------------------
00138  * setLevel()
00139  *------------------------------------------------------------------------*/
00140 
00141 template <typename PrefixPolicy, typename ColorPolicy,
00142   typename SafetyPolicy>
00143 void
00144 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::setLevel
00145   (const unsigned int level) {
00146 
00147   level_ = level;
00148 }
00149 
00150 /*--------------------------------------------------------------------------
00151  * log()
00152  *------------------------------------------------------------------------*/
00153 
00154 template <typename PrefixPolicy, typename ColorPolicy,
00155   typename SafetyPolicy>
00156 bool
00157 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::log
00158   (const Message& message) {
00159 
00160   if (!dynamic_cast<const Error *>(&message) && message.getLevel() >=level_)
00161     return true;
00162   return process(message);
00163 }
00164 
00165 /*--------------------------------------------------------------------------
00166  * LoggerImpl()
00167  *------------------------------------------------------------------------*/
00168 
00169 template <typename PrefixPolicy, typename ColorPolicy,
00170   typename SafetyPolicy>
00171 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::LoggerImpl
00172   (const unsigned int level) : level_(level) {
00173 }
00174 
00175 /*--------------------------------------------------------------------------
00176  * sendHeader()
00177  *------------------------------------------------------------------------*/
00178 
00179 template <typename PrefixPolicy, typename ColorPolicy,
00180   typename SafetyPolicy>
00181 bool
00182 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::sendHeader() {
00183   return send(Bridge::getInstance().getCompleteInfo()) && send("\n\n");
00184 }
00185 
00186 /*--------------------------------------------------------------------------
00187  * process()
00188  *------------------------------------------------------------------------*/
00189 
00190 template <typename PrefixPolicy, typename ColorPolicy,
00191   typename SafetyPolicy>
00192 bool
00193 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::process
00194   (const Message& message) {
00195 
00196   if (ColorPolicy::apply(message) && PrefixPolicy::apply(message) &&
00197     send(message.getString() + '\n'))
00198     return true;
00199 
00200   return SafetyPolicy::apply(this, message);
00201 }
00202 
00203 } // namespace logger
00204 } // namespace keyvalue
00205 
00206 #endif // KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_

Generated on Sat Mar 20 15:08:29 2010 for KeyValue by  doxygen 1.6.1