Results 1 to 10 of 10

Thread: Redirect qDebug statements to a file...?

  1. #1
    Join Date
    May 2015
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Redirect qDebug statements to a file...?

    Dear Expert,

    How to redirect all qDebug statements (output) to a file?, and also if (file) it reaches the fixed size (for example: 1MB), then it should open an another and write the contents into that file, instead of writing on to a stdout.

    I would really appreciate the quick response.. thanks in advance.

    Qt Code:
    1. void mymsghand(const char *msg) {
    2. QString txt;
    3.  
    4. txt = QString("%1").arg(msg);
    5. QFile outFile("outfile.log");
    6. outFile.open(QIODevice:WriteOnly | QIODevice:Append);
    7. QTextStream ts(&outFile);
    8. ts << txt << endl;
    9. }
    10.  
    11. int main(int argc, char *argv) {
    12. QApplication app(argc, argv);
    13. qInstallMsgHandler(mymsghand);
    14. ...
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    But here i really dont know how to check the file max size, as when it reaches the max size it should create another file and start write into the new file.

    Regards,
    Maiya

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: Redirect qDebug statements to a file...?

    See QFileInfo class. Once you create QFile in the handler, get QFileInfo, check size (QFileInfo::size()) and if it is big enough, start a new file.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Redirect qDebug statements to a file...?

    QFile::size()

    Cheers,
    _

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    Default Re: Redirect qDebug statements to a file...?

    If you are writing a lot of messages with qDebug, you may find that your constant opening and closing your log file can cause performance problems. i.e. Opening and closing files is a relatively expensive operation.

    Since the message handler doesn't accept a user or void pointer parameter that you can set or pass to it, you may want to consider a global variable for a QFile pointer. I typically avoid global variables at all costs, but I have found that doing the following can avoid having to repeatedly open/close your log file:


    1. Allocate global variable on heap for QFile log file and open it upon program initialization. You haven't set your message handler yet, so use qDebug() to report any errors related to allocating the global pointer or opening the log file and terminate, etc.
    2. Set your message handler using qInstallMsgHandler once your log file has been successfully opened
    3. Verify the global QFile pointer is not null before using the pointer in your message handler
    4. Optionally QFile::flush() your log file after writing new messages with your message handler
    5. Remove your message handler using qInstallMsgHandler(0) upon program termination
    6. Close the log file and delete the QFile pointer global variable you allocated in the first step, then set the global variable to nullptr, 0, or NULL, etc.


    If you're not writing a ton of messages, probably not a big deal, but I've used this technique in programs where I spew a ton of debug output, etc.

    If anyone has a technique to avoid a global variable and avoid having to open-append/write/close the log file inside of a message handler, I'd love to hear how others may have accomplished this.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Redirect qDebug statements to a file...?

    Be aware that the global variable needs some form of protection if you are using threads.

    Cheers,
    _

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    Default Re: Redirect qDebug statements to a file...?

    Quote Originally Posted by anda_skoa View Post
    Be aware that the global variable needs some form of protection if you are using threads.
    Yes, thanks for mentioning. Where I have resorted to using a global variable for a QFile pointer, I protected access via a QMutex via the QMutexLocker convenience class.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. #7
    Join Date
    May 2015
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: Redirect qDebug statements to a file...?

    Dear Experts,

    Kindly could you let me know, how to stop writing a qDebug statements to a file, as have written from the steps to write all qDebug statements to a file, but after some instant or by checking some condition it should stop writing to a file instead it should start write to a console now, any help would be appreciated. Thanks in advance.

    Regards,
    Maiya

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: Redirect qDebug statements to a file...?

    As explained in the previous posts, you can restore the original message handler by calling qInstallMsgHandler(0); (Qt 4) or qInstallMessageHandler(0); (Qt 5).

  9. #9
    Join Date
    May 2015
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: Redirect qDebug statements to a file...?

    Dear Experts,

    Thank you so much for your suggestions on the above queries.

    Now, I have generated a file to which redirect all qDebug statements to that. While doing any operations logs (all qDebug statements) will automatically start writing to a created file rather displaying it on the stdout. But now I wanted to write to another file if the first file size reaches the max size instead of writing to a old file it should stop write to old file and automatically create another file start write to it. I would really appreciate you help and suggestions. Thanks in advance.

    Regards,
    Maiya

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Redirect qDebug statements to a file...?

    You have already been given an approach to rotating logs: use QFileInfo or QFile::size() to look at the log after you write. If it is too big start a new file.

Similar Threads

  1. QDebug output to file
    By gpuckett in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2015, 17:18
  2. How to Redirect qDebug from QtGui Application to Console?
    By wangq0206 in forum Qt Programming
    Replies: 1
    Last Post: 21st August 2014, 09:39
  3. redirecting qDebug to file, threading question
    By jcox23 in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2010, 05:31
  4. Using qDebug to write to an external file
    By qtUser500 in forum Newbie
    Replies: 4
    Last Post: 18th November 2009, 14:01
  5. redirect qDebug() to file
    By sriky27 in forum Qt Programming
    Replies: 9
    Last Post: 17th March 2009, 20:21

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.