PDA

View Full Version : Redirect qDebug statements to a file...?



prakashmaiya
13th July 2015, 16:15
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. :)



void mymsghand(const char *msg) {
QString txt;

txt = QString("%1").arg(msg);
QFile outFile("outfile.log");
outFile.open(QIODevice:WriteOnly | QIODevice:Append);
QTextStream ts(&outFile);
ts << txt << endl;
}

int main(int argc, char *argv) {
QApplication app(argc, argv);
qInstallMsgHandler(mymsghand);
...
return app.exec();
}


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

Radek
13th July 2015, 16:31
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.

anda_skoa
13th July 2015, 17:00
QFile::size()


Cheers,
_

jefftee
14th July 2015, 03:46
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:



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.
Set your message handler using qInstallMsgHandler once your log file has been successfully opened
Verify the global QFile pointer is not null before using the pointer in your message handler
Optionally QFile::flush() your log file after writing new messages with your message handler
Remove your message handler using qInstallMsgHandler(0) upon program termination
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.

anda_skoa
14th July 2015, 11:05
Be aware that the global variable needs some form of protection if you are using threads.

Cheers,
_

jefftee
15th July 2015, 01:32
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.

prakashmaiya
22nd July 2015, 10:06
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

yeye_olive
22nd July 2015, 11:16
As explained in the previous posts, you can restore the original message handler by calling qInstallMsgHandler(0); (Qt 4) or qInstallMessageHandler(0); (Qt 5).

prakashmaiya
23rd July 2015, 09:50
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

ChrisW67
23rd July 2015, 10:03
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.