CppCMS
log.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOSTER_LOGGER_H
9 #define BOOSTER_LOGGER_H
10 
11 #include <booster/config.h>
12 #include <iosfwd>
13 #include <booster/auto_ptr_inc.h>
14 #include <string>
15 #include <booster/copy_ptr.h>
16 #include <booster/hold_ptr.h>
17 #include <booster/noncopyable.h>
18 
22 namespace booster {
23 
24 template<typename T>
25 class shared_ptr;
26 template<typename T>
27 class weak_ptr;
28 
32 
33 namespace log {
34 
38  typedef enum {
39  emergency = 0,
40  alert = 10,
41  critical = 20,
42  error = 30,
43  warning = 40,
44  notice = 50,
45  info = 60,
46  debug = 70,
47  all = 100
48  } level_type;
49 
50 
57  class BOOSTER_API message {
58  public:
63  message(level_type l,char const *m,char const *name,int line);
64 
68  message();
69 
70  ~message();
72  message(message &);
74  message &operator=(message &);
75 
79  level_type level() const;
83  char const *module() const;
87  char const *file_name() const;
91  int file_line() const;
95  std::string log_message() const;
96 
100  std::ostream &out();
101  private:
102  level_type level_;
103  char const *module_;
104  char const *file_name_;
105  int file_line_;
106 
107  std::auto_ptr<std::ostringstream> message_;
108 
109  struct data;
110  copy_ptr<data> d;
111  };
112 
117  class BOOSTER_API sink : public noncopyable {
118  public:
126  virtual void log(message const &m) = 0;
127  virtual ~sink() {}
128  };
129 
137  class BOOSTER_API logger : public noncopyable {
138  public:
142  static logger &instance();
143 
149  bool should_be_logged(level_type level,char const *module);
150 
158  void set_log_level(level_type level,char const *module);
159 
163  void reset_log_level(char const *module);
167  void set_default_level(level_type level);
168 
175  void add_sink(shared_ptr<sink> const &s);
176 
181  void remove_sink(weak_ptr<sink> const &s);
182 
186  void remove_all_sinks();
187 
192  void log(message const &);
193 
197  static char const *level_to_string(level_type level);
201  static level_type string_to_level(std::string const &);
202 
203  private:
204 
205  struct entry {
206  char const *module;
207  level_type level;
208  };
209 
210  static const int max_entries_size_ = 1024;
211  level_type default_level_;
212  entry entries_[max_entries_size_];
213  int entries_size_;
214 
215  struct data;
216  hold_ptr<data> d;
217 
218  logger();
219  ~logger();
220  };
221 
227  namespace sinks {
228 
233  BOOSTER_API std::string format_plain_text_message(message const &msg);
234 
241  BOOSTER_API std::string format_plain_text_message_tz(message const &msg,int timezone_offset = 0);
242 
246  class BOOSTER_API standard_error : public sink {
247  public:
248  standard_error();
249  virtual void log(message const &);
250  virtual ~standard_error();
251  private:
252  struct data;
253  hold_ptr<data> d;
254  };
255 
262  class BOOSTER_API stream : public sink {
263  public:
267  stream(std::ostream &s);
271  virtual void log(message const &msg);
272  virtual ~stream();
273  private:
274  std::ostream *out_;
275  struct data;
276  hold_ptr<data> d;
277  };
278 
282  class BOOSTER_API file : public sink {
283  public:
284 
290  static const int app = -1;
291 
295  file();
296 
303  file(std::string const &file_name,int max_files = 0);
304 
305  virtual ~file();
306 
310  void open(std::string file_name);
317  void max_files(unsigned limit);
321  void append();
322 
331  void set_timezone(std::string const &name);
332 
334  virtual void log(message const &);
335  private:
336 
337  void shift(std::string const &base);
338  std::string format_file(std::string const &,int);
339 
340  unsigned max_files_;
341  BOOSTER_UNUSED_MEMBER size_t max_size_;
342  BOOSTER_UNUSED_MEMBER size_t current_size_;
343  bool opened_;
344  bool append_;
345  bool use_local_time_;
346  int tz_offset_;
347 
348  struct data;
349  hold_ptr<data> d;
350  };
351 
352  #ifdef BOOSTER_POSIX
353  class BOOSTER_API syslog : public sink {
359  public:
364  syslog();
365 
369  syslog(std::string const &id,int opts,int facility = 0);
373  syslog(int opts,int facility = 0);
377  virtual void log(message const &);
378  virtual ~syslog();
379  private:
380  struct data;
381  hold_ptr<data> d;
382  };
383  #endif
384 
385  } // sinks
386 
387 
408  #define BOOSTER_LOG(level,module) \
409  ::booster::log::logger::instance().should_be_logged(::booster::log::level,module) \
410  && ::booster::log::message(::booster::log::level,module,__FILE__,__LINE__).out()
411 
412 
414  #define BOOSTER_EMERG(m) BOOSTER_LOG(emergency,m)
415  #define BOOSTER_ALERT(m) BOOSTER_LOG(alert,m)
417  #define BOOSTER_CRITICAL(m) BOOSTER_LOG(critical,m)
419  #define BOOSTER_ERROR(m) BOOSTER_LOG(error,m)
421  #define BOOSTER_WARNING(m) BOOSTER_LOG(warning,m)
423  #define BOOSTER_NOTICE(m) BOOSTER_LOG(notice,m)
425  #define BOOSTER_INFO(m) BOOSTER_LOG(info,m)
427  #define BOOSTER_DEBUG(m) BOOSTER_LOG(debug,m)
429 
430 } // log
431 
432 } // booster
433 
434 #endif
This is the abstract interface to general sink - the consumer of the logged messages.
Definition: log.h:117
log file based sink - sends messages to log file
Definition: log.h:282
This is the central class that manages all logging operations.
Definition: log.h:137
BOOSTER_API std::string format_plain_text_message(message const &msg)
stderr based sink - sends messages to standard error output
Definition: log.h:246
BOOSTER_API std::string format_plain_text_message_tz(message const &msg, int timezone_offset=0)
Definition: log.h:25
log sink for a generic std::ostream
Definition: log.h:262
This class represents a single message that should be written to log.
Definition: log.h:57
level_type
Definition: log.h:38
Definition: log.h:27
basic_message< char > message
Definition: message.h:494
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15