Results 1 to 5 of 5

Thread: redirecting qDebug to file, threading question

  1. #1
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default redirecting qDebug to file, threading question

    Hello,

    I installed in my application a custom Message Handler with qInstallMessageHandler
    to control the output of qDebug, qWarning and the likes...

    For simplicity, let's say that I redirect them into a file...

    What's unclear for me now, do I have to take usual precautions regarding threading (locking the file i'm writing on in that case I guess) given that qDebugs are called from the main thread but also from other threads resulting in possibly attempt to write the same file from several places...

    Thanks for any answer!

  2. #2
    Join Date
    Mar 2007
    Posts
    57
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: redirecting qDebug to file, threading question

    I don't know whether qDebug is thread safe. However, I do know that you can instruct it to write to a file without using qInstallMessageHandler. See docs for QDebug.

  3. #3
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: redirecting qDebug to file, threading question

    Quote Originally Posted by TMan View Post
    I don't know whether qDebug is thread safe. However, I do know that you can instruct it to write to a file without using qInstallMessageHandler. See docs for QDebug.
    this part I got:
    here's an excerpt:
    Qt Code:
    1. #ifndef QT_NO_DEBUG_OUTPUT
    2. QTextStream *out = 0;
    3. QMutex mutex;
    4.  
    5. static void logOutput(QtMsgType type, const char *msg)
    6. {
    7. QMutexLocker locker(&mutex);
    8.  
    9. QString debugdate = QDateTime::currentDateTime().toString("yyyy.MM.dd|hh:mm:ss");
    10. QString message(msg);
    11.  
    12. // extract the priority of the message
    13. int priority = message.left(1).toInt();
    14. if (priority >= 0) message.remove(0, 1);
    15.  
    16. switch (type)
    17. {
    18. case QtDebugMsg:
    19. debugdate += ":";
    20. (*out) << debugdate << " " << message << endl;
    21. break;
    22. case QtWarningMsg:
    23. debugdate += ":";
    24. (*out) << debugdate << " " << msg << endl;
    25. break;
    26. case QtCriticalMsg:
    27. debugdate += ":";
    28. (*out) << debugdate << " " << msg << endl;
    29. break;
    30. case QtFatalMsg:
    31. debugdate += "[F]";
    32. (*out) << debugdate << " " << msg << endl;
    33. }
    34.  
    35. if (QtFatalMsg == type)
    36. {
    37. abort();
    38. }
    39. }
    40. #endif
    To copy to clipboard, switch view to plain text mode 

    but do I need to protect the write to the file?

  4. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: redirecting qDebug to file, threading question

    I have recently taken the same approach and I wrote code almost entirely identical to yours. I hadn't considered a threading problem though. I'm not explicitly creating any new threads, so I'm not sure that I do need to worry about it.

    One thing I was considering was having the logging to file happening in a different thread. The custom MessageHandler could emit a signal that could be handled by a class in another thread. This might solve your (potential) problem as I believe that the signal/slot mechanism is thread safe, but don't quote me on that.

    Oh, wait, do quote me on that...

    Qt provides thread support in the form of platform-independent threading classes, a thread-safe way of posting events, and signal-slot connections across threads

    This approach also has the advantage of moving expensive file IO into another thread, which might have a positive performance impact.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: redirecting qDebug to file, threading question

    To answer the question if qdebug is threadsafe:
    QDebug uses a QTextstream. A QTextStream is not threadsafe.
    The documentation is not clear about this, but if you look at the source code of qdebug or qtextstream you see there's no mutex locking at all in the code.

Similar Threads

  1. Replies: 26
    Last Post: 23rd April 2011, 10:44
  2. Qt threading question..
    By tgreaves in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2009, 12:00
  3. Question about threading basics
    By donglebob in forum Qt Programming
    Replies: 8
    Last Post: 29th August 2008, 18:40
  4. Newbie threading question
    By deepayan in forum Qt Programming
    Replies: 17
    Last Post: 16th April 2007, 00:25
  5. Redirecting qDebug()
    By hardgeus in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2006, 01:39

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.