#ifndef __JLOGGER__JMESSAGESTREAM__ #define __JLOGGER__JMESSAGESTREAM__ #include #include #include "JLogger/JMessageLogger.hh" /** * \file * Message reporting compatible with STL output stream operations. * \author mdejong */ namespace JLOGGER {} namespace JPP { using namespace JLOGGER; } namespace JLOGGER { /** * Message streamer. * * This class can be used in the same way as the STL ostream operator.\n * The actual message is reported on detruction of the object, e.g: * * JStreamLogger logger(std::cout); * * JDebugStream (logger) << "This is a debug message " << hex << 15; * * will produce on the terminal: * * "... This is a debug message f" * * where ... stands for the message prefix (see class JMessageLogger). * The list of available message streamer classes includes: * * - JDebugStream * - JWarningStream * - JErrorStream * - JNoticeStream * - JStatusStream */ class JMessageStream : public std::ostringstream { public: /** * Appends value to internal buffer. * * \param value value * \return this JMessageStream */ template JMessageStream& operator<<(const T& value) { static_cast(*this) << value; return *this; } /** * Type definition of I/O operator. */ typedef std::ostream& (*io_manip) (std::ostream&); /** * Append I/O manipulator. * * \param manip I/O manipulator * \return this JMessageStream */ JMessageStream& operator<<(io_manip manip) { static_cast(*this) << manip; return *this; } protected: /** * Constructor. * * \param __logger message logger * \param __typeout logger method */ JMessageStream(JMessageLogger& __logger, void (JMessageLogger::*__typeout)(const std::string&)) : std::ostringstream(), logger (__logger), typeout(__typeout) {} /** * Report message. */ ~JMessageStream() { (logger.*typeout)(this->str()); } JMessageLogger& logger; void (JMessageLogger::*typeout)(const std::string&); }; /** * Level specific message streamers. */ struct JDebugStream : JMessageStream { JDebugStream (JMessageLogger& logger) : JMessageStream(logger, &JMessageLogger::debug) {} }; struct JWarningStream : JMessageStream { JWarningStream(JMessageLogger& logger) : JMessageStream(logger, &JMessageLogger::warning) {} }; struct JErrorStream : JMessageStream { JErrorStream (JMessageLogger& logger) : JMessageStream(logger, &JMessageLogger::error) {} }; struct JNoticeStream : JMessageStream { JNoticeStream (JMessageLogger& logger) : JMessageStream(logger, &JMessageLogger::notice) {} }; struct JStatusStream : JMessageStream { JStatusStream (JMessageLogger& logger) : JMessageStream(logger, &JMessageLogger::status) {} }; } #endif