log.h
Go to the documentation of this file.
//
// Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOSTER_LOGGER_H
#define BOOSTER_LOGGER_H
#include <booster/config.h>
#include <iosfwd>
#include <memory>
#include <string>
#include <booster/copy_ptr.h>
#include <booster/hold_ptr.h>
#include <booster/noncopyable.h>
namespace booster {
template<typename T>
class shared_ptr;
template<typename T>
class weak_ptr;
namespace log {
typedef enum {
emergency = 0,
alert = 10,
critical = 20,
error = 30,
warning = 40,
notice = 50,
info = 60,
debug = 70,
all = 100
} level_type;
class BOOSTER_API message {
public:
message(level_type l,char const *m,char const *name,int line);
message();
~message();
message(message &);
message &operator=(message &);
level_type level() const;
char const *module() const;
char const *file_name() const;
int file_line() const;
std::string log_message() const;
std::ostream &out();
private:
level_type level_;
char const *module_;
char const *file_name_;
int file_line_;
std::auto_ptr<std::ostringstream> message_;
struct data;
copy_ptr<data> d;
};
class BOOSTER_API sink : public noncopyable {
public:
virtual void log(message const &m) = 0;
virtual ~sink() {}
};
class BOOSTER_API logger : public noncopyable {
public:
static logger &instance();
bool should_be_logged(level_type level,char const *module);
void set_log_level(level_type level,char const *module);
void reset_log_level(char const *module);
void set_default_level(level_type level);
void add_sink(shared_ptr<sink> const &s);
void remove_sink(weak_ptr<sink> const &s);
void remove_all_sinks();
void log(message const &);
static char const *level_to_string(level_type level);
static level_type string_to_level(std::string const &);
private:
struct entry {
char const *module;
level_type level;
};
static const int max_entries_size_ = 1024;
level_type default_level_;
entry entries_[max_entries_size_];
int entries_size_;
struct data;
hold_ptr<data> d;
logger();
~logger();
};
namespace sinks {
BOOSTER_API std::string format_plain_text_message(message const &msg);
BOOSTER_API std::string format_plain_text_message_tz(message const &msg,int timezone_offset = 0);
class BOOSTER_API standard_error : public sink {
public:
standard_error();
virtual void log(message const &);
virtual ~standard_error();
private:
struct data;
hold_ptr<data> d;
};
class BOOSTER_API file : public sink {
public:
file();
virtual ~file();
void open(std::string file_name);
void max_files(unsigned limit);
void append();
void set_timezone(std::string const &name);
virtual void log(message const &);
private:
void shift(std::string const &base);
std::string format_file(std::string const &,int);
unsigned max_files_;
size_t max_size_;
size_t current_size_;
bool opened_;
bool append_;
bool use_local_time_;
int tz_offset_;
struct data;
hold_ptr<data> d;
};
#ifdef BOOSTER_POSIX
class BOOSTER_API syslog : public sink {
public:
syslog();
virtual void log(message const &);
virtual ~syslog();
private:
struct data;
hold_ptr<data> d;
};
#endif
} // sinks
#define BOOSTER_LOG(level,module) \
::booster::log::logger::instance().should_be_logged(::booster::log::level,module) \
&& ::booster::log::message(::booster::log::level,module,__FILE__,__LINE__).out()
#define BOOSTER_EMERG(m) BOOSTER_LOG(emergency,m)
#define BOOSTER_ALERT(m) BOOSTER_LOG(alert,m)
#define BOOSTER_CRITICAL(m) BOOSTER_LOG(critical,m)
#define BOOSTER_ERROR(m) BOOSTER_LOG(error,m)
#define BOOSTER_WARNING(m) BOOSTER_LOG(warning,m)
#define BOOSTER_NOTICE(m) BOOSTER_LOG(notice,m)
#define BOOSTER_INFO(m) BOOSTER_LOG(info,m)
#define BOOSTER_DEBUG(m) BOOSTER_LOG(debug,m)
} // log
} // booster
#endif
booster::log::sink
This is the abstract interface to general sink - the consumer of the logged messages.
Definition: log.h:117
booster::log::sinks::file
log file based sink - sends messages to log file
Definition: log.h:259
booster::hold_ptr< data >
booster::copy_ptr< data >
booster::log::logger
This is the central class that manages all logging operations.
Definition: log.h:137
booster::log::sinks::format_plain_text_message
BOOSTER_API std::string format_plain_text_message(message const &msg)
booster::log::sinks::standard_error
stderr based sink - sends messages to standard error output
Definition: log.h:246
booster::log::sinks::format_plain_text_message_tz
BOOSTER_API std::string format_plain_text_message_tz(message const &msg, int timezone_offset=0)
booster::shared_ptr
Definition: log.h:25
booster::log::message
This class represents a single message that should be written to log.
Definition: log.h:57
booster::log::level_type
level_type
Definition: log.h:38
booster::weak_ptr
Definition: log.h:27
booster::locale::message
basic_message< char > message
Definition: message.h:494
booster::noncopyable
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15