00001
00002
00003 #ifndef KERNEL_JAFAR_DEBUG_HPP
00004 #define KERNEL_JAFAR_DEBUG_HPP
00005
00006 #include <iostream>
00007 #include <fstream>
00008 #include <string>
00009 #include <map>
00010 #include <list>
00011 #include <vector>
00012
00013
00014 #include <boost/serialization/singleton.hpp>
00015
00016 namespace jafar {
00017 namespace debug {
00018
00019 #ifndef SWIG
00020
00021 class DebugStream;
00022
00023 namespace details
00024 {
00025 typedef DebugStream& (*stream_function)(DebugStream& stream);
00026 typedef std::ios_base&(*iosbase_function)(std::ios_base&);
00027 }
00028
00029 #endif // SWIG
00030
00031
00039 class DebugStream {
00040
00041 public:
00042
00044 enum Level {Off = 0,
00045 Trace = 1,
00046 Warning = 2,
00047 Debug = 3,
00048 VerboseDebug = 4,
00049 VeryVerboseDebug = 5
00050 };
00051
00055 static void setDefaultLevel(Level level_) {
00056 instance().defaultLevel = level_;
00057 }
00058
00060 static void setLevel(std::string const& module_, Level level_) {
00061 instance().modulesLevel[module_]=level_;
00062 }
00063
00065 static void moduleOff(std::string const& module_) {
00066 setLevel(module_, DebugStream::Off);
00067 }
00068
00070 static void moduleWarning(std::string const& module_) {
00071 setLevel(module_, DebugStream::Warning);
00072 }
00073
00075 static void moduleDebug(std::string const& module_) {
00076 setLevel(module_, DebugStream::Debug);
00077 }
00078
00080 static void moduleVerboseDebug(std::string const& module_) {
00081 setLevel(module_, DebugStream::VerboseDebug);
00082 }
00083
00085 static void moduleVeryVerboseDebug(std::string const& module_) {
00086 setLevel(module_, DebugStream::VeryVerboseDebug);
00087 }
00088
00090 static void unsetModule(std::string const& module_) {
00091 instance().modulesLevel.erase(module_);
00092 }
00093
00095 static void setOutputFile(std::string const& filename_);
00096
00098 static void setDefaultStream();
00099
00100 #ifndef SWIG // hide these methods in the interactive interface
00101
00103 static void setStream(std::ostream& os_) {
00104 instance().debugStream = &os_;
00105 }
00106
00110 static void setup(std::string const& module_, Level level_, bool display_ = true);
00111
00116 static void sendLocation(std::string const& module_, char const* file_, int line_);
00117
00121 static DebugStream& instance() {
00122
00123 return boost::serialization::singleton<DebugStream>::get_mutable_instance();
00124
00125 }
00126
00127 #endif // SWIG
00128
00129 private:
00130
00132 std::ostream* debugStream;
00133
00135 std::ofstream fileStream;
00136 bool fsOpen;
00137
00138 std::map<std::string, Level> modulesLevel;
00139
00141 Level defaultLevel;
00142
00143 bool debugging;
00144
00145 DebugStream();
00146 ~DebugStream();
00147
00149 bool isDebugging() const { return debugging; }
00150
00152 static std::ostream& stream() { return *(instance().debugStream); }
00153
00154
00155
00156
00157
00158
00159
00160
00161 friend class boost::serialization::detail::singleton_wrapper<DebugStream>;
00162
00163 friend DebugStream& operator << (DebugStream& debugStream,
00164 details::stream_function function);
00165 friend DebugStream& operator << (DebugStream& debugStream,
00166 details::iosbase_function function);
00167 friend DebugStream& endl(DebugStream& debugStream);
00168
00169 template<typename T>
00170 friend DebugStream& operator << (DebugStream& debugStream,
00171 T const& value);
00172 };
00173
00174
00175 #ifndef SWIG
00176
00177
00178
00179 inline DebugStream& operator << (DebugStream& debugStream, details::stream_function function)
00180 {
00181 if (debugStream.isDebugging())
00182 return function(debugStream);
00183
00184 return debugStream;
00185 }
00186
00187 inline DebugStream& operator << (DebugStream& debugStream, details::iosbase_function function)
00188 {
00189 if (debugStream.isDebugging())
00190 function(debugStream.stream());
00191
00192 return debugStream;
00193 }
00194
00195 template<typename T>
00196 inline DebugStream& operator << (DebugStream& debugStream, T const& value)
00197 {
00198 if (debugStream.isDebugging())
00199 debugStream.stream() << value;
00200
00201 return debugStream;
00202 }
00203
00204 inline DebugStream& endl(DebugStream& debugStream)
00205 {
00206 if (debugStream.isDebugging())
00207 debugStream.stream() << std::endl;
00208
00209 return debugStream;
00210 }
00211
00212 template< typename _Type_ >
00213 std::ostream& operator<<(std::ostream& s, const std::list<_Type_*>& gr)
00214 {
00215 s << " ( ";
00216 for( typename std::list<_Type_*>::const_iterator it = gr.begin(); it != gr.end(); ++it )
00217 {
00218 if( it != gr.begin() ) s << " , ";
00219 s << **it;
00220 }
00221 s << " ) ";
00222 return s;
00223 }
00224
00225 template< typename _Type_ >
00226 std::ostream& operator<<(std::ostream& s, const std::list<_Type_>& gr)
00227 {
00228 s << " ( ";
00229 for( typename std::list<_Type_>::const_iterator it = gr.begin(); it != gr.end(); ++it )
00230 {
00231 if( it != gr.begin() ) s << " , ";
00232 s << *it;
00233 }
00234 s << " ) ";
00235 return s;
00236 }
00237
00238 template< typename _Type_ >
00239 std::ostream& operator<<(std::ostream& s, const std::vector<_Type_*>& gr)
00240 {
00241 s << " ( ";
00242 for( typename std::vector<_Type_*>::const_iterator it = gr.begin(); it != gr.end(); ++it )
00243 {
00244 if( it != gr.begin() ) s << " , ";
00245 s << **it;
00246 }
00247 s << " ) ";
00248 return s;
00249 }
00250
00251 template< typename _Type_ >
00252 std::ostream& operator<<(std::ostream& s, const std::vector<_Type_>& gr)
00253 {
00254 s << " ( ";
00255 for( typename std::vector<_Type_>::const_iterator it = gr.begin(); it != gr.end(); ++it )
00256 {
00257 if( it != gr.begin() ) s << " , ";
00258 s << *it;
00259 }
00260 s << " ) ";
00261 return s;
00262 }
00263
00264 #endif // SWIG
00265
00266 }
00267 }
00268
00269
00270 #include "kernel/jafarMacro.hpp"
00271
00272 #endif // KERNEL_JAFAR_DEBUG_HPP