YANE-Framework Tutorial 1.1.0

Tutorial page of class yane::Utils::DebugMaster

Introduction

This demo shows the use of the debugmaster class. The classes yane::Utils::DebugMaster and yane::Utils::DebugClient are used to format and regulate the output of the program. Some features:

Using yane::Utils::DebugMaster and yane::Utils::DebugClient

This is a short introduction into using the debugmaster and debugclient.

Every program usually uses only one debugmaster or more specific a class which is derived from yane::Utils::DebugMaster. The debugmaster defines where to put the output (e.g. console or a file) and how the output is formatted.
Usually there is more than one debugclient in one programm which is derived from yane::Utils::DebugClient. For example, every thread should possess its own debugclient. The debugclient defines the name of the "output" (visible in the debug-output).

A debug message has two parts. The required part are the timer, the name of the debugclient and a message, the optional part is some added data.

For more information look at the documentation of the class.

Libraries

The first thing to do is to define the libraries which we require. These libraries form the head of our main program:

#include <iostream>
#include <cstdlib>
#include <cmath>

#include <yane/utils.h>

#include "Debugmasterhtml/debugmasterhtml.h"

Namespaces

Having defined the libraries which we are going to use, we now define the output namespaces:

using namespace std;

Creating a debugmaster.

First we need to a yane::Utils::RTClock object. After that we are able to create a debugmaster object via one of the inherited classes. Here, yane::Utils::DebugMasterConsole uses the console as output, whereas yane::Utils:DebugMasterFile or DebugMasterHTML would use a formatted file. After that we need to set the output level between 0 and 6. Depending on the output level, all data (level 6) or only important data (e.g. level 2) are debugged.

int main()
        yane::Utils::RTClock * clock = new yane::Utils::RTClock();

        yane::Utils::DebugMasterConsole * master = new yane::Utils::DebugMasterConsole(clock, 90);
        // yane::Utils::DebugMasterFile * master = new yane::Utils::DebugMasterFile(clock, "test.log", 90, true);
        // yane::Utils::DebugMasterHTML * master = new yane::Utils::DebugMasterHTML(clock, "debug.html", 120, true);

        master->setLevel(4);

Using a debugmaster.

First we need a debugclient. The debugclient uses the debugmaster to store messages. Usually, there is more than one client per master. After that we create some output data.

        yane::Utils::DebugClient * main_debug = master->newClient("Main");

        int n = 5;
        double * array = new double[n];
        for(...)
                array[i] = exp(i);

        string str = "Testmessage";

Now we have some data and are able to debug it. Here, we debug test data which consists of a double array, a single double and a string.

        main_debug->sendDebugInfo("Hello World", "Testdata", array, sizeof(double)*n, DebugMaster::T_DOUBLEARRAY, 0);
        main_debug->sendDebugInfo("Hello World 2", "Testdata", &array[3], sizeof(double), DebugMaster::T_DOUBLE , 3);
        main_debug->sendDebugInfo("Hello World 3", "Testdata", const_cast<char*>(str.c_str()), str.length(), DebugMaster::T_STRING , 3);

The next line, however, won't be debugged since our current debug level is 4 and 6 > 4. After the debug level is set to 6, we are able to debug the same message.

        main_debug->sendDebugInfo("Hello World 4", "Testdata", 0, 0, 0, 6);

        master->setLevel(6);

        main_debug->sendDebugInfo("Hello World 5", "Testdata", 0, 0, 0, 6);

Until now we only used the "Main" debug client. Next, we assume that several threads or a different part of the program to exist. We create a new debugclient and are able to debug data with a new name, e.g. "Thread". The name is displayed in the debug message.

        yane::Utils::DebugClient * thread_debug = master->newClient("Thread");

        thread_debug->sendDebugInfo("Hello World", "Testdata", array, sizeof(double)*n, DebugMaster::T_DOUBLEARRAY, 0);
        thread_debug->sendDebugInfo("Hello World 2", "Testdata", &array[3], sizeof(double), DebugMaster::T_DOUBLE , 3);

Closing the main program

Last, before closing our main program, we delete all the constructed objects and the allocated memory:

        delete clock;
        delete master;
        delete main_debug;
        delete thread_debug;

Writing our own master.

Of course it is possible to write your own debugmaster. To this end, you have to extend the yane::Utils::DebugMaster class and overload, e.g., the constructor or the following class to provide your own formatting:

virtual void addDebugInfo(std::string clientname, std::string message, std::string dataname, void* data, int datasize, int datatype, int level);

For further details please take a look at the class DebugMasterHTML.