Results 1 to 12 of 12

Thread: Macro used for debugging

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Macro used for debugging

    Hi, Below is a macro I use for debugging. Before I put this in Wiki I thought I would post it here so that if it is useful, we will put it in wiki

    Qt Code:
    1. // --- Debugging ---
    2. #include <QtDebug>
    3.  
    4. #define APP_NO_DEBUG_MESSAGES
    5.  
    6.  
    7. #if !defined( APP_NO_DEBUG_MESSAGES )
    8.  
    9. // Flags for Debug Output
    10. # define DEBUG_PRINT_FILEPATH
    11. # define DEBUG_PRINT_SHORT_FN_NAME
    12. # define DEBUG_PRINT_LINENUMBER
    13.  
    14. // Adjust Length of fields Here
    15. // FileName
    16. # define DEBUG_FILEPATH_LENGTH 27
    17. # define DEBUG_FILENAME_LENGTH 22
    18. // Function
    19. # define DEBUG_LONG_FUNCTION_LENGTH 40
    20. # define DEBUG_SHORT_FUNCTION_LENGTH 25
    21.  
    22. // LineNumber
    23. # define DEBUG_LINENUMBER_LENGTH 4
    24.  
    25. // file names
    26. # if defined( DEBUG_PRINT_FILEPATH )
    27. # define DEBUG_FILENAME ( QString( "%1").arg(__FILE__ , \
    28. -DEBUG_FILEPATH_LENGTH, QLatin1Char(' ')) )
    29.  
    30. # elif defined( DEBUG_PRINT_FILENAME )
    31. # include <QFileInfo>
    32. # define DEBUG_FILENAME ( QString( "%1").arg( \
    33. QFileInfo(__FILE__).fileName(), \
    34. -DEBUG_FILENAME_LENGTH, QLatin1Char(' ')) )
    35.  
    36. # elif defined( DEBUG_NO_FILENAME )
    37. # define DEBUG_FILENAME ""
    38. # else
    39. # error No debug file flags defined
    40. # endif
    41.  
    42. // function Names
    43. # if defined( Q_CC_GNU )
    44. # if defined ( DEBUG_PRINT_LONG_FN_NAME )
    45. # define DEBUG_FUNCTION_NAME QString( " %1").arg( __PRETTY_FUNCTION__, \
    46. -DEBUG_FULL_FUNCTION_LENGTH, QLatin1Char(' '))
    47. #
    48. # elif defined ( DEBUG_PRINT_SHORT_FN_NAME )
    49. # define DEBUG_FUNCTION_NAME QString( " %1").arg( __FUNCTION__ +QString("()"), \
    50. -DEBUG_SHORT_FUNCTION_LENGTH, QLatin1Char(' '))
    51. #
    52. # elif defined ( DEBUG_NO_FUNCTION_NAME )
    53. # define DEBUG_FUNCTION_NAME ""
    54. # else
    55. # error No debug fuction flags defined
    56. # endif
    57. # else
    58. # define DEBUG_FUNCTION_NAME ""
    59. # endif
    60.  
    61. // Line numbers
    62. # if defined ( DEBUG_PRINT_LINENUMBER )
    63. # define DEBUG_LINENUMBER ( QString("[%1]")\
    64. .arg( QString::number(__LINE__), DEBUG_LINENUMBER_LENGTH, QLatin1Char(' ')) )
    65. # else
    66. # define DEBUG_LINENUMBER ""
    67. # endif
    68.  
    69. // Debug Macro
    70. # define DEBUG() qDebug() << QString( DEBUG_FILENAME + DEBUG_LINENUMBER + DEBUG_FUNCTION_NAME + " :" )
    71. #else
    72. # define DEBUG() if(true);else qDebug()
    73. #endif
    To copy to clipboard, switch view to plain text mode 

    How do I use it
    Qt Code:
    1. #include "Debug.h"
    2.  
    3. void foo(){
    4. DEBUG() << "Blah " << "Blah "
    5. }
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Macro used for debugging

    Any comments ??
    Or, Is it worth having this in Wiki ?
    We can't solve problems by using the same kind of thinking we used when we created them

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

    Default Re: Macro used for debugging

    Sorry, I'm a bit sleepy right now... but what's the point of this macro?

  4. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Macro used for debugging

    Well,

    The macro allow you to print a debug message which has the filename, line number and the function name. this macro can be enable/disabled using
    Qt Code:
    1. #define APP_NO_DEBUG_MESSAGES
    To copy to clipboard, switch view to plain text mode 
    You can disble/enable the Filename, Function name and the line number
    Both the function name and the filename can be short or more descriptive
    Qt Code:
    1. # define DEBUG_PRINT_FILEPATH
    2. # define DEBUG_PRINT_SHORT_FN_NAME
    3. # define DEBUG_PRINT_LINENUMBER
    To copy to clipboard, switch view to plain text mode 
    The output is aligned and the width of the field can be configured
    Qt Code:
    1. // file name
    2. # define DEBUG_FILEPATH_LENGTH 27
    3. # define DEBUG_FILENAME_LENGTH 22
    4. #
    5. // Function
    6. #
    7. # define DEBUG_LONG_FUNCTION_LENGTH 40
    8. #
    9. # define DEBUG_SHORT_FUNCTION_LENGTH 25
    10. #
    11. // LineNumber
    12. #
    13. # define DEBUG_LINENUMBER_LENGTH 4
    To copy to clipboard, switch view to plain text mode 
    so for line
    Qt Code:
    1. void foo(){
    2. DEBUG() << "I am here ";
    3. }
    4.  
    5. void foobar(){
    6. DEBUG() << "Yupppy ";
    7. }
    To copy to clipboard, switch view to plain text mode 
    the output would be

    Qt Code:
    1. src/filename.cpp [ 2] void foo() : "I am here "
    2. src/filename.cpp [ 6] void foobar() : "Yuppy"
    To copy to clipboard, switch view to plain text mode 
    Since the output is aligned, it is easier to comprehend
    We can't solve problems by using the same kind of thinking we used when we created them

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

    Default Re: Macro used for debugging

    C00l... although complicated Can't you make it work so that it is called by using qDebug() (so that one doesn't have to replace all qDebug() calls)?

  6. #6
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Macro used for debugging

    C00L.... although complicated
    Thanks
    Anyone can do that, replacing the DEBUG with qDebug in the #define DEBUG() should do I think . But I personally would not want to do that . Hmmm, Just doesn't look fine. what if I do not want the extra information for a some set of debug messages
    We can't solve problems by using the same kind of thinking we used when we created them

  7. #7
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Macro used for debugging

    the question still remains....

    Is it helpful to have this in our wiki ?
    We can't solve problems by using the same kind of thinking we used when we created them

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

    Default Re: Macro used for debugging

    Quote Originally Posted by sunil.thaha View Post
    Anyone can do that, replacing the DEBUG with qDebug in the #define DEBUG() should do I think .
    I don't think so, because you use qDebug() in your debug macro, so you can't substitute qDebug using a different macro. What I'd like to see is to substitute the qDebug macro to add information you add in your macro. Sorry if the explanation sounds complicated...

  9. #9
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Macro used for debugging

    How about having this on top
    Qt Code:
    1. #if defined( qDebug )
    2. # undef qDebug
    3. #endif
    To copy to clipboard, switch view to plain text mode 

    PFA driver file.

    So is it fine to have it in our wiki ?
    Attached Files Attached Files
    We can't solve problems by using the same kind of thinking we used when we created them

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

    Default Re: Macro used for debugging

    I think you don't understand... Your macro uses qDebug(), so you can't undef it or it won't work.

  11. #11
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Macro used for debugging

    Did you try out the driver file. it works for me ...
    We can't solve problems by using the same kind of thinking we used when we created them

  12. #12
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Macro used for debugging

    Since there has been three downloads ... Did anyone find it useful ?
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. Q_OBJECT macro issue
    By kandalf in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 19:28
  2. qDebug macro substitution
    By the_bis in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 08:31

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.