PDA

View Full Version : Detecting errors with QTextStream and QFile



December
5th August 2008, 17:41
Hi All,

I'm using QFile and QTextStream to log data from a server application.

My problem is I want to be able to rotate out the log files using an external program.

When the log file gets removed, how can I detect that I need to re-open it from the app?

Calling fileExists() with every write to the log seems horribly time consuming.

file->error() doesn't return an error in the above case, neither does textstream->status(). The log file is not there, but no new one is created, and the data supposedly being written is just lost!

Any ideas?

Thanks.

caduel
5th August 2008, 18:11
This is probably OS specific.
On linux, if you have a handle to an open file, and that file is removed somehow... the OS does not really remove the file until all open file handles are closed.
So your app can indead continue happily to use the file. It still does exist as the app's handle keeps it alive. New commands like "ls" will not show the file, as they do not have an open handle...

If you want to make sure the that a file has not been "removed" (from the directory), you have to either check (e.g.) with QFile::exists() or you might wish to watch it with QFileSystemWatcher.

The more efficient way would be to use QFilesystemWatcher.
HTH