Little debugging helper

Just wanted to share this code with you:

#include <QString>
#ifndef QT_NO_DEBUG
#    define DEBUG_FUNC_NAME DbgHelper dbgHelper(Q_FUNC_INFO);
#    define DEBUG_FUNC_NAME
class DbgHelper {
    DbgHelper(const QString &t);
    QString txt;
    static int indent;
    static int colorIndex;
    int myColor;
#include "dbghelper.h"
#include <stdio.h>
#include <QtDebug>
int DbgHelper::indent = 0;
int DbgHelper::colorIndex = 0;
static void DbgHelper_output(int color, int indent, 
                             const QString &prefix, const QString &funcName) {
  QString text = QString(4*indent, ' ')+QString(prefix+" "+funcName);
  qDebug() << text;
  fprintf(stderr, "%s\n", qPrintable(text));
DbgHelper::DbgHelper(const QString &t) {
    txt = t;
#ifdef NO_COLOR
    myColor = colorIndex;
    colorIndex = (colorIndex+1) % 7;
    DbgHelper_output(myColor, indent, "BEGIN", txt);
DbgHelper::~DbgHelper() {
    DbgHelper_output(myColor, indent, "END", txt);

Now you can do this:

#include "dbghelper.h"
#include <QtDebug>
class Cls {
  ~Cls() {
  void method(const QString &str) {
    int x = anotherMethod();
  int anotherMethod() {
    qDebug() << "Regular output";
    return 7;
int main(){
 Cls c;
 return 0;

And obtain a nice colorful (where ANSI escape sequences are supported) output:

BEGIN Cls::Cls()
END Cls::Cls()
BEGIN void Cls::method(const QString&)
    BEGIN int Cls::anotherMethod()
Regular output
    END int Cls::anotherMethod()
END void Cls::method(const QString&)
BEGIN Cls::~Cls()
END Cls::~Cls()

If you want, you can extend it to take argument values as well or print the file and line numbers where a particular method is defined.

What do you think? Do you have your own macros or classes that help you with debugging?

12 Responses to “Little debugging helper”

  1. Keith says:

    Wow Wysota. Very nice work. Only thing, would be nice to add a time to check the time how long it took to execute the function and what not.

  2. Hermann says:

    Thanks! Really useful!!!

  3. Kichi says:

    Very good work.

    When I tried your code, I found 2 things.
    First, it is need to include in debhelper.cpp.
    Second, it is need to delete typo character “.” after ~DbgHelper(){.

    I’m sorry in poor English.

  4. wysota says:

    That’s very easy, just add QTime::currentTime().toString(“hh:mm:ss.zzz”) to the string being written to debug or use QTime::elapsed().

    Thanks, I removed the dot (something must have gotten messed up while copying code to WordPress). I also added an include to QtDebug, if that’s what you meant.

  5. Sunil Thaha says:

    Hmm … interesting :-)

    I will send you my version. Perhaps we could create a tree model, and create a gui around it, so that one can look at the trace back.

  6. cadillac says:

    А можно я перетяну ваш пост в свой блог ? На правах копи-паста конечно укажу источником

  7. sergeyua says:

    Не могу скормить ваш rss ридеру Abilon. Вернее получается, но читать не получается – прога кажись не может ничего получить. Пытаюсь добавить вот этот адрес ленты – Подскажите дебилу – кто чем читает

  8. longines says:

    wysota, спасибо за классный пост. Нечасто я благодарю блоггеров, но тут вот захотел.

  9. greatkir says:

    wysota, спасибо за пост. Редко я такое говорю, но тут вот захотелось.

  10. emiola says:

    Thanks Wysota, it’s really useful. But what license I must consider if I use it in a real project? Regards.

  11. knishua says:


    I want to thank you. Your blog helped sort the problem of running a qt sql application on any computer by adding the


    I want to acknowledge this in front of the web community.



Leave a Reply to knishua