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.