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 
75  LogLevel istring_to_level(const std::string& llStr);
77 
91  bool istring_to_level(const std::string& llStr, LogLevel& ll);
93  bool string_to_level(const std::string& str, LogLevel& ll);
95  std::string level_to_string(const LogLevel& level);
97  LogLevel old_level_to_level(unsigned int old_level);
98 
99 
100 
102 
107  class LogMessage {
108  public:
109 
111 
119  LogMessage(LogLevel level,
120  const IString& message);
121 
123 
131  LogMessage(LogLevel level,
132  const IString& message,
133  const std::string& identifier);
134 
136 
139  LogLevel getLevel() const;
140 
141  protected:
142 
144 
148  void setIdentifier(std::string identifier);
149 
150  private:
151 
153 
158  static std::string getDefaultIdentifier();
159 
161 
165  void setDomain(std::string domain);
166 
168  std::string time;
169 
171  LogLevel level;
172 
174  std::string domain;
175 
177  std::string identifier;
178 
180  IString message;
181 
183 
186  friend std::ostream& operator<<(std::ostream& os,
187  const LogMessage& message);
188 
190 
193  friend class Logger;
194 
195  };
196 
197 
198 
200 
208  public:
209 
211  virtual void log(const LogMessage& message) = 0;
212 
213  virtual ~LogDestination() {}
214 
216  void setFormat(const LogFormat& newformat);
217 
219 
222  void setPrefix(const std::string& prefix);
223 
224  protected:
225 
227  LogDestination();
228 
229  private:
230 
232 
235  LogDestination(const LogDestination& unique);
236 
238 
241  void operator=(const LogDestination& unique);
242 
244  friend std::ostream& operator<<(std::ostream& os, const LogDestination& dest);
245 
246  protected:
249 
251 
254  std::string prefix;
255  };
256 
257 
258 
260 
269  class LogStream
270  : public LogDestination {
271  public:
272 
274 
279  LogStream(std::ostream& destination);
280 
282 
287  virtual void log(const LogMessage& message);
288 
289  private:
290 
292 
295  LogStream(const LogStream& unique);
296 
298 
301  void operator=(const LogStream& unique);
302 
304 
307  std::ostream& destination;
308 
310 
315  Glib::Mutex mutex;
316 
317  };
318 
320 
331  class LogFile
332  : public LogDestination {
333  public:
334 
336 
342  LogFile(const std::string& path);
343 
345 
350  void setMaxSize(int newsize);
351 
353 
359  void setBackups(int newbackup);
360 
362 
366  void setReopen(bool newreopen);
367 
369  operator bool(void);
370 
372  bool operator!(void);
373 
375 
381  virtual void log(const LogMessage& message);
382  private:
383  LogFile(void);
384  LogFile(const LogFile& unique);
385  void operator=(const LogFile& unique);
386  void backup(void);
387  std::string path;
388  std::ofstream destination;
389  int maxsize;
390  int backups;
391  bool reopen;
392  Glib::Mutex mutex;
393  };
394 
395  class LoggerContextRef;
396 
399  class LoggerContext {
400  friend class Logger;
401  friend class LoggerContextRef;
402  private:
404  int usage_count;
405 
407  Glib::Mutex mutex;
408 
410  std::list<LogDestination*> destinations;
411 
413  LogLevel threshold;
414 
415  LoggerContext(LogLevel thr):usage_count(0),threshold(thr) { };
416 
417  LoggerContext(const LoggerContext& ctx):
418  usage_count(0),destinations(ctx.destinations),threshold(ctx.threshold) { };
419 
420  ~LoggerContext(void);
421 
422  void Acquire(void);
423 
424  void Release(void);
425  };
429 
430 
469  class Logger {
470  public:
471 
473 
476  //static Logger rootLogger;
477  static Logger& getRootLogger();
478 
480 
484  Logger(Logger& parent,
485  const std::string& subdomain);
486 
488 
492  Logger(Logger& parent,
493  const std::string& subdomain,
494  LogLevel threshold);
495 
497  ~Logger();
498 
500 
507  void addDestination(LogDestination& destination);
508 
510 
512  void addDestinations(const std::list<LogDestination*>& destinations);
513 
515 
517  void setDestinations(const std::list<LogDestination*>& destinations);
518 
520 
524  const std::list<LogDestination*>& getDestinations(void) const;
525 
527  void removeDestinations(void);
528 
530  void deleteDestinations(void);
531 
533 
538  void setThreshold(LogLevel threshold);
539 
541 
548  static void setThresholdForDomain(LogLevel threshold,
549  const std::list<std::string>& subdomains);
550 
552 
559  static void setThresholdForDomain(LogLevel threshold,
560  const std::string& domain);
561 
563  LogLevel getThreshold() const;
564 
566 
575  void setThreadContext(void);
576 
578 
580  void msg(LogMessage message);
581 
583 
594  void msg(LogLevel level, const std::string& str) {
595  msg(LogMessage(level, IString(str)));
596  }
597 
598  template<class T0>
599  void msg(LogLevel level, const std::string& str,
600  const T0& t0) {
601  msg(LogMessage(level, IString(str, t0)));
602  }
603 
604  template<class T0, class T1>
605  void msg(LogLevel level, const std::string& str,
606  const T0& t0, const T1& t1) {
607  msg(LogMessage(level, IString(str, t0, t1)));
608  }
609 
610  template<class T0, class T1, class T2>
611  void msg(LogLevel level, const std::string& str,
612  const T0& t0, const T1& t1, const T2& t2) {
613  msg(LogMessage(level, IString(str, t0, t1, t2)));
614  }
615 
616  template<class T0, class T1, class T2, class T3>
617  void msg(LogLevel level, const std::string& str,
618  const T0& t0, const T1& t1, const T2& t2, const T3& t3) {
619  msg(LogMessage(level, IString(str, t0, t1, t2, t3)));
620  }
621 
622  template<class T0, class T1, class T2, class T3, class T4>
623  void msg(LogLevel level, const std::string& str,
624  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
625  const T4& t4) {
626  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4)));
627  }
628 
629  template<class T0, class T1, class T2, class T3, class T4,
630  class T5>
631  void msg(LogLevel level, const std::string& str,
632  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
633  const T4& t4, const T5& t5) {
634  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5)));
635  }
636 
637  template<class T0, class T1, class T2, class T3, class T4,
638  class T5, class T6>
639  void msg(LogLevel level, const std::string& str,
640  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
641  const T4& t4, const T5& t5, const T6& t6) {
642  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6)));
643  }
644 
645  template<class T0, class T1, class T2, class T3, class T4,
646  class T5, class T6, class T7>
647  void msg(LogLevel level, const std::string& str,
648  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
649  const T4& t4, const T5& t5, const T6& t6, const T7& t7) {
650  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6, t7)));
651  }
652 
653  private:
654 
656 
661  Logger();
662 
664 
667  Logger(const Logger& unique);
668 
670 
673  void operator=(const Logger& unique);
674 
676 
680  std::string getDomain();
681 
683 
689  void log(const LogMessage& message);
690 
692  Logger *parent;
693 
695  std::string domain;
696 
698  std::string context_id;
699 
700  LoggerContext context;
701 
702  LoggerContext& getContext(void);
703 
704  Glib::Mutex mutex;
705 
706 #define rootLoggerMagic (0xF6569201)
707  static Logger *rootLogger;
708  static std::map<std::string,LogLevel>* defaultThresholds;
709  static unsigned int rootLoggerMark;
710  };
711 
714 } // namespace Arc
715 
716 #define rootLogger getRootLogger()
717 
718 #define LOG(LGR, THR, FSTR, ...) { if ((LGR).getThreshold() >= (THR)(LGR).msg((THR), (FSTR), ...); }
719 
720 #endif // __ARC_LOGGER__