Results 1 to 4 of 4

Thread: Global debugging function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Global debugging function

    Maybe this functionality already exists in the Qt docs and I haven't come across it yet, so I'm open to suggestions.

    Basically what I'm trying to do is create a one-line, generic, global error reporting mechanism for my multi-file program. When it's compiled in debug mode, the function outputs to qDebug() and I read it from the application output in Qt Creator. However, if it's compiled in release mode, I want the function to output to a file instead of the command prompt.

    Here's my (buggy) implementation:
    Qt Code:
    1. //myerror.h:
    2. #ifndef MYERROR_H
    3. #define MYERROR_H
    4.  
    5. #ifdef DEBUGMODE //DEBUGMODE is defined in the .pro file by "Debug:DEFINES += DEBUGMODE"
    6. #include <QDebug>
    7. #else
    8. #include <QFile>
    9. QFile ERRLOG("c:/temp/my_errors.log");
    10. #endif
    11.  
    12. inline void QDEBUG(QString a)
    13. {
    14. #ifdef DEBUGMODE
    15. qDebug() << a;
    16. #else
    17. ERRLOG.write(QString("%1\n").arg(a).toAscii());
    18. #endif
    19. }
    20.  
    21. #endif // MYERROR_H
    22.  
    23. //mainwindow.cpp:
    24. MainWindow::MainWindow(QWidget *parent) :
    25. QMainWindow(parent),
    26. ui(new Ui::MainWindow)
    27. {
    28. #ifndef DEBUGMODE
    29. ERRLOG.open(QIODevice::WriteOnly);
    30. #endif
    31.  
    32. ui->setupUi(this);
    33. //etc
    34. }
    35. MainWindow::~MainWindow()
    36. {
    37. #ifndef DEBUGMODE
    38. ERRLOG.close();
    39. #endif
    40. delete ui;
    41. }
    To copy to clipboard, switch view to plain text mode 

    Now, this works while I'm in debug mode, but doesn't work when in release mode. The file "my_errors.log" is being generated, but as far as I can tell it was never opened or written to. I can't just open the QFile in myerror.h because it's a header, so I have to do it in mainwindow.cpp.

    Additionally, including this header in more than one cpp file resulted in a linker error complaining about multiple definitions, even though I've guarded myerror.h!

    Is there a better way to do this? Can I just re-route qDebug() to a file? I couldn't find anything on that in the documentation.

    Thanks!
    Last edited by Phlucious; 8th December 2011 at 00:04.

Similar Threads

  1. Connect to global function?
    By hakermania in forum Newbie
    Replies: 2
    Last Post: 3rd September 2011, 16:36
  2. QtConcurrentRun and global function
    By hakermania in forum Newbie
    Replies: 2
    Last Post: 16th July 2011, 10:05
  3. Replies: 3
    Last Post: 6th September 2010, 23:00
  4. Replies: 3
    Last Post: 25th May 2010, 09:46
  5. Replies: 0
    Last Post: 10th March 2010, 08:13

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
  •  
Qt is a trademark of The Qt Company.