Little debugging helper

Just wanted to share this code with you:

#ifndef DBGHELPER_H
#include <QString>
#ifndef QT_NO_DEBUG
#    define DEBUG_FUNC_NAME DbgHelper dbgHelper(Q_FUNC_INFO);
#else
#    define DEBUG_FUNC_NAME
#endif
 
class DbgHelper {
public:
    DbgHelper(const QString &t);
    ~DbgHelper();
private:
    QString txt;
    static int indent;
    static int colorIndex;
    int myColor;
};
#endif
#include "dbghelper.h"
#ifdef DBGHELPER_USES_PRINTF
#include <stdio.h>
#else
#include <QtDebug>
#endif
 
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);
  if(color>=0){
    text.prepend("\x1b[3"+QString::number(1+color)+"m");
    text.append("\x1b[39m");
  }
#ifndef DBGHELPER_USES_PRINTF
  qDebug() << text;
#else
  fprintf(stderr, "%s\n", qPrintable(text));
#endif
}
 
DbgHelper::DbgHelper(const QString &t) {
    txt = t;
#ifdef NO_COLOR
    myColor=-1;
#else
    myColor = colorIndex;
    colorIndex = (colorIndex+1) % 7;
#endif
    DbgHelper_output(myColor, indent, "BEGIN", txt);
    ++indent;
}
 
DbgHelper::~DbgHelper() {
    --indent;
    DbgHelper_output(myColor, indent, "END", txt);
}

Now you can do this:

#include "dbghelper.h"
#include <QtDebug>
 
class Cls {
public:
  Cls(){
    DEBUG_FUNC_NAME
  }
  ~Cls() {
    DEBUG_FUNC_NAME
  }
  void method(const QString &str) {
    DEBUG_FUNC_NAME
    int x = anotherMethod();
  }
  int anotherMethod() {
    DEBUG_FUNC_NAME
    qDebug() << "Regular output";
    return 7;
  }
};
 
int main(){
 Cls c;
 c.method("foobar");
 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:

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

    @Kichi:
    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:

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

  7. sergeyua says:

    Не могу скормить ваш rss ридеру Abilon. Вернее получается, но читать не получается – прога кажись не может ничего получить. Пытаюсь добавить вот этот адрес ленты – http://blog.wysota.eu.org/index.php/feed/. Подскажите дебилу – кто чем читает blog.wysota.eu.org?

  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:

    Hi,

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

    QApplication::addLibraryPath(“.\\plugins”);

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

    Brgds,

    kNish

Leave a Reply to wysota