Home | nevrax.com |
|
How to log information (for debugging purpose) ?
The main functionalities are offered by the macros nldebug, nlinfo, nlwarning and also nlerror and nlassert, and by their variant forms (see debug.h). They allow to display strings, with open arguments, using the "printf" syntax. In the default behaviour, the text passed to nldebug is displayed into stdout in NL_DEBUG mode, but not in NL_RELEASE. The logged texts are also written into a file called "log.log" in the working directory. These macros print the strings with an information header. Example : sint32 age = -2; nldebug( "Toto is %d years old", age ); if ( age < 0 ) { nlerror( "Invalid age for toto : %d", age ); // the program will not come here because nlerror throws an exception to exit } STDOUT (logger thread_id file line: debug_string): DBG 1234 myfile.cpp 10: Toto is -2 years old ERR 1234 myfile.cpp 13: Invalid age for toto : -2 Abort FILE OUTPUT (date time logger debug_string): 01/04/11 18:24:50 DBG: Toto is -2 years old 01/04/11 18:24:50 ERR: Invalid age for toto : -2 Because NeL allows to create multithreaded programs, these macros use mutual exclusions (mutex) to ensure no data is corrupted and the displayed text not interlaced.
You may want to customize the logging system directly, for your own needs. Include "nel/misc/log.h".
Then, you can attach some displayers to the global loggers (NLMISC::CLog objects).
createDebug(); // automatically done in a "service" NLNET::CNetDisplayer *nd = new CNetDisplayer(); // the address of the Logging Server is automatically retrieved using the Naming Service if ( nd->connected() ) // this line is optional: here we don't want the displayer to attempt other connections if the first one failed { NLMISC::DebugLog.addDisplayer( nd ); } How to log a string without repeating the header ? Use the methods of NLMISC::CLog. Example : NLMISC::DebugLog.displayNL ( "Dump of Values :" ); for ( int j=0; j!=height; ++j ) { for ( int i=0; i!=width; ++i ) { NLMISC::DebugLog.displayRaw( "%d ", Values[j][i] ); } NLMISC::DebugLog.displayRawNL( ": line %d", j ); }
|