Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BOOSTER_LOGGER_H
00009 #define BOOSTER_LOGGER_H
00010
00011 #include <booster/config.h>
00012 #include <iosfwd>
00013 #include <memory>
00014 #include <string>
00015 #include <booster/copy_ptr.h>
00016 #include <booster/hold_ptr.h>
00017 #include <booster/noncopyable.h>
00018
00022 namespace booster {
00023
00024 template<typename T>
00025 class shared_ptr;
00026 template<typename T>
00027 class weak_ptr;
00028
00032
00033 namespace log {
00034
00038 typedef enum {
00039 emergency = 0,
00040 alert = 10,
00041 critical = 20,
00042 error = 30,
00043 warning = 40,
00044 notice = 50,
00045 info = 60,
00046 debug = 70,
00047 all = 100
00048 } level_type;
00049
00050
00057 class BOOSTER_API message {
00058 public:
00063 message(level_type l,char const *m,char const *name,int line);
00064
00068 message();
00069
00070 ~message();
00072 message(message &);
00074 message &operator=(message &);
00075
00079 level_type level() const;
00083 char const *module() const;
00087 char const *file_name() const;
00091 int file_line() const;
00095 std::string log_message() const;
00096
00100 std::ostream &out();
00101 private:
00102 level_type level_;
00103 char const *module_;
00104 char const *file_name_;
00105 int file_line_;
00106
00107 std::auto_ptr<std::ostringstream> message_;
00108
00109 struct data;
00110 copy_ptr<data> d;
00111 };
00112
00117 class BOOSTER_API sink : public noncopyable {
00118 public:
00126 virtual void log(message const &m) = 0;
00127 virtual ~sink() {}
00128 };
00129
00137 class BOOSTER_API logger : public noncopyable {
00138 public:
00142 static logger &instance();
00143
00149 bool should_be_logged(level_type level,char const *module);
00150
00158 void set_log_level(level_type level,char const *module);
00159
00163 void reset_log_level(char const *module);
00167 void set_default_level(level_type level);
00168
00175 void add_sink(shared_ptr<sink> const &s);
00176
00181 void remove_sink(weak_ptr<sink> const &s);
00182
00186 void remove_all_sinks();
00187
00192 void log(message const &);
00193
00197 static char const *level_to_string(level_type level);
00201 static level_type string_to_level(std::string const &);
00202
00203 private:
00204
00205 struct entry {
00206 char const *module;
00207 level_type level;
00208 };
00209
00210 static const int max_entries_size_ = 1024;
00211 level_type default_level_;
00212 entry entries_[max_entries_size_];
00213 int entries_size_;
00214
00215 struct data;
00216 hold_ptr<data> d;
00217
00218 logger();
00219 ~logger();
00220 };
00221
00227 namespace sinks {
00228
00233 BOOSTER_API std::string format_plain_text_message(message const &msg);
00234
00241 BOOSTER_API std::string format_plain_text_message_tz(message const &msg,int timezone_offset = 0);
00242
00246 class BOOSTER_API standard_error : public sink {
00247 public:
00248 standard_error();
00249 virtual void log(message const &);
00250 virtual ~standard_error();
00251 private:
00252 struct data;
00253 hold_ptr<data> d;
00254 };
00255
00259 class BOOSTER_API file : public sink {
00260 public:
00264 file();
00265 virtual ~file();
00266
00270 void open(std::string file_name);
00277 void max_files(unsigned limit);
00281 void append();
00282
00291 void set_timezone(std::string const &name);
00292
00294 virtual void log(message const &);
00295 private:
00296
00297 void shift(std::string const &base);
00298 std::string format_file(std::string const &,int);
00299
00300 unsigned max_files_;
00301 size_t max_size_;
00302 size_t current_size_;
00303 bool opened_;
00304 bool append_;
00305 bool use_local_time_;
00306 int tz_offset_;
00307
00308 struct data;
00309 hold_ptr<data> d;
00310 };
00311
00312 #ifdef BOOSTER_POSIX
00313
00314
00315
00316
00317
00318 class BOOSTER_API syslog : public sink {
00319 public:
00324 syslog();
00328 virtual void log(message const &);
00329 virtual ~syslog();
00330 private:
00331 struct data;
00332 hold_ptr<data> d;
00333 };
00334 #endif
00335
00336 }
00337
00338
00359 #define BOOSTER_LOG(level,module) \
00360 ::booster::log::logger::instance().should_be_logged(::booster::log::level,module) \
00361 && ::booster::log::message(::booster::log::level,module,__FILE__,__LINE__).out()
00362
00363
00365 #define BOOSTER_EMERG(m) BOOSTER_LOG(emergency,m)
00366
00367 #define BOOSTER_ALERT(m) BOOSTER_LOG(alert,m)
00368
00369 #define BOOSTER_CRITICAL(m) BOOSTER_LOG(critical,m)
00370
00371 #define BOOSTER_ERROR(m) BOOSTER_LOG(error,m)
00372
00373 #define BOOSTER_WARNING(m) BOOSTER_LOG(warning,m)
00374
00375 #define BOOSTER_NOTICE(m) BOOSTER_LOG(notice,m)
00376
00377 #define BOOSTER_INFO(m) BOOSTER_LOG(info,m)
00378
00379 #define BOOSTER_DEBUG(m) BOOSTER_LOG(debug,m)
00380
00381 }
00382
00383 }
00384
00385 #endif