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 #include <booster/memory_inc.h>
22 namespace booster {
23 
27 
28 namespace log {
29 
33  typedef enum {
34  emergency = 0,
35  alert = 10,
36  critical = 20,
37  error = 30,
38  warning = 40,
39  notice = 50,
40  info = 60,
41  debug = 70,
42  all = 100
43  } level_type;
44 
45 
52  class BOOSTER_API message {
53  public:
58  message(level_type l,char const *m,char const *name,int line);
59 
63  message();
64 
65  ~message();
67  message(message &&);
69  message &operator=(message &&);
70 
74  level_type level() const;
78  char const *module() const;
82  char const *file_name() const;
86  int file_line() const;
90  std::string log_message() const;
91 
95  std::ostream &out();
96  private:
97  level_type level_;
98  char const *module_;
99  char const *file_name_;
100  int file_line_;
101 
102  std::unique_ptr<std::ostringstream> message_;
103 
104  struct data;
105  copy_ptr<data> d;
106  };
107 
112  class BOOSTER_API sink : public noncopyable {
113  public:
121  virtual void log(message const &m) = 0;
122  virtual ~sink() {}
123  };
124 
132  class BOOSTER_API logger : public noncopyable {
133  public:
137  static logger &instance();
138 
144  bool should_be_logged(level_type level,char const *module);
145 
153  void set_log_level(level_type level,char const *module);
154 
158  void reset_log_level(char const *module);
162  void set_default_level(level_type level);
163 
170  void add_sink(shared_ptr<sink> const &s);
171 
176  void remove_sink(weak_ptr<sink> const &s);
177 
181  void remove_all_sinks();
182 
187  void log(message const &);
188 
192  static char const *level_to_string(level_type level);
196  static level_type string_to_level(std::string const &);
197 
198  private:
199 
200  struct entry {
201  char const *module;
202  level_type level;
203  };
204 
205  static const int max_entries_size_ = 1024;
206  level_type default_level_;
207  entry entries_[max_entries_size_];
208  int entries_size_;
209 
210  struct data;
211  hold_ptr<data> d;
212 
213  logger();
214  ~logger();
215  };
216 
222  namespace sinks {
223 
228  BOOSTER_API std::string format_plain_text_message(message const &msg);
229 
236  BOOSTER_API std::string format_plain_text_message_tz(message const &msg,int timezone_offset = 0);
237 
241  class BOOSTER_API standard_error : public sink {
242  public:
243  standard_error();
244  virtual void log(message const &);
245  virtual ~standard_error();
246  private:
247  struct data;
248  hold_ptr<data> d;
249  };
250 
257  class BOOSTER_API stream : public sink {
258  public:
262  stream(std::ostream &s);
266  virtual void log(message const &msg);
267  virtual ~stream();
268  private:
269  std::ostream *out_;
270  struct data;
271  hold_ptr<data> d;
272  };
273 
277  class BOOSTER_API file : public sink {
278  public:
279 
285  static const int app = -1;
286 
290  file();
291 
298  file(std::string const &file_name,int max_files = 0);
299 
300  virtual ~file();
301 
305  void open(std::string file_name);
312  void max_files(unsigned limit);
316  void append();
317 
326  void set_timezone(std::string const &name);
327 
329  virtual void log(message const &);
330  private:
331 
332  void shift(std::string const &base);
333  std::string format_file(std::string const &,int);
334 
335  unsigned max_files_;
336  BOOSTER_UNUSED_MEMBER size_t max_size_;
337  BOOSTER_UNUSED_MEMBER size_t current_size_;
338  bool opened_;
339  bool append_;
340  bool use_local_time_;
341  int tz_offset_;
342 
343  struct data;
344  hold_ptr<data> d;
345  };
346 
347  #ifdef BOOSTER_POSIX
348  class BOOSTER_API syslog : public sink {
354  public:
359  syslog();
360 
364  syslog(std::string const &id,int opts,int facility = 0);
368  syslog(int opts,int facility = 0);
372  virtual void log(message const &);
373  virtual ~syslog();
374  private:
375  struct data;
376  hold_ptr<data> d;
377  };
378  #endif
379 
380  } // sinks
381 
382 
403  #define BOOSTER_LOG(level,module) \
404  ::booster::log::logger::instance().should_be_logged(::booster::log::level,module) \
405  && ::booster::log::message(::booster::log::level,module,__FILE__,__LINE__).out()
406 
407 
409  #define BOOSTER_EMERG(m) BOOSTER_LOG(emergency,m)
410  #define BOOSTER_ALERT(m) BOOSTER_LOG(alert,m)
412  #define BOOSTER_CRITICAL(m) BOOSTER_LOG(critical,m)
414  #define BOOSTER_ERROR(m) BOOSTER_LOG(error,m)
416  #define BOOSTER_WARNING(m) BOOSTER_LOG(warning,m)
418  #define BOOSTER_NOTICE(m) BOOSTER_LOG(notice,m)
420  #define BOOSTER_INFO(m) BOOSTER_LOG(info,m)
422  #define BOOSTER_DEBUG(m) BOOSTER_LOG(debug,m)
424 
425 } // log
426 
427 } // booster
428 
429 #endif
This is the abstract interface to general sink - the consumer of the logged messages.
Definition: log.h:112
log file based sink - sends messages to log file
Definition: log.h:277
This is the central class that manages all logging operations.
Definition: log.h:132
BOOSTER_API std::string format_plain_text_message(message const &msg)
stderr based sink - sends messages to standard error output
Definition: log.h:241
BOOSTER_API std::string format_plain_text_message_tz(message const &msg, int timezone_offset=0)
log sink for a generic std::ostream
Definition: log.h:257
This class represents a single message that should be written to log.
Definition: log.h:52
level_type
Definition: log.h:33
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