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 
51  };
52 
54  struct LoggerFormat {
57  : format(format) {};
58  LogFormat format;
59  };
60 
62  std::ostream& operator<<(std::ostream& os, const LoggerFormat& format);
63 
65 
68  std::ostream& operator<<(std::ostream& os, LogLevel level);
70  LogLevel string_to_level(const std::string& str);
72 
86  bool istring_to_level(const std::string& llStr, LogLevel& ll);
88  bool string_to_level(const std::string& str, LogLevel& ll);
90  std::string level_to_string(const LogLevel& level);
92  LogLevel old_level_to_level(unsigned int old_level);
93 
94 
95 
97 
102  class LogMessage {
103  public:
104 
106 
114  LogMessage(LogLevel level,
115  const IString& message);
116 
118 
126  LogMessage(LogLevel level,
127  const IString& message,
128  const std::string& identifier);
129 
131 
134  LogLevel getLevel() const;
135 
136  protected:
137 
139 
143  void setIdentifier(std::string identifier);
144 
145  private:
146 
148 
153  static std::string getDefaultIdentifier();
154 
156 
160  void setDomain(std::string domain);
161 
163  std::string time;
164 
166  LogLevel level;
167 
169  std::string domain;
170 
172  std::string identifier;
173 
175  IString message;
176 
178 
181  friend std::ostream& operator<<(std::ostream& os,
182  const LogMessage& message);
183 
185 
188  friend class Logger;
189 
190  };
191 
192 
193 
195 
203  public:
204 
206  virtual void log(const LogMessage& message) = 0;
207 
208  virtual ~LogDestination() {}
209 
211  void setFormat(const LogFormat& newformat);
212 
214 
217  void setPrefix(const std::string& prefix);
218 
219  protected:
220 
222  LogDestination();
223 
224  private:
225 
227 
230  LogDestination(const LogDestination& unique);
231 
233 
236  void operator=(const LogDestination& unique);
237 
239  friend std::ostream& operator<<(std::ostream& os, const LogDestination& dest);
240 
241  protected:
244 
246 
249  std::string prefix;
250  };
251 
252 
253 
255 
264  class LogStream
265  : public LogDestination {
266  public:
267 
269 
274  LogStream(std::ostream& destination);
275 
277 
282  virtual void log(const LogMessage& message);
283 
284  private:
285 
287 
290  LogStream(const LogStream& unique);
291 
293 
296  void operator=(const LogStream& unique);
297 
299 
302  std::ostream& destination;
303 
305 
310  Glib::Mutex mutex;
311 
312  };
313 
315 
326  class LogFile
327  : public LogDestination {
328  public:
329 
331 
337  LogFile(const std::string& path);
338 
340 
345  void setMaxSize(int newsize);
346 
348 
354  void setBackups(int newbackup);
355 
357 
361  void setReopen(bool newreopen);
362 
364  operator bool(void);
365 
367  bool operator!(void);
368 
370 
376  virtual void log(const LogMessage& message);
377  private:
378  LogFile(void);
379  LogFile(const LogFile& unique);
380  void operator=(const LogFile& unique);
381  void backup(void);
382  std::string path;
383  std::ofstream destination;
384  int maxsize;
385  int backups;
386  bool reopen;
387  Glib::Mutex mutex;
388  };
389 
390  class LoggerContextRef;
391 
394  class LoggerContext {
395  friend class Logger;
396  friend class LoggerContextRef;
397  private:
399  int usage_count;
400 
402  Glib::Mutex mutex;
403 
405  std::list<LogDestination*> destinations;
406 
408  LogLevel threshold;
409 
410  LoggerContext(LogLevel thr):usage_count(0),threshold(thr) { };
411 
412  LoggerContext(const LoggerContext& ctx):
413  usage_count(0),destinations(ctx.destinations),threshold(ctx.threshold) { };
414 
415  ~LoggerContext(void);
416 
417  void Acquire(void);
418 
419  void Release(void);
420  };
424 
425 
464  class Logger {
465  public:
466 
468 
471  //static Logger rootLogger;
472  static Logger& getRootLogger();
473 
475 
479  Logger(Logger& parent,
480  const std::string& subdomain);
481 
483 
487  Logger(Logger& parent,
488  const std::string& subdomain,
489  LogLevel threshold);
490 
492  ~Logger();
493 
495 
502  void addDestination(LogDestination& destination);
503 
505 
507  void addDestinations(const std::list<LogDestination*>& destinations);
508 
510 
512  void setDestinations(const std::list<LogDestination*>& destinations);
513 
515 
519  const std::list<LogDestination*>& getDestinations(void) const;
520 
522  void removeDestinations(void);
523 
525  void deleteDestinations(void);
526 
528 
533  void setThreshold(LogLevel threshold);
534 
536 
543  static void setThresholdForDomain(LogLevel threshold,
544  const std::list<std::string>& subdomains);
545 
547 
554  static void setThresholdForDomain(LogLevel threshold,
555  const std::string& domain);
556 
558  LogLevel getThreshold() const;
559 
561 
570  void setThreadContext(void);
571 
573 
575  void msg(LogMessage message);
576 
578 
589  void msg(LogLevel level, const std::string& str) {
590  msg(LogMessage(level, IString(str)));
591  }
592 
593  template<class T0>
594  void msg(LogLevel level, const std::string& str,
595  const T0& t0) {
596  msg(LogMessage(level, IString(str, t0)));
597  }
598 
599  template<class T0, class T1>
600  void msg(LogLevel level, const std::string& str,
601  const T0& t0, const T1& t1) {
602  msg(LogMessage(level, IString(str, t0, t1)));
603  }
604 
605  template<class T0, class T1, class T2>
606  void msg(LogLevel level, const std::string& str,
607  const T0& t0, const T1& t1, const T2& t2) {
608  msg(LogMessage(level, IString(str, t0, t1, t2)));
609  }
610 
611  template<class T0, class T1, class T2, class T3>
612  void msg(LogLevel level, const std::string& str,
613  const T0& t0, const T1& t1, const T2& t2, const T3& t3) {
614  msg(LogMessage(level, IString(str, t0, t1, t2, t3)));
615  }
616 
617  template<class T0, class T1, class T2, class T3, class T4>
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) {
621  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4)));
622  }
623 
624  template<class T0, class T1, class T2, class T3, class T4,
625  class T5>
626  void msg(LogLevel level, const std::string& str,
627  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
628  const T4& t4, const T5& t5) {
629  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5)));
630  }
631 
632  template<class T0, class T1, class T2, class T3, class T4,
633  class T5, class T6>
634  void msg(LogLevel level, const std::string& str,
635  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
636  const T4& t4, const T5& t5, const T6& t6) {
637  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6)));
638  }
639 
640  template<class T0, class T1, class T2, class T3, class T4,
641  class T5, class T6, class T7>
642  void msg(LogLevel level, const std::string& str,
643  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
644  const T4& t4, const T5& t5, const T6& t6, const T7& t7) {
645  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6, t7)));
646  }
647 
648  private:
649 
651 
656  Logger();
657 
659 
662  Logger(const Logger& unique);
663 
665 
668  void operator=(const Logger& unique);
669 
671 
675  std::string getDomain();
676 
678 
684  void log(const LogMessage& message);
685 
687  Logger *parent;
688 
690  std::string domain;
691 
693  std::string context_id;
694 
695  LoggerContext context;
696 
697  LoggerContext& getContext(void);
698 
699  Glib::Mutex mutex;
700 
701 #define rootLoggerMagic (0xF6569201)
702  static Logger *rootLogger;
703  static std::map<std::string,LogLevel>* defaultThresholds;
704  static unsigned int rootLoggerMark;
705  };
706 
709 } // namespace Arc
710 
711 #define rootLogger getRootLogger()
712 
713 #define LOG(LGR, THR, FSTR, ...) { if ((LGR).getThreshold() >= (THR)(LGR).msg((THR), (FSTR), ...); }
714 
715 #endif // __ARC_LOGGER__