Using tgt's logging features
Overview
This tutorial introduces tgt’s logging system. It covers the configuration of loglevels, different kinds of logging technics such as console logging or html file logging and how to use logging in custom classes.
Working with different Loggers
Each logging command is sent to the log manager singleton. The LogMgr passes these logging commands to each registered logger. For obvious reasons, there can be only one command line logger, but there can be as many other loggers as you want. tgt comes with console, text file and html file output.
Each log message has a categorie and an error level. The categorie is simply a string but it’s intend is to offer a hierachical strucutre with different substructures seperated by a dot. For example: “tgt:Texture:Manager”. Loglevels are DEBUG, INFO, WARNING, ERROR, FATAL in this order.
It depends on the actual logger what is logged and what isn’t. tgt standard configuration is a console logger logging everything with loglevel INFO.
Adjusting loglevels and categories
Below you can see example code on how to wokr with loggers. A html log is created and conifgured as well as a console log.
Note that there can be only on console log registered in the log manager. If you are trying to add a second the first one will be deleted. Therefor the log manager offers a function getConsoleLog() to alter the state of the current console logger.
As stated above, tgt uses logging in categories and you can add log categories and the loglevel you want to the loggers:
// create a html file logging all children from every source (for example
// to log tgt messages as well)
// with date, timestamp, category and LogLevel
HtmlLog * htmlLog = new HtmlLog( "log.html", true, true, true, true);
htmlLog->addCat( "", true, Debug );
// create standard console log listening only to log messages
// from myApp.toto with log level INFO
// There can be only one ConsoleLog, so this one will replace the old one.
ConsoleLog * consoleLog1 = new ConsoleLog();
consoleLog1->addCat( "myApp.toto", false, Info );
// Adding loggers to the tgt::LogManager Singleton LogMgr.
// The logmanager will take care of theire deletion.
LogMgr.addLog(htmlLog);
LogMgr.addLog(consoleLog1);
// Get the current console logger and add a logging categorie.
ConsoleLog * consoleLog2 = LogMgr.getConsoleLog();
consoleLog2->addCat( "myApp.tata", false, Error );A logging example
Now that we have configured our log enviroment we can start logging.
How to use logging in a custom class
The below describes how to enhance your own classes to support tgt logging. First of all, the correspondent .h file should be included.
#include "tgt/logmanager.h"
Every class, that wants to use logging must have its proper categorie. This category is set by a static class variable called loggerCat_. This is a string and should cointain something descriptive like “myApp.Toto” in the example below.
tata.cpp
class Toto {
protected:
static const std::string loggerCat_;
public:
Toto() {};
void foo();
}; Sending log messages is done by using the approriate macros. There is one macro for each log level. The categorie for the message is retrieved from the loggerCat_ variable.
toto.cpp
const std::string Toto::loggerCat_("myApp.toto");
void Toto::foo(){
LDEBUG("This is a debug message");
LINFO("This is an info message");
LWARNING("This is a warning message");
LERROR("This is an error message");
LFATAL("This is a fatal message");
} If you are going to call Toto::foo() now, the above log messages are sent to the logmanager wich distributes them to its registered loggers. If you compile and run the logging example in the tgt/sample/ directory the output will be like the ones shown below.

The sample program creates a html file as well. It’s displayed below in a browser window.



