libsdr  0.1.0
A simple SDR library
logger.hh
1 #ifndef __SDR_LOGGER_HH__
2 #define __SDR_LOGGER_HH__
3 
4 #include <string>
5 #include <sstream>
6 #include <list>
7 
8 
9 namespace sdr {
10 
12 typedef enum {
13  LOG_DEBUG,
14  LOG_INFO,
15  LOG_WARNING,
16  LOG_ERROR
17 } LogLevel;
18 
19 
22 class LogMessage: public std::stringstream
23 {
24 public:
28  LogMessage(LogLevel level, const std::string &msg="");
30  LogMessage(const LogMessage &other);
32  virtual ~LogMessage();
33 
35  LogLevel level() const;
37  inline std::string message() const { return this->str(); }
38 
39 protected:
41  LogLevel _level;
42 };
43 
44 
47 {
48 protected:
50  LogHandler();
51 
52 public:
54  virtual ~LogHandler();
56  virtual void handle(const LogMessage &msg) = 0;
57 };
58 
59 
60 
63 {
64 public:
69  StreamLogHandler(std::ostream &stream, LogLevel level);
71  virtual ~StreamLogHandler();
72 
74  virtual void handle(const LogMessage &msg);
75 
76 protected:
78  std::ostream &_stream;
80  LogLevel _level;
81 };
82 
83 
84 
86 class Logger
87 {
88 protected:
90  Logger();
91 
92 public:
94  virtual ~Logger();
95 
97  static Logger &get();
98 
100  void log(const LogMessage &message);
101 
104  void addHandler(LogHandler *handler);
105 
106 protected:
108  static Logger *_instance;
110  std::list<LogHandler *> _handler;
111 };
112 
113 }
114 
115 #endif // __SDR_LOGGER_HH__
Base class of all log-message handlers.
Definition: logger.hh:46
void addHandler(LogHandler *handler)
Adds a message handler.
Definition: logger.cc:95
Definition: autocast.hh:8
LogLevel level() const
Returns the level of the message.
Definition: logger.cc:26
virtual ~LogHandler()
Destructor.
Definition: logger.cc:38
virtual ~Logger()
Destructor.
Definition: logger.cc:80
std::list< LogHandler * > _handler
All registered handlers.
Definition: logger.hh:110
StreamLogHandler(std::ostream &stream, LogLevel level)
Constructor.
Definition: logger.cc:46
virtual void handle(const LogMessage &msg)
Handles the message.
Definition: logger.cc:57
LogMessage(LogLevel level, const std::string &msg="")
Constructor.
Definition: logger.cc:9
virtual void handle(const LogMessage &msg)=0
Needs to be implemented by sub-classes to handle log messages.
std::ostream & _stream
The output stream.
Definition: logger.hh:78
void log(const LogMessage &message)
Logs a message.
Definition: logger.cc:100
LogHandler()
Hidden constructor.
Definition: logger.cc:34
virtual ~StreamLogHandler()
Destructor.
Definition: logger.cc:52
virtual ~LogMessage()
Destructor.
Definition: logger.cc:21
A log message.
Definition: logger.hh:22
Serializes log message into the specified stream.
Definition: logger.hh:62
LogLevel _level
The minimum log-level.
Definition: logger.hh:80
Logger()
Hidden constructor.
Definition: logger.cc:74
std::string message() const
Returns the message.
Definition: logger.hh:37
The logger class (singleton).
Definition: logger.hh:86
LogLevel _level
The level of the message.
Definition: logger.hh:41
static Logger * _instance
The singleton instance.
Definition: logger.hh:108