Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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 };
00136
00137
00138
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
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
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
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
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
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 }
00216 }
00217
00218 #endif // KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_