ISIS Core Library 0.7.2 (api 3.0.0)

/scr/tee1/isis/lib/Core/CoreUtils/message.hpp

Go to the documentation of this file.
00001 //
00002 // C++ Interface: message
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: Enrico Reimer<reimer@cbs.mpg.de>, (C) 2008
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 
00013 #ifndef MESSAGE_H
00014 #define MESSAGE_H
00015 
00016 #include <sstream>
00017 #include <string>
00018 #include <ctime>
00019 #include <list>
00020 #include <iostream>
00021 #define BOOST_FILESYSTEM_VERSION 2 //@todo switch to 3 as soon as we drop support for boost < 1.44
00022 #include <boost/filesystem/path.hpp>
00023 #include <boost/weak_ptr.hpp>
00024 #include <boost/date_time/posix_time/posix_time_types.hpp>
00025 
00026 namespace isis
00027 {
00028 enum LogLevel {error = 1, warning, notice, info, verbose_info};
00029 namespace util
00030 {
00031 
00032 class MSubject : public std::string
00033 {
00034 public:
00035     template<typename T> MSubject( const T &cont ) {
00036         std::ostringstream text;
00037         text << cont;
00038         assign( text.str() );
00039     }
00040 };
00041 
00042 const char *logLevelName( LogLevel level );
00043 
00044 template<class MODULE> class Log;
00045 class Message;
00046 
00047 class MessageHandlerBase
00048 {
00049     static LogLevel m_stop_below;
00050 protected:
00051     MessageHandlerBase( LogLevel level ): m_level( level ) {}
00052     virtual ~MessageHandlerBase() {}
00053 public:
00054     LogLevel m_level;
00055     virtual void commit( const Message &msg ) = 0;
00056     static void stopBelow( LogLevel );
00057     bool requestStop( LogLevel _level );
00058 };
00059 
00060 class Message: public std::ostringstream
00061 {
00062     boost::weak_ptr<MessageHandlerBase> commitTo;
00063 public:
00064     std::string m_object, m_module;
00065     boost::filesystem::path m_file;
00066     std::list<std::string> m_subjects;
00067     boost::posix_time::ptime m_timeStamp;
00068     int m_line;
00069     LogLevel m_level;
00070     Message( std::string object, std::string module, std::string file, int line, LogLevel level, boost::weak_ptr<MessageHandlerBase> _commitTo );
00071     Message( const Message &src );
00072     ~Message();
00073     std::string merge()const;
00074     std::string strTime()const;
00075     template<typename T> Message &operator << ( T val ) {
00076         *( ( std::ostringstream * )this ) << val;
00077         return *this;
00078     }
00079     Message &operator << ( const MSubject &subj ) {
00080         m_subjects.push_back( subj );
00081         *( ( std::ostringstream * )this ) << "{s}";
00082         return *this;
00083     }
00084     bool shouldCommit()const;
00085 };
00086 
00093 class DefaultMsgPrint : public MessageHandlerBase
00094 {
00095 protected:
00096     static std::ostream *o;
00097     static const int max_age = 500;
00098     std::list<std::pair<boost::posix_time::ptime, std::string> > last;
00099 
00100 public:
00101     DefaultMsgPrint( LogLevel level ): MessageHandlerBase( level ) {}
00102     virtual ~DefaultMsgPrint() {}
00103     void commit( const Message &mesg );
00104     static void setStream( std::ostream &_o );
00105 };
00106 
00107 }
00108 }
00109 #endif //MESSAGE_H