00001 #include <iostream>
00002 #include <fstream>
00003
00004 #include "OBTTrace.h"
00005 #include "OBTSingleton.h"
00006
00007 using namespace std ;
00008 using namespace OBT ;
00009
00010 const unsigned int OBT_TRACE_CRITICAL =
00011 OBT::Singleton< OBT::Tracer >::getInstance().registerId( "TRACE CRITICAL" ) ;
00012 const unsigned int OBT_TRACE_ERROR =
00013 OBT::Singleton< OBT::Tracer >::getInstance().registerId( "TRACE ERROR" ) ;
00014 const unsigned int OBT_TRACE_WARNING =
00015 OBT::Singleton< OBT::Tracer >::getInstance().registerId( "TRACE WARNING" ) ;
00016 const unsigned int OBT_TRACE_NOTICE =
00017 OBT::Singleton< OBT::Tracer >::getInstance().registerId( "TRACE NOTICE" ) ;
00018 const unsigned int OBT_TRACE_INFO =
00019 OBT::Singleton< OBT::Tracer >::getInstance().registerId( "TRACE INFO" ) ;
00020 const unsigned int OBT_TRACE_DEBUG =
00021 OBT::Singleton< OBT::Tracer >::getInstance().registerId( "TRACE DEBUG" ) ;
00022
00023
00024
00025
00026 Tracer::Tracer()
00027 :
00028 _traceAll( false ),
00029 _traceNone( false ),
00030 _disable( false ),
00031 _tracedIds(),
00032 _output( 0 )
00033 {
00034 }
00035
00036
00037
00038
00039 Tracer::~Tracer()
00040 {
00041 delete _output ;
00042 if ( _fileOut.is_open() )
00043 {
00044 _fileOut.close() ;
00045 }
00046
00047 }
00048
00049
00050
00051
00052 bool Tracer::test( const char* id)
00053 {
00054 return !_traceNone && ( _traceAll || _tracedIds.find( getHashId( id ) ) != _tracedIds.end() ) ;
00055 }
00056
00057
00058
00059
00060 bool Tracer::test( unsigned int id )
00061 {
00062 return !_traceNone && ( _traceAll || _tracedIds.find( id ) != _tracedIds.end() ) ;
00063 }
00064
00065
00066
00067
00068 void Tracer::traceWithoutTest( const char* id,
00069 const char* fct,
00070 const char* file,
00071 const int line,
00072 const char* msg )
00073 {
00074
00075 ostringstream stream ;
00076 #ifdef _MSC_VER
00077
00078 stream << "[" << id << "] in \"" << fct << "\" of " << file << "@" << line << endl << msg << endl ;
00079 #else
00080
00081 stream << "[" << id << "] in \"" << fct << "\" of " << file << " +" << line << " : " <<msg << endl ;
00082 #endif
00083
00084 if( !_disable )
00085 {
00086 cerr << stream.str() << endl ;
00087 }
00088
00089
00090 if( _fileOut.is_open() )
00091 {
00092 _fileOut << stream.str() << endl ;
00093 _fileOut.flush() ;
00094 }
00095
00096 if( _output )
00097 {
00098 _output->trace( id, fct, file, line, msg ) ;
00099 }
00100 }
00101
00102
00103
00104
00105 unsigned int Tracer::registerId( const char* id )
00106 {
00107 int intId = getHashId( id ) ;
00108 bool isNewId = _tracedIds[ intId ] != id ;
00109
00110 _tracedIds[ intId ] = id ;
00111
00112 return isNewId ? intId : 0 ;
00113 }
00114
00115
00116
00117
00118 void Tracer::unregisterId( const char* id )
00119 {
00120 int intId = getHashId( id ) ;
00121 _tracedIds.erase( intId ) ;
00122 }
00123
00124
00125
00126
00127 unsigned int Tracer::getHashId( const std::string& id )
00128 {
00129 unsigned int a = 64187 ;
00130 unsigned int b = 378533 ;
00131 unsigned int hashId = 0 ;
00132 for( std::size_t i = 0; i < id.length() ; i++ )
00133 {
00134 hashId = hashId * a + id[i] ;
00135 a = a * b;
00136 }
00137 return hashId ;
00138 }
00139
00140
00141
00142 void Tracer::traceAll( bool b )
00143 {
00144
00145 _traceAll = b ;
00146 }
00147
00148
00149
00150
00151 void Tracer::traceNone( bool b )
00152 {
00153
00154 _traceNone = b ;
00155 }
00156
00157
00158
00159
00160 void
00161 Tracer::setFile( const char* file )
00162 {
00163 if( file )
00164 {
00165
00166 if ( _fileOut.is_open() )
00167 {
00168 _fileOut.close() ;
00169 }
00170
00171
00172 _fileOut.open( file, ios_base::trunc ) ;
00173 }
00174 }
00175
00176
00177
00178
00179 void
00180 Tracer::setOutput( TracerOutput* output, bool disable )
00181 {
00182
00183 delete _output ;
00184
00185 _output = output ;
00186
00187 _disable = disable && output ;
00188 }