logger.hh
#ifndef __SDR_LOGGER_HH__
#define __SDR_LOGGER_HH__
#include <string>
#include <sstream>
#include <list>
namespace sdr {
typedef enum {
LOG_DEBUG,
LOG_INFO,
LOG_WARNING,
LOG_ERROR
} LogLevel;
class LogMessage: public std::stringstream
{
public:
LogMessage(LogLevel level, const std::string &msg="");
LogMessage(const LogMessage &other);
virtual ~LogMessage();
LogLevel level() const;
inline std::string message() const { return this->str(); }
protected:
LogLevel _level;
};
class LogHandler
{
protected:
LogHandler();
public:
virtual ~LogHandler();
virtual void handle(const LogMessage &msg) = 0;
};
class StreamLogHandler: public LogHandler
{
public:
StreamLogHandler(std::ostream &stream, LogLevel level);
virtual ~StreamLogHandler();
virtual void handle(const LogMessage &msg);
protected:
std::ostream &_stream;
LogLevel _level;
};
class Logger
{
protected:
Logger();
public:
virtual ~Logger();
static Logger &get();
void log(const LogMessage &message);
void addHandler(LogHandler *handler);
protected:
static Logger *_instance;
std::list<LogHandler *> _handler;
};
}
#endif // __SDR_LOGGER_HH__
sdr::LogHandler
Base class of all log-message handlers.
Definition: logger.hh:46
sdr::Logger::addHandler
void addHandler(LogHandler *handler)
Adds a message handler.
Definition: logger.cc:95
sdr
Definition: autocast.hh:8
sdr::LogMessage::level
LogLevel level() const
Returns the level of the message.
Definition: logger.cc:26
sdr::LogHandler::~LogHandler
virtual ~LogHandler()
Destructor.
Definition: logger.cc:38
sdr::Logger::~Logger
virtual ~Logger()
Destructor.
Definition: logger.cc:80
sdr::Logger::_handler
std::list< LogHandler * > _handler
All registered handlers.
Definition: logger.hh:110
sdr::StreamLogHandler::StreamLogHandler
StreamLogHandler(std::ostream &stream, LogLevel level)
Constructor.
Definition: logger.cc:46
sdr::StreamLogHandler::handle
virtual void handle(const LogMessage &msg)
Handles the message.
Definition: logger.cc:57
sdr::LogMessage::LogMessage
LogMessage(LogLevel level, const std::string &msg="")
Constructor.
Definition: logger.cc:9
sdr::LogHandler::handle
virtual void handle(const LogMessage &msg)=0
Needs to be implemented by sub-classes to handle log messages.
sdr::StreamLogHandler::_stream
std::ostream & _stream
The output stream.
Definition: logger.hh:78
sdr::Logger::log
void log(const LogMessage &message)
Logs a message.
Definition: logger.cc:100
sdr::LogHandler::LogHandler
LogHandler()
Hidden constructor.
Definition: logger.cc:34
sdr::StreamLogHandler::~StreamLogHandler
virtual ~StreamLogHandler()
Destructor.
Definition: logger.cc:52
sdr::LogMessage::~LogMessage
virtual ~LogMessage()
Destructor.
Definition: logger.cc:21
sdr::LogMessage
A log message.
Definition: logger.hh:22
sdr::StreamLogHandler
Serializes log message into the specified stream.
Definition: logger.hh:62
sdr::StreamLogHandler::_level
LogLevel _level
The minimum log-level.
Definition: logger.hh:80
sdr::Logger::Logger
Logger()
Hidden constructor.
Definition: logger.cc:74
sdr::LogMessage::message
std::string message() const
Returns the message.
Definition: logger.hh:37
sdr::Logger
The logger class (singleton).
Definition: logger.hh:86
sdr::LogMessage::_level
LogLevel _level
The level of the message.
Definition: logger.hh:41
sdr::Logger::_instance
static Logger * _instance
The singleton instance.
Definition: logger.hh:108