ARC SDK
Logger.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef __ARC_LOGGER__
4 #define __ARC_LOGGER__
5 
6 #include <string>
7 #include <list>
8 #include <map>
9 #include <iostream>
10 #include <fstream>
11 
12 #include <arc/Thread.h>
13 #include <arc/IString.h>
14 
15 namespace Arc {
16 
19 
20  enum LogLevel {
21  DEBUG = 1,
22 
23  VERBOSE = 2,
24 
25  INFO = 4,
26 
27  WARNING = 8,
28  ERROR = 16,
29 
30  FATAL = 32
31 
32  };
33 
35  enum LogFormat {
39 
40 
42  };
43 
45  struct LoggerFormat {
48  : format(format) {};
49  LogFormat format;
50  };
51 
53  std::ostream& operator<<(std::ostream& os, const LoggerFormat& format);
54 
56 
59  std::ostream& operator<<(std::ostream& os, LogLevel level);
61  LogLevel string_to_level(const std::string& str);
63 
77  bool istring_to_level(const std::string& llStr, LogLevel& ll);
79  bool string_to_level(const std::string& str, LogLevel& ll);
81  std::string level_to_string(const LogLevel& level);
83  LogLevel old_level_to_level(unsigned int old_level);
84 
85 
86 
88 
93  class LogMessage {
94  public:
95 
97 
105  LogMessage(LogLevel level,
106  const IString& message);
107 
109 
117  LogMessage(LogLevel level,
118  const IString& message,
119  const std::string& identifier);
120 
122 
125  LogLevel getLevel() const;
126 
127  protected:
128 
130 
134  void setIdentifier(std::string identifier);
135 
136  private:
137 
139 
144  static std::string getDefaultIdentifier();
145 
147 
151  void setDomain(std::string domain);
152 
154  std::string time;
155 
157  LogLevel level;
158 
160  std::string domain;
161 
163  std::string identifier;
164 
166  IString message;
167 
169 
172  friend std::ostream& operator<<(std::ostream& os,
173  const LogMessage& message);
174 
176 
179  friend class Logger;
180 
181  };
182 
183 
184 
186 
194  public:
195 
197  virtual void log(const LogMessage& message) = 0;
198 
199  virtual ~LogDestination() {}
200 
202  void setFormat(const LogFormat& newformat);
203 
204  protected:
205 
207  LogDestination();
208 
209  private:
210 
212 
215  LogDestination(const LogDestination& unique);
216 
218 
221  void operator=(const LogDestination& unique);
222 
223  protected:
226  };
227 
228 
229 
231 
240  class LogStream
241  : public LogDestination {
242  public:
243 
245 
250  LogStream(std::ostream& destination);
251 
253 
258  virtual void log(const LogMessage& message);
259 
260  private:
261 
263 
266  LogStream(const LogStream& unique);
267 
269 
272  void operator=(const LogStream& unique);
273 
275 
278  std::ostream& destination;
279 
281 
286  Glib::Mutex mutex;
287 
288  };
289 
291 
302  class LogFile
303  : public LogDestination {
304  public:
305 
307 
313  LogFile(const std::string& path);
314 
316 
321  void setMaxSize(int newsize);
322 
324 
330  void setBackups(int newbackup);
331 
333 
337  void setReopen(bool newreopen);
338 
340  operator bool(void);
341 
343  bool operator!(void);
344 
346 
352  virtual void log(const LogMessage& message);
353  private:
354  LogFile(void);
355  LogFile(const LogFile& unique);
356  void operator=(const LogFile& unique);
357  void backup(void);
358  std::string path;
359  std::ofstream destination;
360  int maxsize;
361  int backups;
362  bool reopen;
363  Glib::Mutex mutex;
364  };
365 
366  class LoggerContextRef;
367 
370  class LoggerContext {
371  friend class Logger;
372  friend class LoggerContextRef;
373  private:
375  int usage_count;
376 
378  Glib::Mutex mutex;
379 
381  std::list<LogDestination*> destinations;
382 
384  LogLevel threshold;
385 
386  LoggerContext(LogLevel thr):usage_count(0),threshold(thr) { };
387 
388  LoggerContext(const LoggerContext& ctx):
389  usage_count(0),destinations(ctx.destinations),threshold(ctx.threshold) { };
390 
391  ~LoggerContext(void);
392 
393  void Acquire(void);
394 
395  void Release(void);
396  };
400 
401 
440  class Logger {
441  public:
442 
444 
447  //static Logger rootLogger;
448  static Logger& getRootLogger();
449 
451 
455  Logger(Logger& parent,
456  const std::string& subdomain);
457 
459 
463  Logger(Logger& parent,
464  const std::string& subdomain,
465  LogLevel threshold);
466 
468  ~Logger();
469 
471 
478  void addDestination(LogDestination& destination);
479 
481 
483  void addDestinations(const std::list<LogDestination*>& destinations);
484 
486 
488  void setDestinations(const std::list<LogDestination*>& destinations);
489 
491 
495  const std::list<LogDestination*>& getDestinations(void) const;
496 
498  void removeDestinations(void);
499 
501  void deleteDestinations(void);
502 
504 
509  void setThreshold(LogLevel threshold);
510 
512 
519  static void setThresholdForDomain(LogLevel threshold,
520  const std::list<std::string>& subdomains);
521 
523 
530  static void setThresholdForDomain(LogLevel threshold,
531  const std::string& domain);
532 
534  LogLevel getThreshold() const;
535 
537 
546  void setThreadContext(void);
547 
549 
551  void msg(LogMessage message);
552 
554 
565  void msg(LogLevel level, const std::string& str) {
566  msg(LogMessage(level, IString(str)));
567  }
568 
569  template<class T0>
570  void msg(LogLevel level, const std::string& str,
571  const T0& t0) {
572  msg(LogMessage(level, IString(str, t0)));
573  }
574 
575  template<class T0, class T1>
576  void msg(LogLevel level, const std::string& str,
577  const T0& t0, const T1& t1) {
578  msg(LogMessage(level, IString(str, t0, t1)));
579  }
580 
581  template<class T0, class T1, class T2>
582  void msg(LogLevel level, const std::string& str,
583  const T0& t0, const T1& t1, const T2& t2) {
584  msg(LogMessage(level, IString(str, t0, t1, t2)));
585  }
586 
587  template<class T0, class T1, class T2, class T3>
588  void msg(LogLevel level, const std::string& str,
589  const T0& t0, const T1& t1, const T2& t2, const T3& t3) {
590  msg(LogMessage(level, IString(str, t0, t1, t2, t3)));
591  }
592 
593  template<class T0, class T1, class T2, class T3, class T4>
594  void msg(LogLevel level, const std::string& str,
595  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
596  const T4& t4) {
597  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4)));
598  }
599 
600  template<class T0, class T1, class T2, class T3, class T4,
601  class T5>
602  void msg(LogLevel level, const std::string& str,
603  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
604  const T4& t4, const T5& t5) {
605  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5)));
606  }
607 
608  template<class T0, class T1, class T2, class T3, class T4,
609  class T5, class T6>
610  void msg(LogLevel level, const std::string& str,
611  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
612  const T4& t4, const T5& t5, const T6& t6) {
613  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6)));
614  }
615 
616  template<class T0, class T1, class T2, class T3, class T4,
617  class T5, class T6, class T7>
618  void msg(LogLevel level, const std::string& str,
619  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
620  const T4& t4, const T5& t5, const T6& t6, const T7& t7) {
621  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6, t7)));
622  }
623 
624  private:
625 
627 
632  Logger();
633 
635 
638  Logger(const Logger& unique);
639 
641 
644  void operator=(const Logger& unique);
645 
647 
651  std::string getDomain();
652 
654 
660  void log(const LogMessage& message);
661 
663  Logger *parent;
664 
666  std::string domain;
667 
669  std::string context_id;
670 
671  LoggerContext context;
672 
673  LoggerContext& getContext(void);
674 
675  Glib::Mutex mutex;
676 
677 #define rootLoggerMagic (0xF6569201)
678  static Logger *rootLogger;
679  static std::map<std::string,LogLevel>* defaultThresholds;
680  static unsigned int rootLoggerMark;
681  };
682 
685 } // namespace Arc
686 
687 #define rootLogger getRootLogger()
688 
689 #define LOG(LGR, THR, FSTR, ...) { if ((LGR).getThreshold() >= (THR)(LGR).msg((THR), (FSTR), ...); }
690 
691 #endif // __ARC_LOGGER__