00001
00007 #ifndef __MSG_H__
00008 #define __MSG_H__
00009
00010 #include <iostream>
00011 #include <string>
00012 #include <cassert>
00013 using std::string;
00014
00018 namespace Msg {
00019
00020
00021
00034 typedef void (*fnPtr) (const string& s, const bool terminate);
00035 namespace Handler {
00036 inline void ignore (const string& , const bool =true) { }
00037 inline void cout_notify (const string& s, const bool terminate=true)
00038 { std::cout << s << (terminate? ".\n" : ""); }
00039 inline void cerr_error (const string& s, const bool terminate=true)
00040 { std::cerr << "Error: " << s << (terminate? ".\n" : ""); }
00041 inline void cerr_warning (const string& s, const bool terminate=true)
00042 { std::cerr << "Warning: " << s << (terminate? ".\n" : ""); }
00043 }
00045
00046
00047
00050 typedef enum { Verbose=0, Notify=1, Requested=2, Warning=3, Error=4 } MessageLevel;
00051 const int NumMessageLevels=5;
00052
00053
00054
00077 template <class Level=MessageLevel, int size=NumMessageLevels>
00078 class LevelHandler;
00079 template <class Level, int size>
00080 class LevelHandler {
00081 typedef LevelHandler<Level,size> self;
00082
00083 public:
00084 LevelHandler& register_handler( Level m, fnPtr h)
00085 { assert(m<size); handler[m]=h; return *this; }
00086
00087 void operator() (Level m, const string& s, const bool terminate=true) const
00088 { handler[m](s,terminate); }
00089
00090 LevelHandler()
00091 { for (int i=0; i<size; i++) handler[i] = Default; }
00092
00093 static self*& default_instance()
00094 { return default_instance_; }
00095
00096 static void set_default_instance(self* h)
00097 { default_instance_ = h; }
00098
00099 private:
00100 fnPtr handler[size];
00101 static self* default_instance_;
00102 static fnPtr Default;
00103 };
00104
00105
00106
00107 template <class Level, int size>
00108 fnPtr LevelHandler<Level,size>::Default=Handler::ignore;
00109
00110
00113 template <class Level, int size>
00114 LevelHandler<Level,size>* LevelHandler<Level,size>::default_instance_=0;
00115
00116
00117 }
00118
00119 #endif
00120