int main(int argc, char** argv)
{
QApplication a(argc, argv);
qInstallMessageHandler([](QtMsgType type, const QMessageLogContext& context, const QString& str)
{
QString infoType;
if (type == QtDebugMsg)
infoType = "Debug";
else if (type == QtInfoMsg)
infoType = "Info";
else if (type == QtCriticalMsg)
infoType = "Critical";
else if (type == QtWarningMsg)
infoType = "Warning";
QString fileinfo = context.file;
QString dataTime = QDateTime::currentDateTime().toString("yy-mm-dd-hh-mm-ss");
QFile file("./log.txt");
QMutex mutex;
mutex.lock();
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
{
QString msg = QString("%1 %2 %3 %4").arg(infoType).arg(dataTime).arg(fileinfo).arg(str);
file.write(msg.toUtf8());
}
file.flush();
file.close();
mutex.unlock();
});
qDebug() << "this is a debug\r\n";
qInfo() << "this is a info\r\n";
qCritical() << "this is a critical\r\n";
qWarning() << "this is a warning\r\n";
return a.exec();
}