PDA

View Full Version : A little debugging helper class



toutarrive
4th July 2010, 15:25
Hi all,

Just a little piece of code i wanted to share.
It's an helper to trace the function calls while debugging.
It has been tested with GGC and Windows only.
I've got still some work to do with threaded function call

scope.h


#ifndef SCOPE_H
#define SCOPE_H

#include<QString>
#include"windows.h"

#define FUNC Logger log(__FUNCTION__)
#define CONSOLE Signature(Q_FUNC_INFO)

// Use macro LOG in the function(s) you want to trace
#define LOG FUNC;CONSOLE;

// To keep your comments indented.
#define INDENT Indent()

class Logger {

public:
enum COLORS {
BLACK = 0,
BLUE = FOREGROUND_BLUE,
GREEN = FOREGROUND_GREEN,
CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE,
RED = FOREGROUND_RED,
MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE,
BROWN = FOREGROUND_RED | FOREGROUND_GREEN,
LIGHTGRAY = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
DARKGRAY = FOREGROUND_INTENSITY,
LIGHTBLUE = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
LIGHTGREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
LIGHTCYAN = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
LIGHTRED = FOREGROUND_RED | FOREGROUND_INTENSITY,
LIGHTMAGENTA = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
};
Logger (const QString &str);
~Logger ();
unsigned getIndent(){return m_indent;}
friend void Color(WORD Attributes);
friend void Signature(const QString & fct);
friend void Indent( );
private:
static unsigned m_indent;
const QString m_text;

};

extern unsigned idx;

#endif //SCOPE_H



scope.cpp



#include "logger.h"


//void Color(WORD Attributes);
unsigned Logger::m_indent = 0;
unsigned idx = 0;




Logger::Logger (const QString &str)
: m_text(str)
{
Color(LIGHTBLUE );
QString text = QString( m_indent++ <<1, ' ') + "[+] BEGIN " + m_text;
fprintf(stderr, "%s\n", qPrintable(text));
idx = getIndent();
Color(WHITE );
}

Logger::~Logger ()
{
Color(LIGHTBLUE);
QString text = QString( --m_indent <<1, ' ')+ "[-] END " + m_text ;
fprintf(stderr, "%s\n\n", qPrintable(text));
idx = getIndent();
Color(WHITE );
}


inline void Color(WORD Attributes)
{
HANDLE H=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(H,Attributes);
}

void Signature(const QString & fct)
{
Color(Logger::CYAN);
QString text = QString( idx <<1, ' ')+ fct + " was called";
fprintf(stderr, "%s\n", qPrintable(text));
Color(Logger::WHITE);
}

// To keep your comments indented through each function call ( using qDebug() )
// USAGE: INDENT; qDebug() << "A comment"<< aValue;
void Indent()
{
QString text =QString( idx<<1, ' ');
fprintf(stderr, "%s", qPrintable(text));
}


Hope you find it useful.

toutarrive
4th July 2010, 17:22
Just to show you how to use it:



#include "scope.h"

int main (int argc, char ** argv)
{
LOG
if ( ! createConnection()) return -1;
QApplication app(argc, argv);
app.setStyle(new CustomStyle);

INDENT; qDebug()<<"This is a comment, we are in Main";

InfoBox mainVindow;
mainVindow.show();
return app.exec();
}


Repeat the process in yours others source files, where you want to trace your functions calls.