QT qDebug() 打印到文件里

匿名 (未验证) 提交于 2019-12-03 00:03:02

 #include <QMutex> #include <QMessageLogContext> #include <QtMessageHandler>  void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {     // 加锁     static QMutex mutex;     mutex.lock();      QByteArray localMsg = msg.toLocal8Bit();      QString strMsg("");     switch(type)     {     case QtDebugMsg:         //调试信息         strMsg = QString("Debug:");         break;     case QtWarningMsg:         //警告信息         strMsg = QString("Warning:");         break;     case QtCriticalMsg:         //严重错误         strMsg = QString("Critical:");         break;     case QtFatalMsg:         //致命错误         strMsg = QString("Fatal:");         break;     }      // 设置输出信息格式     QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");     QString strMessage = QString("DateTime:%1 Message:%2 File:%3  "                                  "Line:%4  Function:%5 ")             .arg(strDateTime).arg(localMsg.constData()).arg(context.file)             .arg(context.line).arg(context.function);      // 输出信息至文件中(读写、追加形式)      QString  MyLogFilePath = “D:\” ;     MyLogFilePath += "log.txt";     QFile file(MyLogFilePath);     file.open(QIODevice::ReadWrite | QIODevice::Append);     QTextStream stream(&file);     stream << strMessage << "\r\n";     file.flush();     file.close();      // 解锁     mutex.unlock(); }   int main(int argc, char *argv[]) {     // 安装消息处理程序     qInstallMessageHandler(myMessageOutput);      QApplication a(argc, argv);     a.setStyle("plastique");         Widget w;     w.show();      return a.exec(); }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!