Results 1 to 10 of 10

Thread: redirect qDebug() to file

  1. #1
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default redirect qDebug() to file

    Hi,

    How to redirect the qDebug() output to a file instead of application output

    Regards,
    Srikanth

  2. #2
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: redirect qDebug() to file

    Simplest way without changing code:

    your_excutable > your_file
    or
    your_excutable >& your_file

  3. #3
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: redirect qDebug() to file

    Hi,

    Thank you for your reply but I would be installing the application on the device, is there any other way please suggest

    -Srikanth

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: redirect qDebug() to file

    Hi,

    Install a message handler using "qIntallMsgHandler" and there you can use "QTextStream" to write the message to a file.

    Also, think that "qDebug" only will work when the application is compiled as debug.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: redirect qDebug() to file

    try this
    Qt Code:
    1. #include <QApplication>
    2. #include <QtDebug>
    3. #include <QFile>
    4. #include <QTextStream>
    5.  
    6. void myMessageHandler(QtMsgType type, const char *msg)
    7. {
    8. QString txt;
    9. switch (type) {
    10. case QtDebugMsg:
    11. txt = QString("Debug: %1").arg(msg);
    12. break;
    13. case QtWarningMsg:
    14. txt = QString("Warning: %1").arg(msg);
    15. break;
    16. case QtCriticalMsg:
    17. txt = QString("Critical: %1").arg(msg);
    18. break;
    19. case QtFatalMsg:
    20. txt = QString("Fatal: %1").arg(msg);
    21. abort();
    22. }
    23. QFile outFile("log");
    24. outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    25. QTextStream ts(&outFile);
    26. ts << txt << endl;
    27. }
    28.  
    29. int main( int argc, char * argv[] )
    30. {
    31. QApplication app( argc, argv );
    32. qInstallMsgHandler(myMessageHandler);
    33. ...
    34. return app.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following 2 users say thank you to spirit for this useful post:

    GuusDavidson (6th July 2011), monst (28th April 2012)

  7. #6
    Join Date
    Mar 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: redirect qDebug() to file

    Thanks a lot for your suggestions I appreciate it

  8. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: redirect qDebug() to file

    Also it is recommended to "handle" fatal messages (using abort() or exit(-1) or something akin) because that's what the default handler does and if you don't do it you're likely to end up with segfaults instead (though not necessarily at the same place so you may have a hard time tracking the bugs...).
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: redirect qDebug() to file

    Quote Originally Posted by fullmetalcoder View Post
    Also it is recommended to "handle" fatal messages (using abort() or exit(-1) or something akin) because that's what the default handler does and if you don't do it you're likely to end up with segfaults instead (though not necessarily at the same place so you may have a hard time tracking the bugs...).
    Qt Code:
    1. ...
    2. case QtFatalMsg:
    3. txt = QString("Fatal: %1").arg(msg);
    4. abort();
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: redirect qDebug() to file

    Qt is using that handler in the function qt_message_output() and then it's checking if the message is QtFatalMsg and generating core dump or sth, so there is no need to implement it in addition to that. Whats more it's more complicated on Windows (with msvc) than just abort() in default implementation.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  11. #10
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: redirect qDebug() to file

    Quote Originally Posted by spirit View Post
    Qt Code:
    1. ...
    2. case QtFatalMsg:
    3. txt = QString("Fatal: %1").arg(msg);
    4. abort();
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Sorry missed that one line, I was in a bit of a rush when I read your post...
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 15:54
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

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.