Tracer used to output messages. More...
#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. |
Tracer used to output messages.
This tracer singleton gives an easy way to create a log in your programs. In the log, only the registered ids are displayed.
To use the trace you must register the ids. To do that you can define a function like this:
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 }
And call it in the begining of your main function:
int main( int argc, char* argv[] ) { initTracer( true, string() ) ; // All trace / No file ... }
To display a message in the log
TRACE( "MyWarning", "the message " << "is build " << "with a stream " << myData ) ;
The result with Windows will be (in visual studio format double click on the line will go to the right line in the file)
[MyWarning] in "myObject::myFunction" of myFile.cpp@100
the message is build with a stream 365
The result with Linux will be (copy/paste the file name and line to go to the right line in the file with vim)
[MyWarning] in "myFunction" of myFile.cpp +100 : the message is build with a stream 365
To increase performance, all the registered ids are stored in a map which is indexed by the hash code of the string. For better performances, it is better to use the hash code instead of the string, nevertheless bost work well.
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" ) ;
then you can use
TRACE( MyWarning, "the message " << "is build " << "with a stream " << myData ) ;
The performance is increased because the hash code don't need to be compute anymore.
The traceNone takes precedence over traceAll.
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::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().
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::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. |
Close the previous file if necessary.
Definition at line 161 of file OBTTrace.cpp.
References _fileOut.
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. |
Destroy the previous output if necessary. Be carreful, Ogre visualisation creates its own output, it can destroy yours.
Definition at line 180 of file OBTTrace.cpp.
bool Tracer::test | ( | unsigned int | id | ) |
Test if the id is traced.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 60 of file OBTTrace.cpp.
References _traceAll, _tracedIds, and _traceNone.
00061 { 00062 return !_traceNone && ( _traceAll || _tracedIds.find( id ) != _tracedIds.end() ) ; 00063 }
bool Tracer::test | ( | const char * | id | ) |
Test if the id is traced.
[in] | id | The id of the trace which will be done. |
You should not use this method. It is used by the macros to know if the message must be displayed.
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 }
void OBT::Tracer::trace | ( | unsigned int | id, | |
const char * | fct, | |||
const char * | file, | |||
const int | line, | |||
const char * | msg | |||
) | [inline] |
Traces the message.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 204 of file OBTTrace.h.
00208 { if ( test( id ) ) { traceWithoutTest( _tracedIds[ id ].c_str(), fct, file, line, msg ) ; } }
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. |
You should not use this method, instead you should use the following macros:
Definition at line 197 of file OBTTrace.h.
00201 { if ( test( id ) ) { traceWithoutTest( id, fct, file, line, msg ) ; } }
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 |
traceAll is inefficient if traceNone is called.
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::traceWithoutTest | ( | const char * | id, | |
const char * | fct, | |||
const char * | file, | |||
const int | line, | |||
const char * | msg | |||
) | [protected] |
Traces the message without test the id.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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 }
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 }
The singleton is a friend.
Definition at line 141 of file OBTTrace.h.
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::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().
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().
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().
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().
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().