00001 #ifndef logging_base_log_H
00002 #define logging_base_log_H
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
00032 #ifndef __cplusplus
00033 #error This is a C++ include file and cannot be used from plain C
00034 #endif
00035
00036 #include <string>
00037 #include <lokiSmartPtr.h>
00038
00039 namespace Logging
00040 {
00041
00046 class BaseLog
00047 {
00048 public:
00049
00050 virtual ~BaseLog(){}
00051
00057 enum Priority
00058 {
00059 LM_SHUTDOWN = 01,
00061 LM_TRACE = 02,
00062
00065 LM_DEBUG = 04,
00066
00068 LM_INFO = 010,
00069
00072 LM_NOTICE = 020,
00073
00075 LM_WARNING = 040,
00076
00078 LM_ERROR = 0200,
00079
00081 LM_CRITICAL = 0400,
00082
00085 LM_ALERT = 01000,
00086
00088 LM_EMERGENCY = 02000
00089 };
00090
00091
00105 struct LogRecord {
00106 Priority priority;
00107 std::string message;
00108 std::string file;
00109 unsigned long line;
00110 std::string method;
00111 unsigned long long timeStamp;
00112 };
00113
00114
00125 virtual void
00126 log(Priority priority,
00127 const std::string &message,
00128 const std::string &file,
00129 unsigned long line,
00130 const std::string &method);
00131
00139 virtual void
00140 log(const LogRecord &lr) = 0;
00141
00142
00148 virtual std::string
00149 getName() const = 0;
00150
00155 static const std::string FIELD_UNAVAILABLE;
00156
00160 static const std::string GLOBAL_LOGGER_NAME;
00161
00165 static const std::string ANONYMOUS_LOGGER_NAME;
00166
00171 static const std::string STATIC_LOGGER_NAME;
00172 };
00173
00183 template <class P>
00184 class RefCounted
00185 {
00186 public:
00187 RefCounted()
00188 {
00189 pCount_ = new unsigned int();
00190 assert(pCount_);
00191 *pCount_ = 1;
00192 }
00193
00194 RefCounted(const RefCounted& rhs)
00195 : pCount_(rhs.pCount_)
00196 {}
00197
00198
00199 template <typename P1>
00200 RefCounted(const RefCounted<P1>& rhs)
00201 : pCount_(reinterpret_cast<const RefCounted&>(rhs).pCount_)
00202 {}
00203
00204 P Clone(const P& val)
00205 {
00206 ++*pCount_;
00207 return val;
00208 }
00209
00210 bool Release(const P&)
00211 {
00212 if (!--*pCount_)
00213 {
00214 delete pCount_;
00215 return true;
00216 }
00217 return false;
00218 }
00219
00220 void Swap(RefCounted& rhs)
00221 { std::swap(pCount_, rhs.pCount_); }
00222
00223 enum { destructiveCopy = false };
00224
00225 private:
00226
00227 unsigned int* pCount_;
00228 };
00229
00230 #ifdef MAKE_VXWORKS
00231
00232 template <class P>
00233 class RefCountedMT : public Loki::ObjectLevelLockable<RefCountedMT<P>, LOKI_DEFAULT_MUTEX >
00234 {
00235 typedef Loki::ObjectLevelLockable<RefCountedMT<P>, LOKI_DEFAULT_MUTEX > base_type;
00236 typedef typename base_type::IntType CountType;
00237 typedef volatile CountType *CountPtrType;
00238
00239 public:
00240 RefCountedMT()
00241 {
00242 pCount_ = reinterpret_cast<CountPtrType>(
00243 Loki::SmallObject<LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL>::operator new(
00244 sizeof(*pCount_)));
00245 assert(pCount_);
00246
00247 Loki::ObjectLevelLockable<RefCountedMT, LOKI_DEFAULT_MUTEX>::AtomicAssign(*pCount_, 1);
00248 }
00249
00250 RefCountedMT(const RefCountedMT& rhs) :
00251 Loki::ObjectLevelLockable<RefCountedMT<P>, LOKI_DEFAULT_MUTEX>( rhs ),
00252 pCount_(rhs.pCount_)
00253 {}
00254
00255
00256 template <typename P1>
00257 RefCountedMT(const RefCountedMT<P1>& rhs)
00258 : pCount_(reinterpret_cast<const RefCountedMT<P>&>(rhs).pCount_)
00259 {}
00260
00261 P Clone(const P& val)
00262 {
00263 Loki::ObjectLevelLockable<RefCountedMT, LOKI_DEFAULT_MUTEX>::AtomicIncrement(*pCount_);
00264 return val;
00265 }
00266
00267 bool Release(const P&)
00268 {
00269 if (! Loki::ObjectLevelLockable<RefCountedMT, LOKI_DEFAULT_MUTEX>::AtomicDecrement(*pCount_))
00270 {
00271 Loki::SmallObject<LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL>::operator delete(
00272 const_cast<CountType *>(pCount_),
00273 sizeof(*pCount_));
00274 return true;
00275 }
00276 return false;
00277 }
00278
00279 void Swap(RefCountedMT& rhs)
00280 { std::swap(pCount_, rhs.pCount_); }
00281
00282 enum { destructiveCopy = false };
00283
00284 private:
00285
00286 CountPtrType pCount_;
00287 };
00288 #endif //MAKE_VXWORKS
00289 };
00290
00291 #endif