OBTTrace.h

Go to the documentation of this file.
00001 #ifndef TRACE_H
00002 #define TRACE_H
00003 
00004 #include <set>
00005 #include <map>
00006 #include <string>
00007 #include <sstream>
00008 #include <fstream>
00009 
00010 #include "OBT.h"
00011 #include "OBTSingleton.h"
00012 #include "OBT_Exception.h"
00013 
00014 namespace OBT
00015 {
00020 class OBT_API TracerOutput 
00021 {
00022         friend class Tracer ;
00023 
00024 protected:
00025 
00027         virtual ~TracerOutput() {}
00028 
00036   virtual void trace( const char* id,
00037                       const char* fct, 
00038                       const char* file, 
00039                       const int line, 
00040                       const char* msg ) 
00041   {
00042     std::ostringstream stream ;
00043 #ifdef _MSC_VER
00044     // The debug message is set for the visual studio format
00045     stream << "[" << id << "] in \"" << fct << "\" of " <<  file << "@" << line << std::endl << msg << std::endl ;
00046 #else
00047     // The debug message is set for the vim format
00048     stream << "[" << id << "] in \"" << fct << "\" of " <<  file << " +" << line <<     " : " <<msg << std::endl ;
00049 #endif
00050     trace( stream.str() ) ; 
00051   }
00054   virtual void trace( const std::string& msg ) {} ;
00055 } ;
00056 
00138 class OBT_API Tracer 
00139 {
00141         friend class Singleton< Tracer > ;
00142 
00143 public:
00144 
00149         unsigned int registerId( const char* id ) ;
00150 
00153   void unregisterId( const char* id ) ;
00154 
00159         void traceAll( bool b = true ) ;
00160 
00163         void traceNone( bool b = true ) ;
00164 
00170         void setFile( const char* file = 0 ) ;
00171 
00180         void setOutput( TracerOutput* output, bool disable ) ;
00181 
00197         void trace(     const char* id, 
00198                                 const char* fct, 
00199                                 const char* file, 
00200                                 const int line, 
00201                                 const char* msg ) { if ( test( id ) )   { traceWithoutTest( id, fct, file, line, msg ) ; } }
00204         void trace(     unsigned int id, 
00205                                 const char* fct, 
00206                                 const char* file, 
00207                                 const int line, 
00208                                 const char* msg ) { if ( test( id ) )   { traceWithoutTest( _tracedIds[ id ].c_str(), fct, file, line, msg ) ; } }
00214   bool test( const char* id ) ;
00217         bool test( unsigned int id ) ;
00218 
00220         ~Tracer() ;
00221 
00222 protected:
00224         Tracer() ;
00225 
00228         std::ofstream _fileOut ;        
00229 
00232         bool _traceAll ;
00235         bool _traceNone ;
00236 
00239         bool _disable ;
00240 
00243   std::map< unsigned int, std::string > _tracedIds ;
00244 
00247         TracerOutput* _output ;
00248 
00251         void traceWithoutTest( const char* id,
00252               const char* fct, 
00253                                       const char* file, 
00254                                       const int line, 
00255                                       const char* msg ) ;
00259   unsigned int getHashId( const std::string& id ) ;
00260 } ; 
00263 template class OBT_API Singleton< Tracer > ;
00264 } // namespace OBT
00265 //-------------------------------------------------------------------------
00266 // UTILITY TRACE MESSAGE IDS
00267 //-------------------------------------------------------------------------
00270 extern OBT_API const unsigned int OBT_TRACE_CRITICAL ;
00273 extern OBT_API const unsigned int OBT_TRACE_ERROR ;
00276 extern OBT_API const unsigned int OBT_TRACE_WARNING ;
00279 extern OBT_API const unsigned int OBT_TRACE_NOTICE ;
00282 extern OBT_API const unsigned int OBT_TRACE_INFO ;
00285 extern OBT_API const unsigned int OBT_TRACE_DEBUG ;
00286 //-------------------------------------------------------------------------
00287 // UTILITY TRACE MACROS
00288 //-------------------------------------------------------------------------
00292 #define TRACE( ID, MSG ) \
00293 { \
00294   OBT::Tracer& tracer = OBT::Singleton< OBT::Tracer >::getInstance() ; \
00295         try \
00296         { \
00297     if( tracer.test( ID ) ) \
00298     { \
00299                   std::ostringstream msgForDebugTRACE ; \
00300                   msgForDebugTRACE << MSG ; \
00301                   tracer.trace( ID, __FUNCTION__, __FILE__, __LINE__, msgForDebugTRACE.str().c_str() ); \
00302     } \
00303         } \
00304         catch ( OBT::OBT_Exception& exception ) \
00305         { \
00306                 tracer.trace( ID, __FUNCTION__, __FILE__, __LINE__, exception.what() ); \
00307         } \
00308 }
00309 
00312 #define TRACE_CRITICAL( MSG ) TRACE( OBT_TRACE_CRITICAL, MSG )
00313 
00315 #define TRACE_ERROR( MSG ) TRACE( OBT_TRACE_ERROR, MSG )
00316 
00319 #define TRACE_WARNING( MSG ) TRACE( OBT_TRACE_WARNING, MSG )
00320 
00323 #define TRACE_NOTICE( MSG ) TRACE( OBT_TRACE_NOTICE, MSG )
00324 
00327 #define TRACE_INFO( MSG ) TRACE( OBT_TRACE_INFO, MSG )
00328 
00331 #define TRACE_DEBUG( MSG ) TRACE( OBT_TRACE_DEBUG, MSG )
00332 
00333 #endif // TRACE_H

Generated on Wed Oct 1 10:36:19 2008 for OBT by  doxygen 1.5.3