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  enum LogLevel {
21  DEBUG = 1,
22  VERBOSE = 2,
24  INFO = 4,
26  WARNING = 8,
28  ERROR = 16,
29  FATAL = 32
31  };
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  LogFormat getFormat() const;
220 
222  static void setDefaultFormat(const LogFormat& newformat);
223 
225  static LogFormat getDefaultFormat();
226 
228 
231  void setPrefix(const std::string& prefix);
232 
234  std::string getPrefix() const;
235 
236  protected:
237 
239  LogDestination();
240 
241  private:
242 
244 
247  LogDestination(const LogDestination& unique);
248 
250 
253  void operator=(const LogDestination& unique);
254 
256  friend std::ostream& operator<<(std::ostream& os, const LogDestination& dest);
257 
258  protected:
260 
266  mutable Glib::Mutex mutex;
267 
270 
272 
275  std::string prefix;
276  };
277 
278 
279 
281 
290  class LogStream
291  : public LogDestination {
292  public:
293 
295 
300  LogStream(std::ostream& destination);
301 
303 
308  virtual void log(const LogMessage& message);
309 
310  private:
311 
313 
316  LogStream(const LogStream& unique);
317 
319 
322  void operator=(const LogStream& unique);
323 
325 
328  std::ostream& destination;
329 
330  };
331 
333 
344  class LogFile
345  : public LogDestination {
346  public:
347 
349 
355  LogFile(const std::string& path);
356 
358  ~LogFile();
359 
361 
366  void setMaxSize(int newsize);
367 
369 
375  void setBackups(int newbackup);
376 
378 
382  void setReopen(bool newreopen);
383 
385  void Reopen();
386 
388  static void ReopenAll();
389 
391  operator bool(void);
392 
394  bool operator!(void);
395 
397 
403  virtual void log(const LogMessage& message);
404  private:
405  LogFile(void);
406  LogFile(const LogFile& unique);
407  void operator=(const LogFile& unique);
408  void backup(void);
409  std::string path;
410  std::ofstream destination;
411  int maxsize;
412  int backups;
413  bool reopen;
414  std::list<std::string*> cache;
415  int maxcachesize;
416  Glib::Mutex file_mutex;
417  };
418 
419  class LoggerContextRef;
420 
423  class LoggerContext {
424  friend class Logger;
425  friend class LoggerContextRef;
426  private:
428  int usage_count;
429 
431  Glib::Mutex mutex;
432 
434  std::list<LogDestination*> destinations;
435 
437  LogLevel threshold;
438 
439  LoggerContext(LogLevel thr):usage_count(0),threshold(thr) { };
440 
441  LoggerContext(const LoggerContext& ctx):
442  usage_count(0),destinations(ctx.destinations),threshold(ctx.threshold) { };
443 
444  ~LoggerContext(void);
445 
446  void Acquire(void);
447 
448  void Release(void);
449  };
453 
493  class Logger {
494  public:
495 
497 
500  //static Logger rootLogger;
501  static Logger& getRootLogger();
502 
504 
508  Logger(Logger& parent,
509  const std::string& subdomain);
510 
512 
516  Logger(Logger& parent,
517  const std::string& subdomain,
518  LogLevel threshold);
519 
521  ~Logger();
522 
524 
531  void addDestination(LogDestination& destination);
532 
534 
536  void addDestinations(const std::list<LogDestination*>& destinations);
537 
539 
541  void setDestinations(const std::list<LogDestination*>& destinations);
542 
544 
548  const std::list<LogDestination*>& getDestinations(void) const;
549 
551  void removeDestinations(void);
552 
554 
557  void deleteDestinations(LogDestination* exclude=NULL);
558 
560 
565  void setThreshold(LogLevel threshold);
566 
568 
575  static void setThresholdForDomain(LogLevel threshold,
576  const std::list<std::string>& subdomains);
577 
579 
586  static void setThresholdForDomain(LogLevel threshold,
587  const std::string& domain);
588 
590  LogLevel getThreshold() const;
591 
593 
602  void setThreadContext(void);
603 
605 
607  void msg(LogMessage message);
608 
610 
621  void msg(LogLevel level, const std::string& str) {
622  msg(LogMessage(level, IString(str)));
623  }
624 
625  template<class T0>
626  void msg(LogLevel level, const std::string& str,
627  const T0& t0) {
628  msg(LogMessage(level, IString(str, t0)));
629  }
630 
631  template<class T0, class T1>
632  void msg(LogLevel level, const std::string& str,
633  const T0& t0, const T1& t1) {
634  msg(LogMessage(level, IString(str, t0, t1)));
635  }
636 
637  template<class T0, class T1, class T2>
638  void msg(LogLevel level, const std::string& str,
639  const T0& t0, const T1& t1, const T2& t2) {
640  msg(LogMessage(level, IString(str, t0, t1, t2)));
641  }
642 
643  template<class T0, class T1, class T2, class T3>
644  void msg(LogLevel level, const std::string& str,
645  const T0& t0, const T1& t1, const T2& t2, const T3& t3) {
646  msg(LogMessage(level, IString(str, t0, t1, t2, t3)));
647  }
648 
649  template<class T0, class T1, class T2, class T3, class T4>
650  void msg(LogLevel level, const std::string& str,
651  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
652  const T4& t4) {
653  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4)));
654  }
655 
656  template<class T0, class T1, class T2, class T3, class T4,
657  class T5>
658  void msg(LogLevel level, const std::string& str,
659  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
660  const T4& t4, const T5& t5) {
661  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5)));
662  }
663 
664  template<class T0, class T1, class T2, class T3, class T4,
665  class T5, class T6>
666  void msg(LogLevel level, const std::string& str,
667  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
668  const T4& t4, const T5& t5, const T6& t6) {
669  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6)));
670  }
671 
672  template<class T0, class T1, class T2, class T3, class T4,
673  class T5, class T6, class T7>
674  void msg(LogLevel level, const std::string& str,
675  const T0& t0, const T1& t1, const T2& t2, const T3& t3,
676  const T4& t4, const T5& t5, const T6& t6, const T7& t7) {
677  msg(LogMessage(level, IString(str, t0, t1, t2, t3, t4, t5, t6, t7)));
678  }
679 
680  private:
681 
683 
688  Logger();
689 
691 
694  Logger(const Logger& unique);
695 
697 
700  void operator=(const Logger& unique);
701 
703 
707  std::string getDomain();
708 
710 
716  void log(const LogMessage& message);
717 
719  Logger *parent;
720 
722  std::string domain;
723 
725  std::string context_id;
726 
727  LoggerContext context;
728 
729  LoggerContext& getContext(void);
730 
733  SharedMutex mutex;
734 
735 #define rootLoggerMagic (0xF6569201)
736  static Logger *rootLogger;
737  static std::map<std::string,LogLevel>* defaultThresholds;
738  static unsigned int rootLoggerMark;
739  };
740 
743 } // namespace Arc
744 
745 #define rootLogger getRootLogger()
746 
747 #define LOG(LGR, THR, FSTR, ...) { if ((LGR).getThreshold() >= (THR)(LGR).msg((THR), (FSTR), ...); }
748 
749 #endif // __ARC_LOGGER__
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
void setMaxSize(int newsize)
Set maximal allowed size of file.
Glib::Mutex mutex
A mutex for synchronization.
Definition: Logger.h:266
Definition: Logger.h:23
Logger(Logger &parent, const std::string &subdomain)
Creates a logger.
A base class for log destinations.
Definition: Logger.h:207
static void ReopenAll()
Reopen all LogFile objects.
bool operator!(void)
Returns true if this instance is invalid.
void setDestinations(const std::list< LogDestination *> &destinations)
Set LogDestinations.
static LogFormat getDefaultFormat()
Returns currently assigned default format.
LogMessage(LogLevel level, const IString &message)
Creates a LogMessage with the specified level and message text.
static void setDefaultFormat(const LogFormat &newformat)
Set format for any new log destination.
LogFormat getFormat() const
Returns currently assigned format.
~Logger()
Destroys a logger.
void addDestination(LogDestination &destination)
Adds a LogDestination.
A class for logging to files.
Definition: Logger.h:344
Only message level is printed.
Definition: Logger.h:44
void setPrefix(const std::string &prefix)
Set a prefix for this log destination to be logged before messages.
Only message is printed.
Definition: Logger.h:50
void setIdentifier(std::string identifier)
Sets the identifier of the LogMessage.
LogStream(std::ostream &destination)
Creates a LogStream connected to an ostream.
void msg(LogLevel level, const std::string &str)
Logs a message text.
Definition: Logger.h:621
void setReopen(bool newreopen)
Set file reopen on every write.
A class for logging to ostreams.
Definition: Logger.h:290
Definition: Logger.h:48
A logger class.
Definition: Logger.h:493
LogDestination()
Default constructor. Protected since subclasses should be used instead.
LoggerFormat(LogFormat format)
Make a new LoggerFormat with the given LogFormat.
Definition: Logger.h:56
LogFormat format
Format to use in this LogDestination.
Definition: Logger.h:269
Class used for localised output of log messages.
Definition: IString.h:180
void setThreadContext(void)
Creates per-thread context.
void setFormat(const LogFormat &newformat)
Set format for this log destination.
~LogFile()
Ordinary destructor.
void deleteDestinations(LogDestination *exclude=NULL)
Remove all LogDestinations and delete LogDestination objects.
LogLevel
Logging levels for tagging and filtering log messages.
Definition: Logger.h:20
void setBackups(int newbackup)
Set number of backups to store.
A class for log messages.
Definition: Logger.h:107
friend std::ostream & operator<<(std::ostream &os, const LogDestination &dest)
Sets iword and pword for format and prefix.
std::string prefix
Prefix to use in this log destination.
Definition: Logger.h:275
void addDestinations(const std::list< LogDestination *> &destinations)
Adds LogDestinations.
virtual void log(const LogMessage &message)
Writes a LogMessage to the stream.
LogLevel old_level_to_level(unsigned int old_level)
Convert an old-style log level (int from 0 to 5) to a LogLevel.
static Logger & getRootLogger()
The root Logger.
std::ostream & operator<<(std::ostream &, const Period &)
Prints a Period-object to the given ostream – typically cout.
Definition: Logger.h:28
LogLevel string_to_level(const std::string &str)
Convert string to a LogLevel.
void msg(LogMessage message)
Sends a LogMessage.
Struct to contain LogFormat, to use with operator<<(std::ostream&, const LoggerFormat&) ...
Definition: Logger.h:54
Definition: Logger.h:25
LogLevel istring_to_level(const std::string &llStr)
Convert string case-insensitively to a LogLevel.
LogFormat
Output formats. Defines prefix for each message.
Definition: Logger.h:35
std::string level_to_string(const LogLevel &level)
Convert LogLevel to a string.
LogLevel getLevel() const
Returns the level of the LogMessage.
virtual void log(const LogMessage &message)
Writes a LogMessage to the file.
All information except domain is printed.
Definition: Logger.h:42
Definition: Logger.h:30
const std::list< LogDestination * > & getDestinations(void) const
Obtains current LogDestinations.
Definition: Logger.h:21
virtual void log(const LogMessage &message)=0
Logs a LogMessage to this LogDestination.
WARNING level designates potentially harmful situations.
Definition: Logger.h:27
std::string getPrefix() const
Returns currently assignd prefix.
LogLevel getThreshold() const
Returns the threshold of this logger.
void removeDestinations(void)
Removes all LogDestinations.
friend std::ostream & operator<<(std::ostream &os, const LogMessage &message)
Printing of LogMessages to ostreams.
All information about message is printed.
Definition: Logger.h:37
void setThreshold(LogLevel threshold)
Sets the logging threshold.
static void setThresholdForDomain(LogLevel threshold, const std::list< std::string > &subdomains)
Sets the threshold for domain.
void Reopen()
Reopen file if currently open.