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/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 };
00132
00133
00134
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
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
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
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
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
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 }
00212 }
00213
00214 #endif // KEYVALUE_SYS_LOGGER_LOGGERIMPL_H_