LoggerImpl.h
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
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 };
00125
00126
00127
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
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
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
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
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
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 }
00204 }
00205
00206 #endif // KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_