Back to index

nordugrid-arc-nox  1.1.0~rc6
Logger.h
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifndef __ARC_LOGGER__
00004 #define __ARC_LOGGER__
00005 
00006 #include <string>
00007 #include <iostream>
00008 #include <fstream>
00009 
00010 #include <arc/Thread.h>
00011 #include <arc/IString.h>
00012 
00013 namespace Arc {
00014 
00016 
00029   enum LogLevel {
00030     DEBUG = 1,
00031     VERBOSE = 2,
00032     INFO = 4,
00033     WARNING = 8,
00034     ERROR = 16,
00035     FATAL = 32
00036   };
00037 
00038   enum LogFormat {
00039     LongFormat,
00040     ShortFormat
00041   };
00042 
00043   struct LoggerFormat {
00044     LoggerFormat(LogFormat format)
00045       : format(format) {};
00046     LogFormat format;
00047   };
00048 
00049   std::ostream& operator<<(std::ostream& os, const LoggerFormat& format);
00050 
00052 
00055   std::ostream& operator<<(std::ostream& os, LogLevel level);
00057   LogLevel string_to_level(const std::string& str);
00059 
00073   bool istring_to_level(const std::string& llStr, LogLevel& ll);
00075   bool string_to_level(const std::string& str, LogLevel& ll);
00077   std::string level_to_string(const LogLevel& level);
00078 
00079 
00080 
00082 
00086   class LogMessage {
00087   public:
00088 
00090 
00098     LogMessage(LogLevel level,
00099                const IString& message);
00100 
00102 
00110     LogMessage(LogLevel level,
00111                const IString& message,
00112                const std::string& identifier);
00113 
00115 
00118     LogLevel getLevel() const;
00119 
00120   protected:
00121 
00123 
00127     void setIdentifier(std::string identifier);
00128 
00129   private:
00130 
00132 
00137     static std::string getDefaultIdentifier();
00138 
00140 
00144     void setDomain(std::string domain);
00145 
00147     std::string time;
00148 
00150     LogLevel level;
00151 
00153     std::string domain;
00154 
00156     std::string identifier;
00157 
00159     IString message;
00160 
00162 
00165     friend std::ostream& operator<<(std::ostream& os,
00166                                     const LogMessage& message);
00167 
00169 
00172     friend class Logger;
00173 
00174   };
00175 
00176 
00177 
00179 
00183   class LogDestination {
00184   public:
00185 
00187     virtual void log(const LogMessage& message) = 0;
00188 
00189     virtual ~LogDestination() {}
00190 
00191     void setFormat(const LogFormat& newformat);
00192 
00193   protected:
00194 
00196 
00198     LogDestination();
00199 
00201 
00203     LogDestination(const std::string& locale);
00204 
00205   private:
00206 
00208 
00211     LogDestination(const LogDestination& unique);
00212 
00214 
00217     void operator=(const LogDestination& unique);
00218 
00219   protected:
00220 
00221     std::string locale;
00222     LogFormat format;
00223   };
00224 
00225 
00226 
00228 
00236   class LogStream
00237     : public LogDestination {
00238   public:
00239 
00241 
00246     LogStream(std::ostream& destination);
00247 
00249 
00252     LogStream(std::ostream& destination, const std::string& locale);
00253 
00255 
00260     virtual void log(const LogMessage& message);
00261 
00262   private:
00263 
00265 
00268     LogStream(const LogStream& unique);
00269 
00271 
00274     void operator=(const LogStream& unique);
00275 
00277 
00280     std::ostream& destination;
00281 
00283 
00288     Glib::Mutex mutex;
00289 
00290   };
00291 
00293 
00303   class LogFile
00304     : public LogDestination {
00305   public:
00306 
00308 
00314     LogFile(const std::string& path);
00315 
00317 
00321     LogFile(const std::string& path, const std::string& locale);
00322 
00324 
00329     void setMaxSize(int newsize);
00330 
00332 
00338     void setBackups(int newbackup);
00339 
00341     operator bool(void);
00342 
00344     bool operator!(void);
00345 
00347 
00353     virtual void log(const LogMessage& message);
00354   private:
00355     LogFile(void);
00356     LogFile(const LogFile& unique);
00357     void operator=(const LogFile& unique);
00358     void backup(void);
00359     std::string path;
00360     std::ofstream destination;
00361     int maxsize;
00362     int backups;
00363     Glib::Mutex mutex;
00364   };
00365 
00367 
00383   class Logger {
00384   public:
00385 
00387 
00390     //static Logger rootLogger;
00391     static Logger& getRootLogger();
00392 
00394 
00399     Logger(Logger& parent,
00400            const std::string& subdomain);
00401 
00403 
00408     Logger(Logger& parent,
00409            const std::string& subdomain,
00410            LogLevel threshold);
00411 
00413 
00415     ~Logger();
00416 
00418 
00425     void addDestination(LogDestination& destination);
00426 
00428     void removeDestinations(void);
00429 
00431 
00436     void setThreshold(LogLevel threshold);
00437 
00439 
00442     LogLevel getThreshold() const;
00443 
00445 
00448     void msg(LogMessage message);
00449 
00451 
00457     void msg(LogLevel level, const std::string& str) {
00458       msg(LogMessage(level, IString(str)));
00459     }
00460 
00461     template<class T0>
00462     void msg(LogLevel level, const std::string& str,
00463              const T0& t0) {
00464       msg(LogMessage(level, IString(str, t0)));
00465     }
00466 
00467     template<class T0, class T1>
00468     void msg(LogLevel level, const std::string& str,
00469              const T0& t0, const T1& t1) {
00470       msg(LogMessage(level, IString(str, t0, t1)));
00471     }
00472 
00473     template<class T0, class T1, class T2>
00474     void msg(LogLevel level, const std::string& str,
00475              const T0& t0, const T1& t1, const T2& t2) {
00476       msg(LogMessage(level, IString(str, t0, t1, t2)));
00477     }
00478 
00479     template<class T0, class T1, class T2, class T3>
00480     void msg(LogLevel level, const std::string& str,
00481              const T0& t0, const T1& t1, const T2& t2, const T3& t3) {
00482       msg(LogMessage(level, IString(str, t0, t1, t2, t3)));
00483     }
00484 
00485     template<class T0, class T1, class T2, class T3, class T4>
00486     void msg(LogLevel level, const std::string& str,
00487              const T0& t0, const T1& t1, const T2& t2, const T3& t3,
00488              const T4& t4) {
00489       msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4)));
00490     }
00491 
00492     template<class T0, class T1, class T2, class T3, class T4,
00493              class T5>
00494     void msg(LogLevel level, const std::string& str,
00495              const T0& t0, const T1& t1, const T2& t2, const T3& t3,
00496              const T4& t4, const T5& t5) {
00497       msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5)));
00498     }
00499 
00500     template<class T0, class T1, class T2, class T3, class T4,
00501              class T5, class T6>
00502     void msg(LogLevel level, const std::string& str,
00503              const T0& t0, const T1& t1, const T2& t2, const T3& t3,
00504              const T4& t4, const T5& t5, const T6& t6) {
00505       msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6)));
00506     }
00507 
00508     template<class T0, class T1, class T2, class T3, class T4,
00509              class T5, class T6, class T7>
00510     void msg(LogLevel level, const std::string& str,
00511              const T0& t0, const T1& t1, const T2& t2, const T3& t3,
00512              const T4& t4, const T5& t5, const T6& t6, const T7& t7) {
00513       msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6, t7)));
00514     }
00515 
00516   private:
00517 
00519 
00524     Logger();
00525 
00527 
00530     Logger(const Logger& unique);
00531 
00533 
00536     void operator=(const Logger& unique);
00537 
00539 
00543     std::string getDomain();
00544 
00546 
00552     void log(const LogMessage& message);
00553 
00555     Logger *parent;
00556 
00558     std::string domain;
00559 
00561     std::list<LogDestination*> destinations;
00562 
00564     LogLevel threshold;
00565 
00566 #define rootLoggerMagic (0xF6569201)
00567     static Logger *rootLogger;
00568     static unsigned int rootLoggerMark;
00569   };
00570 
00571 } // namespace Arc
00572 
00573 #define rootLogger getRootLogger()
00574 
00575 #define LOG(LGR, THR, FSTR, ...) { if ((LGR).getThreshold() >= (THR)(LGR).msg((THR), (FSTR), ...); }
00576 
00577 #endif // __ARC_LOGGER__