# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

How to log information (for debugging purpose) ?

Author:
Olivier Cado
Date:
Updated May 4 2001
This document explains how to log some information (into the screen, into a file or to a logging server), e.g. in order to debug your code.

Basic usage of the debugging system

The debugging sytem allows to log information at various levels. Include the header file "nel/misc/debug.h".

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.

Advanced usage of the logging system

You may want to customize the logging system directly, for your own needs. Include "nel/misc/log.h".

Initialization

If your program is not a "service" in the NeL terms (i.e. if it is not built on NLNET::IService), first call createDebug() to create the basic global loggers ErrorLog, WarningLog, InfoLog, DebugLog and AssertLog (the ones that are used by the nlerror, nlwarning, nlinfo, nldebug and nlassert macros).

Then, you can attach some displayers to the global loggers (NLMISC::CLog objects).

  • NLMISC::CStdDisplayer is for stdout (the console, the VC++ output window...). It is attached by default to all of the five logger objects mentionned above.
  • NLMISC::CFileDisplayer is for a file.
  • NLMISC::CMsgBoxDisplayer is for a message box.
  • NLNET::CNetDisplayer is for a logging server (see CLogService in the nelns documentation).
  • You can can create your own displayer, in order to print the logged text onto a new target (for example, see the class CChatDisplayer in Snowballs 0.2) or to customize the filter on the header.
Example (we assume CNetDisplayer allows to log via the network):
 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 );
 }

Logging information

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 );
 }