#include #include #include #include #include #include #include #include using namespace std;void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg){ static QMutex mutex; mutex.lock(); QString text; bool flag = false; switch(type) { case QtDebugMsg://qDebug flag = true; break; case QtInfoMsg://qInfo text = QString(":"); break; case QtWarningMsg://qWarning text = QString(":"); break; case QtCriticalMsg://qCritical text = QString(":"); break; case QtFatalMsg://qFatal text = QString(":"); abort(); } if (flag) {#ifdef QT_NO_DEBUG cout << msg.toStdString() << endl;#else cout << msg.toStdString() << "," << context.file << "," << context.line << "," << context.function << endl;#endif } else { QString message = QString("[%1] %2 %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd")).arg(text).arg(msg); QFile file(QString("log.txt")); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream text_stream(&file); text_stream << message << endl; file.flush(); file.close(); } mutex.unlock();}int main(int argc, char **argv){ qInstallMessageHandler(myMessageOutput); QCoreApplication app(argc, argv); qInfo() << "122"; qInfo() << "12342"; qInfo() << "1243222"; qInfo() << "12322"; qDebug() << "aabc"; qDebug() << "abadrc"; qDebug() << "abzac"; qDebug() << "abaqc"; qDebug() << "abac"; return app.exec();}
QT5 写日志文件
最新推荐文章于 2024-07-22 18:46:00 发布