stream.h
Go to the documentation of this file.
#ifndef WIBBLE_LOG_STREAM_H
#define WIBBLE_LOG_STREAM_H
#include <streambuf>
#include <string>
namespace wibble {
namespace log {
enum Level
{
DEBUG,
INFO,
UNUSUAL,
WARN,
ERR,
CRIT
};
struct Sender
{
virtual ~Sender() {}
virtual void send(Level level, const std::string& msg) = 0;
};
class Streambuf : public std::streambuf
{
protected:
static const Level defaultLevel = INFO;
std::string line;
Level level;
/* Note: we have to use composition instead of overloading because the
* sender needs to be called in the destructor, and destructors cannot call
* overridden methods */
Sender* sender;
void send();
public:
Streambuf();
Streambuf(Sender* s);
virtual ~Streambuf();
void send_partial_line();
void setSender(Sender* s);
void setLevel(const Level& level);
int overflow(int c);
};
std::ostream& operator<<(std::ostream& s, Level lev);
}
}
// vim:set ts=4 sw=4:
#endif
wibble::log::Streambuf
Streambuf class for logging.
Definition: stream.h:35
wibble::log::Streambuf::send
void send()
Send the message "line" with the level "level".
Definition: stream.cpp:24
wibble::log::Streambuf::level
Level level
Level of the next log message.
Definition: stream.h:42
wibble::log::Streambuf::setSender
void setSender(Sender *s)
Set/change the sender to use for this streambuf.
Definition: stream.cpp:22
wibble::log::Streambuf::sender
Sender * sender
Sender used to send log messages.
Definition: stream.h:48
wibble::log::Streambuf::~Streambuf
virtual ~Streambuf()
Definition: stream.cpp:11
wibble::log::Streambuf::send_partial_line
void send_partial_line()
If there is a partial line, send it out.
Definition: stream.cpp:16
wibble::log::Streambuf::line
std::string line
Line buffer with the log message we are building.
Definition: stream.h:40
wibble::log::Streambuf::overflow
int overflow(int c)
override to get data as a std::streambuf
Definition: stream.cpp:38
wibble::log::Streambuf::defaultLevel
static const Level defaultLevel
Level to use for messages whose level has not been specified.
Definition: stream.h:38
wibble::log::Streambuf::setLevel
void setLevel(const Level &level)
Set the level for the next message, and the next message only.
Definition: stream.cpp:33
wibble::log::Streambuf::Streambuf
Streambuf()
Construct a nonworking Streambuf to be initialised later.
Definition: stream.cpp:7
wibble::log::operator<<
std::ostream & operator<<(std::ostream &s, Level lev)
Definition: stream.cpp:47
wibble::log::Level
Level
Urgency of a log message.
Definition: stream.h:12
wibble::log::WARN
@ WARN
Definition: stream.h:16
wibble::log::INFO
@ INFO
Definition: stream.h:14
wibble::log::CRIT
@ CRIT
Definition: stream.h:18
wibble::log::DEBUG
@ DEBUG
Definition: stream.h:13
wibble::log::ERR
@ ERR
Definition: stream.h:17
wibble::log::UNUSUAL
@ UNUSUAL
Definition: stream.h:15
wibble
Definition: amorph.h:17
wibble::log::Sender
Handle sending a log message.
Definition: stream.h:23
wibble::log::Sender::send
virtual void send(Level level, const std::string &msg)=0
Log one line of text with the given level.
wibble::log::Sender::~Sender
virtual ~Sender()
Definition: stream.h:24