I started using Qt5 few days ago. I needed a logger for my app and I decided to use qDebug, but it seems it has to be "redirected" in order to have the logs in a file.
I used qInstallMessageHandler to do that and I wrote my own handler as presented below (inspired from other people here).
It seems it works, but as I am not a guru, I have to ask:
Is it ok to use this in a multi-thread application or not?
Also, if it is ok/safe for using in a multi-thread app, can it be improved somehow?
void MessageHandler(QtMsgType type, const QMessageLogContext & context, const QString & msg)
{
mutex.lock();
QDateTime dateTime(QDateTime::currentDateTime());
QString timeStr(dateTime.toString("dd-MM-yyyy HH:mm:ss:zzz"));
QString contextString(QString("(%1, %2)").arg(context.file).arg(context.line));
QFile outFile("file.log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream stream(&outFile);
stream << timeStr << " " << contextString << ": " << msg << endl;
mutex.unlock();
}
解决方案
Your approach looks simple and clean. I'd keep it like that.
There is one thing you can improve: Open the file only once at application start and close it when you close the application. Opening a file is an expensive operation.
You can write into the same open file from multiple threads, since your mutex ensures that only one thread writes at the same time.