Logging

Summary

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 Logging System

The debugging system 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 behavior, 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 :

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 you to create multi-threaded 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):

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 do you log a string without repeating the header? You can use the methods on 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 );
}