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 {
40 
41 
43  };
44 
46  struct LoggerFormat {
49  : format(format) {};
50  LogFormat format;
51  };
52 
54  std::ostream& operator<<(std::ostream& os, const LoggerFormat& format);
55 
57 
60  std::ostream& operator<<(std::ostream& os, LogLevel level);
62  LogLevel string_to_level(const std::string& str);
64 
78  bool istring_to_level(const std::string& llStr, LogLevel& ll);
80  bool string_to_level(const std::string& str, LogLevel& ll);
82  std::string level_to_string(const LogLevel& level);
84  LogLevel old_level_to_level(unsigned int old_level);
85 
86 
87 
89 
94  class LogMessage {
95  public:
96 
98 
106  LogMessage(LogLevel level,
107  const IString& message);
108 
110 
118  LogMessage(LogLevel level,
119  const IString& message,
120  const std::string& identifier);
121 
123 
126  LogLevel getLevel() const;
127 
128  protected:
129 
131 
135  void setIdentifier(std::string identifier);
136 
137  private:
138 
140 
145  static std::string getDefaultIdentifier();
146 
148 
152  void setDomain(std::string domain);
153 
155  std::string time;
156 
158  LogLevel level;
159 
161  std::string domain;
162 
164  std::string identifier;
165 
167  IString message;
168 
170 
173  friend std::ostream& operator<<(std::ostream& os,
174  const LogMessage& message);
175 
177 
180  friend class Logger;
181 
182  };
183 
184 
185 
187 
195  public:
196 
198  virtual void log(const LogMessage& message) = 0;
199 
200  virtual ~LogDestination() {}
201 
203  void setFormat(const LogFormat& newformat);
204 
206 
208  void setPrefix(const std::string& prefix);
209 
210  protected:
211 
213  LogDestination();
214 
215  private:
216 
218 
221  LogDestination(const LogDestination& unique);
222 
224 
227  void operator=(const LogDestination& unique);
228 
230  friend std::ostream& operator<<(std::ostream& os, const LogDestination& dest);
231 
232  protected:
235 
237  std::string prefix;
238  };
239 
240 
241 
243 
252  class LogStream
253  : public LogDestination {
254  public:
255 
257 
262  LogStream(std::ostream& destination);
263 
265 
270  virtual void log(const LogMessage& message);
271 
272  private:
273 
275 
278  LogStream(const LogStream& unique);
279 
281 
284  void operator=(const LogStream& unique);
285 
287 
290  std::ostream& destination;
291 
293 
298  Glib::Mutex mutex;
299 
300  };
301 
303 
314  class LogFile
315  : public LogDestination {
316  public:
317 
319 
325  LogFile(const std::string& path);
326 
328 
333  void setMaxSize(int newsize);
334 
336 
342  void setBackups(int newbackup);
343 
345 
349  void setReopen(bool newreopen);
350 
352  operator bool(void);
353 
355  bool operator!(void);
356 
358 
364  virtual void log(const LogMessage& message);
365  private:
366  LogFile(void);
367  LogFile(const LogFile& unique);
368  void operator=(const LogFile& unique);
369  void backup(void);
370  std::string path;
371  std::ofstream destination;
372  int maxsize;
373  int backups;
374  bool reopen;
375  Glib::Mutex mutex;
376  };
377 
378  class LoggerContextRef;
379 
382  class LoggerContext {
383  friend class Logger;
384  friend class LoggerContextRef;
385  private:
387  int usage_count;
388 
390  Glib::Mutex mutex;
391 
393  std::list<LogDestination*> destinations;
394 
396  LogLevel threshold;
397 
398  LoggerContext(LogLevel thr):usage_count(0),threshold(thr) { };
399 
400  LoggerContext(const LoggerContext& ctx):
401  usage_count(0),destinations(ctx.destinations),threshold(ctx.threshold) { };
402 
403  ~LoggerContext(void);
404 
405  void Acquire(void);
406 
407  void Release(void);
408  };
412 
413 
452  class Logger {
453  public:
454 
456 
459  //static Logger rootLogger;
460  static Logger& getRootLogger();
461 
463 
467  Logger(Logger& parent,
468  const std::string& subdomain);
469 
471 
475  Logger(Logger& parent,
476  const std::string& subdomain,
477  LogLevel threshold);
478 
480  ~Logger();
481 
483 
490  void addDestination(LogDestination& destination);
491 
493 
495  void addDestinations(const std::list<LogDestination*>& destinations);
496 
498 
500  void setDestinations(const std::list<LogDestination*>& destinations);
501 
503 
507  const std::list<LogDestination*>& getDestinations(void) const;
508 
510  void removeDestinations(void);
511 
513  void deleteDestinations(void);
514 
516 
521  void setThreshold(LogLevel threshold);
522 
524 
531  static void setThresholdForDomain(LogLevel threshold,
532  const std::list<std::string>& subdomains);
533 
535 
542  static void setThresholdForDomain(LogLevel threshold,
543  const std::string& domain);
544 
546  LogLevel getThreshold() const;
547 
549 
558  void setThreadContext(void);
559 
561 
563  void msg(LogMessage message);
564 
566 
577  void msg(LogLevel level, const std::string& str) {
578  msg(LogMessage(level, IString(str)));
579  }
580 
581  template<class T0>
582  void msg(LogLevel level, const std::string& str,
583  const T0& t0) {
584  msg(LogMessage(level, IString(str, t0)));
585  }
586 
587  template<class T0, class T1>
588  void msg(LogLevel level, const std::string& str,
589  const T0& t0, const T1& t1) {
590  msg(LogMessage(level, IString(str, t0, t1)));
591  }
592 
593  template<class T0, class T1, class T2>
594  void msg(LogLevel level, const std::string& str,
595  const T0& t0, const T1& t1, const T2& t2) {
596  msg(LogMessage(level, IString(str, t0, t1, t2)));
597  }
598 
599  template<class T0, class T1, class T2, class T3>
600  void msg(LogLevel level, const std::string& str,
601  const T0& t0, const T1& t1, const T2& t2, const T3& t3) {
602  msg(LogMessage(level, IString(str, t0, t1, t2, t3)));
603  }
604 
605  template<class T0, class T1, class T2, class T3, class T4>
606  void msg(LogLevel level, const std::string& str,
607  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
608  const T4& t4) {
609  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4)));
610  }
611 
612  template<class T0, class T1, class T2, class T3, class T4,
613  class T5>
614  void msg(LogLevel level, const std::string& str,
615  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
616  const T4& t4, const T5& t5) {
617  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5)));
618  }
619 
620  template<class T0, class T1, class T2, class T3, class T4,
621  class T5, class T6>
622  void msg(LogLevel level, const std::string& str,
623  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
624  const T4& t4, const T5& t5, const T6& t6) {
625  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6)));
626  }
627 
628  template<class T0, class T1, class T2, class T3, class T4,
629  class T5, class T6, class T7>
630  void msg(LogLevel level, const std::string& str,
631  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
632  const T4& t4, const T5& t5, const T6& t6, const T7& t7) {
633  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6, t7)));
634  }
635 
636  private:
637 
639 
644  Logger();
645 
647 
650  Logger(const Logger& unique);
651 
653 
656  void operator=(const Logger& unique);
657 
659 
663  std::string getDomain();
664 
666 
672  void log(const LogMessage& message);
673 
675  Logger *parent;
676 
678  std::string domain;
679 
681  std::string context_id;
682 
683  LoggerContext context;
684 
685  LoggerContext& getContext(void);
686 
687  Glib::Mutex mutex;
688 
689 #define rootLoggerMagic (0xF6569201)
690  static Logger *rootLogger;
691  static std::map<std::string,LogLevel>* defaultThresholds;
692  static unsigned int rootLoggerMark;
693  };
694 
697 } // namespace Arc
698 
699 #define rootLogger getRootLogger()
700 
701 #define LOG(LGR, THR, FSTR, ...) { if ((LGR).getThreshold() >= (THR)(LGR).msg((THR), (FSTR), ...); }
702 
703 #endif // __ARC_LOGGER__