Results 1 to 9 of 9

Thread: Debug macros in 64bit systems

  1. #1
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Debug macros in 64bit systems

    Hi all,

    I use the following debug macro to quicly check variables values in code of interest:

    #define dbs(x) qDebug(QVariant(x).toString().latin1())

    I can write something like

    Qt Code:
    1. dbs(5);
    2. dbs("5");
    3.  
    4. int num = 5;
    5. dbs(num);
    6.  
    7. QString str("5");
    8. dbs(str);
    To copy to clipboard, switch view to plain text mode 

    and everything works as expected.

    I have a problem with container's sizes that are size_t types.

    As example:

    Qt Code:
    1. QValueVector<int> v;
    2. v.append(5);
    3.  
    4. dbs(v.count()); // gives an error on 64bit systems
    To copy to clipboard, switch view to plain text mode 

    This is because size_t is an unsigned int 64 bits long and cannot be auto converted by compiler to int or uint types that are 32 bits and are the only constructors defined by QVariant for integral types.

    How can do my macro work as expected? there exsist another way to write a macro with the same results.

    I searched on the net with no results, so here I am asking for an help.

    Thanks
    Marco
    Last edited by wysota; 2nd December 2006 at 08:09. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Debug macros in 64bit systems

    Why not use qDebug class instead?

    Qt Code:
    1. #include <QtDebug>
    2. //...
    3. qDebug() << v.count();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Debug macros in 64bit systems

    int num = 5;
    qDebug() << num;

    /usr/lib/qt3//include/qglobal.h:975: error: too few arguments to function ‘void qDebug(const char*, ...)’git_startup.cpp:529: error: at this point in file

    Thanks
    Marco

  4. #4
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Debug macros in 64bit systems

    BTW I have qt 3.3.6 installed.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Debug macros in 64bit systems

    Quote Originally Posted by mcostalba View Post
    BTW I have qt 3.3.6 installed.
    Ah, sorry, I missed that

    There is a QVariant constructor that takes a Q_LLONG argument, I think it should work for you. Try using
    Qt Code:
    1. dbs((Q_LLONG)v.count());
    To copy to clipboard, switch view to plain text mode 

    Casting the value to uint should work as well, especially that QValueVector::count() should return an uint all by itself... If you have a 64b architecture then uint should be 64 bits wide as well. Are you sure your code is correct?

  6. #6
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Smile Re: Debug macros in 64bit systems

    Thanks, currently I use

    dbs((int)v.count());

    and it works....but it's ugly and easy to forget to cast. The problem is I have a 32bit box and I don't experience the compile error when writing

    dbs(v.count());

    but my tool is publicy released to wider audience, with any kind of system. BTW the compile error was found by an user and reported to me. So I really would like to have a dbs(x) macro that prints _anything_ 'x' is without always remember to cast to int/uint the size_t values.

    Thanks
    Marco

    P.S: I cannot subclass QVariant because key 'd' internal variable is private. I was thinking at template specialization, something like:

    template<typename T>class MyVariant {
    public:
    MyVariant(T x) : v(x){}
    QString toString() { return v.toString(); }
    private:
    QVariant v;
    };
    template<>class MyVariant<size_t> {
    public:
    MyVariant(size_t x) : v((uint)x){}
    QString toString() { return v.toString(); }
    private:
    QVariant v;
    };


    But in this case I need to pass the type when instancing the template or as macro parameter.


    Any ideas??

    Thanks
    Marco

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Debug macros in 64bit systems

    Maybe you should implement a series of (template) functions that would do debugging? You could handle specific types yourself and leave all the others to the template function.

    For instance:

    Qt Code:
    1. template<class T> void myDbg(T a){
    2. qDebug(a);
    3. }
    4.  
    5. void myDbg(size_t a){
    6. qDebug((int)a);
    7. }
    8.  
    9. void myDbg(QString s){
    10. qDebug("%s", s.latin1());
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Debug macros in 64bit systems

    A great idea. Thanks!

    It works. I updated my common header with:

    template<typename T> const QString _valueOf(const T& x) { return QVariant(x).toString(); }
    inline const QString _valueOf(const QString& x) { return x; }
    inline const QString _valueOf(size_t x) { return QString::number((uint)x); }

    // some debug macros
    #define dbg(x) qDebug(#x " is <%s>", _valueOf(x).latin1())
    #define dbs(x) qDebug(_valueOf(x))
    #define dbp(s, x) qDebug(QString(s).arg(_valueOf(x)))


    Thanks again
    Marco

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Debug macros in 64bit systems

    I suggest you also do something like this to easily disable debugging:

    Qt Code:
    1. #ifndef NO_DISPLAY_DEBUG
    2. # define dbg(x) qDebug(#x " is <%s>", _valueOf(x).latin1())
    3. # define dbs(x) qDebug(_valueOf(x))
    4. # define dbp(s, x) qDebug(QString(s).arg(_valueOf(x)))
    5. #else
    6. # define dbg(x)
    7. # define dbs(x)
    8. # define dbp(s, x)
    9. #endif
    To copy to clipboard, switch view to plain text mode 

    I'm not sure about the syntax of the second section, so be prepared for compile errors

Similar Threads

  1. Replies: 11
    Last Post: 22nd March 2006, 19:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.