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 <memory>
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 
259  class BOOSTER_API file : public sink {
260  public:
264  file();
265  virtual ~file();
266 
270  void open(std::string file_name);
277  void max_files(unsigned limit);
281  void append();
282 
291  void set_timezone(std::string const &name);
292 
294  virtual void log(message const &);
295  private:
296 
297  void shift(std::string const &base);
298  std::string format_file(std::string const &,int);
299 
300  unsigned max_files_;
301  size_t max_size_;
302  size_t current_size_;
303  bool opened_;
304  bool append_;
305  bool use_local_time_;
306  int tz_offset_;
307 
308  struct data;
309  hold_ptr<data> d;
310  };
311 
312  #ifdef BOOSTER_POSIX
313  class BOOSTER_API syslog : public sink {
319  public:
324  syslog();
328  virtual void log(message const &);
329  virtual ~syslog();
330  private:
331  struct data;
332  hold_ptr<data> d;
333  };
334  #endif
335 
336  } // sinks
337 
338 
359  #define BOOSTER_LOG(level,module) \
360  ::booster::log::logger::instance().should_be_logged(::booster::log::level,module) \
361  && ::booster::log::message(::booster::log::level,module,__FILE__,__LINE__).out()
362 
363 
365  #define BOOSTER_EMERG(m) BOOSTER_LOG(emergency,m)
366  #define BOOSTER_ALERT(m) BOOSTER_LOG(alert,m)
368  #define BOOSTER_CRITICAL(m) BOOSTER_LOG(critical,m)
370  #define BOOSTER_ERROR(m) BOOSTER_LOG(error,m)
372  #define BOOSTER_WARNING(m) BOOSTER_LOG(warning,m)
374  #define BOOSTER_NOTICE(m) BOOSTER_LOG(notice,m)
376  #define BOOSTER_INFO(m) BOOSTER_LOG(info,m)
378  #define BOOSTER_DEBUG(m) BOOSTER_LOG(debug,m)
380 
381 } // log
382 
383 } // booster
384 
385 #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:259
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
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
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15