Results 1 to 2 of 2

Thread: A little debugging helper class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default A little debugging helper class

    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
    Qt Code:
    1. #ifndef SCOPE_H
    2. #define SCOPE_H
    3.  
    4. #include<QString>
    5. #include"windows.h"
    6.  
    7. #define FUNC Logger log(__FUNCTION__)
    8. #define CONSOLE Signature(Q_FUNC_INFO)
    9.  
    10. // Use macro LOG in the function(s) you want to trace
    11. #define LOG FUNC;CONSOLE;
    12.  
    13. // To keep your comments indented.
    14. #define INDENT Indent()
    15.  
    16. class Logger {
    17.  
    18. public:
    19. enum COLORS {
    20. BLACK = 0,
    21. BLUE = FOREGROUND_BLUE,
    22. GREEN = FOREGROUND_GREEN,
    23. CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE,
    24. RED = FOREGROUND_RED,
    25. MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE,
    26. BROWN = FOREGROUND_RED | FOREGROUND_GREEN,
    27. LIGHTGRAY = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
    28. DARKGRAY = FOREGROUND_INTENSITY,
    29. LIGHTBLUE = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
    30. LIGHTGREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
    31. LIGHTCYAN = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
    32. LIGHTRED = FOREGROUND_RED | FOREGROUND_INTENSITY,
    33. LIGHTMAGENTA = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
    34. YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
    35. WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
    36. };
    37. Logger (const QString &str);
    38. ~Logger ();
    39. unsigned getIndent(){return m_indent;}
    40. friend void Color(WORD Attributes);
    41. friend void Signature(const QString & fct);
    42. friend void Indent( );
    43. private:
    44. static unsigned m_indent;
    45. const QString m_text;
    46.  
    47. };
    48.  
    49. extern unsigned idx;
    50.  
    51. #endif //SCOPE_H
    To copy to clipboard, switch view to plain text mode 

    scope.cpp
    Qt Code:
    1. #include "logger.h"
    2.  
    3.  
    4. //void Color(WORD Attributes);
    5. unsigned Logger::m_indent = 0;
    6. unsigned idx = 0;
    7.  
    8.  
    9.  
    10.  
    11. Logger::Logger (const QString &str)
    12. : m_text(str)
    13. {
    14. Color(LIGHTBLUE );
    15. QString text = QString( m_indent++ <<1, ' ') + "[+] BEGIN " + m_text;
    16. fprintf(stderr, "%s\n", qPrintable(text));
    17. idx = getIndent();
    18. Color(WHITE );
    19. }
    20.  
    21. Logger::~Logger ()
    22. {
    23. Color(LIGHTBLUE);
    24. QString text = QString( --m_indent <<1, ' ')+ "[-] END " + m_text ;
    25. fprintf(stderr, "%s\n\n", qPrintable(text));
    26. idx = getIndent();
    27. Color(WHITE );
    28. }
    29.  
    30.  
    31. inline void Color(WORD Attributes)
    32. {
    33. HANDLE H=GetStdHandle(STD_OUTPUT_HANDLE);
    34. SetConsoleTextAttribute(H,Attributes);
    35. }
    36.  
    37. void Signature(const QString & fct)
    38. {
    39. Color(Logger::CYAN);
    40. QString text = QString( idx <<1, ' ')+ fct + " was called";
    41. fprintf(stderr, "%s\n", qPrintable(text));
    42. Color(Logger::WHITE);
    43. }
    44.  
    45. // To keep your comments indented through each function call ( using qDebug() )
    46. // USAGE: INDENT; qDebug() << "A comment"<< aValue;
    47. void Indent()
    48. {
    49. QString text =QString( idx<<1, ' ');
    50. fprintf(stderr, "%s", qPrintable(text));
    51. }
    To copy to clipboard, switch view to plain text mode 

    Hope you find it useful.

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A little debugging helper class

    Just to show you how to use it:

    Qt Code:
    1. #include "scope.h"
    2.  
    3. int main (int argc, char ** argv)
    4. {
    5. LOG
    6. if ( ! createConnection()) return -1;
    7. QApplication app(argc, argv);
    8. app.setStyle(new CustomStyle);
    9.  
    10. INDENT; qDebug()<<"This is a comment, we are in Main";
    11.  
    12. InfoBox mainVindow;
    13. mainVindow.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. Qt Creator upgraded to 2.0. Now repeatedly asked about Debugging Helper
    By winkle99 in forum Qt Tools
    Replies: 0
    Last Post: 25th June 2010, 23:16
  2. Debugger helper
    By JohnToddSr in forum Qt Tools
    Replies: 33
    Last Post: 15th June 2010, 23:19
  3. debugging "signal does not reach slot" in a template class
    By Daniel Dekkers in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2010, 16:03
  4. Debugging helper library
    By codeman in forum Qt Tools
    Replies: 0
    Last Post: 22nd March 2010, 13:03

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.