00001 //Copyright 2013 Ben Loer 00002 //This file is part of the ConfigHandler library 00003 //It is released under the terms of the GNU General Public License v3 00004 00011 #ifndef MESSAGE_h 00012 #define MESSAGE_h 1 00013 00014 #include <iostream> 00015 #include <sstream> 00016 #include "MessageHandler.hh" 00017 00022 class Message { 00023 public: 00026 Message(MESSAGE_LEVEL level=INFO) : _level(level), 00027 _stream(new std::ostringstream) {} 00029 ~Message() throw(){ MessageHandler::GetInstance()->Post(_stream,_level); } 00030 //Note it's up to the MessageHandler to delete the stream! 00031 00032 00033 //Streaming operators: 00034 //The last three are necessary to catch things like std::endl 00035 00037 template<class T> std::ostream& operator<< (const T& t){ return *_stream<<t;} 00038 std::ostream& operator<< (std::ostream& ( *pf )(std::ostream&)) 00039 { return *_stream<<pf; } 00040 std::ostream& operator<< (std::ios& ( *pf )(std::ios&)){return *_stream<<pf; } 00041 std::ostream& operator<< (std::ios_base& ( *pf )(std::ios_base&)) 00042 { return *_stream<<pf; } 00043 00045 std::string str(){ return _stream->str(); } 00046 private: 00047 MESSAGE_LEVEL _level; 00048 std::ostringstream* _stream; 00049 00051 Message(const Message& right) : _level(right._level), 00052 _stream(right._stream) {} 00054 Message& operator=(const Message& right){ 00055 if (this != &right){ 00056 _level=right._level; 00057 _stream=right._stream; 00058 } 00059 return *this; 00060 } 00061 }; 00062 00063 #endif