Hi all,
I use Qt 4.8.2 on WindowsXP, VS2010.
It seems I found a bug in QFile during appending text. Long time ago I wrote a simple "Trace" class for writing data (text, xml) based on Qt 4.7.8. Never a problem occured.
But now with Qt 4.8.2 the content of the file is confused. My "Trace" class normally writes line by line with a timestamp at beginning and appends a new line at the end. But sometimes whole lines missing or older lines at beginning of the file are overwritten.
A simple example is to write a "socket connection state" into the file. Inside the slots of connected/disconnected/errorOccured I write a line to the trace.
Here is a code snipped of my Trace::write() method, the member m_file is a QFile:
void Trace
::write(const QString text
) {
if (!m_enabled)
return;
QString filename
= getFilenameToday
();
if (filename.isEmpty())
return;
QString timestamp
= QDateTime::currentDateTime().
toString("yyyy-MM-dd hh:mm:ss zzz");
if (!QFile::exists(filename
)) {
str
= QString("************************** Startup %1 **************************\n") .
arg(QDateTime::currentDateTime().
toString("yyyy-MM-dd hh:mm:ss"));
}
str += "\n" + timestamp + ": " + text;
str.replace("\r\n", "\n");
m_file.setFileName(filename);
{
m_file.write(str.toLatin1());
m_file.flush();
m_file.close();
}
deleteOldFiles();
}
void Trace::write(const QString text)
{
if (!m_enabled)
return;
QString filename = getFilenameToday();
if (filename.isEmpty())
return;
QString str;
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz");
if (!QFile::exists(filename))
{
str = QString("************************** Startup %1 **************************\n")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
str += "\n" + timestamp + ": " + text;
str.replace("\r\n", "\n");
m_file.setFileName(filename);
if (m_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered))
{
m_file.write(str.toLatin1());
m_file.flush();
m_file.close();
}
deleteOldFiles();
}
To copy to clipboard, switch view to plain text mode
I now often opening and closing a file is time consuming but this allows to me to open/copy/delete the file during my application is running.
Have anybody an idea what's going wrong? I looked into the changes of 4.8.1 and 4.8.2 and there were fixes of QFile and QIODevice. Maybe they fixed one bug and added a new one ;-)
Thanks a lot...
Bookmarks