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  * 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/IgnoreColor.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 ColorPolicy = IgnoreColor,
00064   typename SafetyPolicy = ForwardToGlobalLogger>
00065 
00066 class LoggerImpl : public Logger, private PrefixPolicy, private ColorPolicy,
00067   private SafetyPolicy {
00068 
00069 public:
00070 
00071   typedef LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy> LoggerImpl_;
00072 
00073   unsigned int
00074   getLevel() const;
00075 
00076   void
00077   setLevel(unsigned int level);
00078 
00079   bool
00080   log(const Message& message);
00081 
00082 protected:
00083 
00084   LoggerImpl(unsigned int level);
00085 
00094   bool
00095   sendHeader();
00096 
00097 private:
00098 
00099   unsigned int level_;
00100 
00116   bool
00117   process(const Message& message);
00118 
00128   virtual bool
00129   send(const string& message) = 0;
00130 
00131 }; // class LoggerImpl
00132 
00133 /*--------------------------------------------------------------------------
00134  * getLevel()
00135  *------------------------------------------------------------------------*/
00136 
00137 template <typename PrefixPolicy, typename ColorPolicy,
00138   typename SafetyPolicy>
00139 unsigned int
00140 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::getLevel() const {
00141   return level_;
00142 }
00143 
00144 /*--------------------------------------------------------------------------
00145  * setLevel()
00146  *------------------------------------------------------------------------*/
00147 
00148 template <typename PrefixPolicy, typename ColorPolicy,
00149   typename SafetyPolicy>
00150 void
00151 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::setLevel
00152   (const unsigned int level) {
00153 
00154   level_ = level;
00155 }
00156 
00157 /*--------------------------------------------------------------------------
00158  * log()
00159  *------------------------------------------------------------------------*/
00160 
00161 template <typename PrefixPolicy, typename ColorPolicy,
00162   typename SafetyPolicy>
00163 bool
00164 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::log
00165   (const Message& message) {
00166 
00167   if (!dynamic_cast<const Error *>(&message) && message.getLevel() >=level_)
00168     return true;
00169   return process(message);
00170 }
00171 
00172 /*--------------------------------------------------------------------------
00173  * LoggerImpl()
00174  *------------------------------------------------------------------------*/
00175 
00176 template <typename PrefixPolicy, typename ColorPolicy,
00177   typename SafetyPolicy>
00178 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::LoggerImpl
00179   (const unsigned int level) : level_(level) {
00180 }
00181 
00182 /*--------------------------------------------------------------------------
00183  * sendHeader()
00184  *------------------------------------------------------------------------*/
00185 
00186 template <typename PrefixPolicy, typename ColorPolicy,
00187   typename SafetyPolicy>
00188 bool
00189 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::sendHeader() {
00190   return send(util::Global<Bridge>::get()->getCompleteInfo()) &&
00191     send("\n\n");
00192 }
00193 
00194 /*--------------------------------------------------------------------------
00195  * process()
00196  *------------------------------------------------------------------------*/
00197 
00198 template <typename PrefixPolicy, typename ColorPolicy,
00199   typename SafetyPolicy>
00200 bool
00201 LoggerImpl<PrefixPolicy, ColorPolicy, SafetyPolicy>::process
00202   (const Message& message) {
00203 
00204   if (ColorPolicy::apply(message) && PrefixPolicy::apply(message) &&
00205     send(message.getString() + '\n'))
00206     return true;
00207 
00208   return SafetyPolicy::apply(this, message);
00209 }
00210 
00211 } // namespace logger
00212 } // namespace keyvalue
00213 
00214 #endif // KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_