#include <OBTTrace.h>
Public Member Functions | |
unsigned int | registerId (const char *id) |
Registers a new id in the ones which will be traced. | |
void | unregisterId (const char *id) |
Unregisters an id. | |
void | traceAll (bool b=true) |
Sets the flag to trace all the messages. | |
void | traceNone (bool b=true) |
Sets the flag to trace all the messages. | |
void | setFile (const char *file=0) |
Activates the dump of the messages on a file. | |
void | setOutput (TracerOutput *output, bool disable) |
Activates the output in an additional output. | |
void | trace (const char *id, const char *fct, const char *file, const int line, const char *msg) |
Traces the message. | |
void | trace (unsigned int id, const char *fct, const char *file, const int line, const char *msg) |
Traces the message. | |
bool | test (const char *id) |
Test if the id is traced. | |
bool | test (unsigned int id) |
Test if the id is traced. | |
~Tracer () | |
Destructor. | |
Protected Member Functions | |
Tracer () | |
Private constructor. | |
void | traceWithoutTest (const char *id, const char *fct, const char *file, const int line, const char *msg) |
Traces the message without test the id. | |
unsigned int | getHashId (const std::string &id) |
Compute the hash id for a string id. | |
Protected Attributes | |
std::ofstream | _fileOut |
The output file. | |
bool | _traceAll |
The trace all flag. | |
bool | _traceNone |
The trace none flag. | |
bool | _disable |
The disable flag. | |
std::map< unsigned int, std::string > | _tracedIds |
The set of the traced ids. | |
TracerOutput * | _output |
The object which outputs the message. | |
Friends | |
class | Singleton< Tracer > |
The singleton is a friend. |
This tracer singleton gives an easy way to create a log in your programs. In the log, only the registered ids are displayed.
void initTracer( bool traceAll, const std::string& file ) { #if !defined NDEBUG OBT::Singleton< OBT::Tracer >::getInstance().traceAll( traceAll ) ; // To display all traces OBT::Singleton< OBT::Tracer >::getInstance().setFile( file ) ; // To put the traces in the file // Other ids to register OBT::Singleton< OBT::Tracer >::getInstance().registerId( "MyWarning" ) ; ... #endif // !NDEBUG }
int main( int argc, char* argv[] ) { initTracer( true, string() ) ; // All trace / No file ... }
TRACE( "MyWarning", "the message " << "is build " << "with a stream " << myData ) ;
[MyWarning] in "myObject::myFunction" of myFile.cpp@100
the message is build with a stream 365
[MyWarning] in "myFunction" of myFile.cpp +100 : the message is build with a stream 365
To use unsigned int instead of string, the returned id gave by registerId must be stored to be use by the TRACE macro.
unsigned int MyWarning = OBT::Singleton< OBT::Tracer >::getInstance().registerId( "MyWarning" ) ;
TRACE( MyWarning, "the message " << "is build " << "with a stream " << myData ) ;
trace and test methods are used by the macros, so you have not to use them directly.
Definition at line 138 of file OBTTrace.h.
Tracer::~Tracer | ( | ) |
Tracer::Tracer | ( | ) | [protected] |
Private constructor.
Definition at line 26 of file OBTTrace.cpp.
00027 : 00028 _traceAll( false ), 00029 _traceNone( false ), 00030 _disable( false ), 00031 _tracedIds(), 00032 _output( 0 ) 00033 { 00034 }
unsigned int Tracer::registerId | ( | const char * | id | ) |
Registers a new id in the ones which will be traced.
[in] | id | The new id. |
Definition at line 105 of file OBTTrace.cpp.
References _tracedIds, and getHashId().
00106 { 00107 int intId = getHashId( id ) ; 00108 bool isNewId = _tracedIds[ intId ] != id ; 00109 // inserts the new id or update the values 00110 _tracedIds[ intId ] = id ; 00111 00112 return isNewId ? intId : 0 ; 00113 }
void Tracer::unregisterId | ( | const char * | id | ) |
Unregisters an id.
[in] | id | The id to remove. |
Definition at line 118 of file OBTTrace.cpp.
References _tracedIds, and getHashId().
00119 { 00120 int intId = getHashId( id ) ; 00121 _tracedIds.erase( intId ) ; 00122 }
void Tracer::traceAll | ( | bool | b = true |
) |
Sets the flag to trace all the messages.
[in] | b | The value of the flag. True is the default |
Definition at line 142 of file OBTTrace.cpp.
References _traceAll.
00143 { 00144 // sets the flag 00145 _traceAll = b ; 00146 }
void Tracer::traceNone | ( | bool | b = true |
) |
Sets the flag to trace all the messages.
[in] | b | The value of the flag. True is the default |
Definition at line 151 of file OBTTrace.cpp.
References _traceNone.
00152 { 00153 // sets the flag 00154 _traceNone = b ; 00155 }
void Tracer::setFile | ( | const char * | file = 0 |
) |
Activates the dump of the messages on a file.
[in] | file | The name of the file, if empty no file will be created, it is the default. |
Definition at line 161 of file OBTTrace.cpp.
References _fileOut.
00162 { 00163 if( file ) 00164 { 00165 // First close the previous file 00166 if ( _fileOut.is_open() ) 00167 { 00168 _fileOut.close() ; 00169 } 00170 00171 // Open the new one 00172 _fileOut.open( file, ios_base::trunc ) ; 00173 } 00174 }
void Tracer::setOutput | ( | TracerOutput * | output, | |
bool | disable | |||
) |
Activates the output in an additional output.
[in] | output | The additional output. |
[in] | disable | To disable the std::cerr output during the validity of tha additional output. If output is null automatically set disable to false. |
Definition at line 180 of file OBTTrace.cpp.
References _disable, and _output.
00181 { 00182 // First delete the previous output 00183 delete _output ; 00184 // set the new one 00185 _output = output ; 00186 // set the disable flag 00187 _disable = disable && output ; // if(output == null) then enable the cerr output even if disable is true 00188 }
void OBT::Tracer::trace | ( | const char * | id, | |
const char * | fct, | |||
const char * | file, | |||
const int | line, | |||
const char * | msg | |||
) | [inline] |
Traces the message.
[in] | id | The id of the trace. |
[in] | file | The name of the current code file. |
[in] | line | The number of the current line. |
[in] | txt | The message. |
Definition at line 197 of file OBTTrace.h.
00197 { if ( test( id ) ) { traceWithoutTest( id, fct, file, line, msg ) ; } } 00200 void trace( unsigned int id, 00201 const char* fct,
void OBT::Tracer::trace | ( | unsigned int | id, | |
const char * | fct, | |||
const char * | file, | |||
const int | line, | |||
const char * | msg | |||
) | [inline] |
Traces the message.
Definition at line 204 of file OBTTrace.h.
00204 { if ( test( id ) ) { traceWithoutTest( _tracedIds[ id ].c_str(), fct, file, line, msg ) ; } }
bool Tracer::test | ( | const char * | id | ) |
Test if the id is traced.
[in] | id | The id of the trace which will be done. |
Definition at line 52 of file OBTTrace.cpp.
References _traceAll, _tracedIds, _traceNone, and getHashId().
00053 { 00054 return !_traceNone && ( _traceAll || _tracedIds.find( getHashId( id ) ) != _tracedIds.end() ) ; 00055 }
bool Tracer::test | ( | unsigned int | id | ) |
Test if the id is traced.
Definition at line 60 of file OBTTrace.cpp.
References _traceAll, _tracedIds, and _traceNone.
00061 { 00062 return !_traceNone && ( _traceAll || _tracedIds.find( id ) != _tracedIds.end() ) ; 00063 }
void Tracer::traceWithoutTest | ( | const char * | id, | |
const char * | fct, | |||
const char * | file, | |||
const int | line, | |||
const char * | msg | |||
) | [protected] |
Traces the message without test the id.
Definition at line 68 of file OBTTrace.cpp.
References _disable, _fileOut, _output, and OBT::TracerOutput::trace().
00073 { 00074 // must be traced 00075 ostringstream stream ; 00076 #ifdef _MSC_VER 00077 // The debug message is set for the visual studio format 00078 stream << "[" << id << "] in \"" << fct << "\" of " << file << "@" << line << endl << msg << endl ; 00079 #else 00080 // The debug message is set for the vim format 00081 stream << "[" << id << "] in \"" << fct << "\" of " << file << " +" << line << " : " <<msg << endl ; 00082 #endif 00083 // Traces the message on cerr 00084 if( !_disable ) 00085 { 00086 cerr << stream.str() << endl ; 00087 } 00088 00089 // Traces the message on the file, if it is defined 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 }
unsigned int Tracer::getHashId | ( | const std::string & | id | ) | [protected] |
Compute the hash id for a string id.
[in] | id | The id of the trace. |
Definition at line 127 of file OBTTrace.cpp.
Referenced by registerId(), test(), and unregisterId().
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 }
std::ofstream OBT::Tracer::_fileOut [protected] |
The output file.
Defined by traceOnFile.
Definition at line 228 of file OBTTrace.h.
Referenced by setFile(), traceWithoutTest(), and ~Tracer().
bool OBT::Tracer::_traceAll [protected] |
The trace all flag.
Defined by traceAll.
Definition at line 232 of file OBTTrace.h.
Referenced by test(), and traceAll().
bool OBT::Tracer::_traceNone [protected] |
The trace none flag.
Defined by traceNone.
Definition at line 235 of file OBTTrace.h.
Referenced by test(), and traceNone().
bool OBT::Tracer::_disable [protected] |
The disable flag.
Use to disable the std::cerr output when a _output is set.
Definition at line 239 of file OBTTrace.h.
Referenced by setOutput(), and traceWithoutTest().
std::map< unsigned int, std::string > OBT::Tracer::_tracedIds [protected] |
The set of the traced ids.
Feed by traceAll.
Definition at line 243 of file OBTTrace.h.
Referenced by registerId(), test(), and unregisterId().
TracerOutput* OBT::Tracer::_output [protected] |
The object which outputs the message.
Set by .
Definition at line 247 of file OBTTrace.h.
Referenced by setOutput(), traceWithoutTest(), and ~Tracer().