如何重定向qDebug,qWarning,qCritical等输出?

我在qDebug() <<调试输出中使用了很多语句。有什么跨平台的方法可以将调试输出重定向到文件,而无需使用Shell脚本?我猜想open()和dup2()将在Linux中完成这项工作,但可以在Windows中与MinGW一起编译吗?


也许有Qt方法可以做到?


红糖糍粑
浏览 898回答 3
3回答

人到中年有点甜

您必须使用qInstallMsgHandler函数安装消息处理程序,然后才能QTextStream将调试消息写入文件中。这是一个示例示例:#include <QtGlobal>#include <stdio.h>#include <stdlib.h>void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg){&nbsp; &nbsp; QByteArray localMsg = msg.toLocal8Bit();&nbsp; &nbsp; switch (type) {&nbsp; &nbsp; case QtDebugMsg:&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case QtInfoMsg:&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case QtWarningMsg:&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case QtCriticalMsg:&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case QtFatalMsg:&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);&nbsp; &nbsp; &nbsp; &nbsp; abort();&nbsp; &nbsp; }}int main(int argc, char **argv){&nbsp; &nbsp; qInstallMessageHandler(myMessageOutput); // Install the handler&nbsp; &nbsp; QApplication app(argc, argv);&nbsp; &nbsp; ...&nbsp; &nbsp; return app.exec();}摘自qInstallMsgHandler(我仅添加了评论)的文档:QtMsgHandler qInstallMessageHandler(QtMsgHandler处理程序)在上面的示例中,该函数myMessageOutput使用stderr了您可能希望用其他文件流替换的函数,或者完全重写了该函数!一旦你写并安装此功能,您所有的qDebug(以及qWarning,qCritical等)消息将被重定向到你在处理程序写入文件。

慕田峪9158850

从这里开始,所有的荣誉都归于精神。#include <QApplication>#include <QtDebug>#include <QFile>#include <QTextStream>void myMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & msg){&nbsp; &nbsp; QString txt;&nbsp; &nbsp; switch (type) {&nbsp; &nbsp; case QtDebugMsg:&nbsp; &nbsp; &nbsp; &nbsp; txt = QString("Debug: %1").arg(msg);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case QtWarningMsg:&nbsp; &nbsp; &nbsp; &nbsp; txt = QString("Warning: %1").arg(msg);&nbsp; &nbsp; break;&nbsp; &nbsp; case QtCriticalMsg:&nbsp; &nbsp; &nbsp; &nbsp; txt = QString("Critical: %1").arg(msg);&nbsp; &nbsp; break;&nbsp; &nbsp; case QtFatalMsg:&nbsp; &nbsp; &nbsp; &nbsp; txt = QString("Fatal: %1").arg(msg);&nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; QFile outFile("log");&nbsp; &nbsp; outFile.open(QIODevice::WriteOnly | QIODevice::Append);&nbsp; &nbsp; QTextStream ts(&outFile);&nbsp; &nbsp; ts << txt << endl;}int main( int argc, char * argv[] ){&nbsp; &nbsp; QApplication app( argc, argv );&nbsp; &nbsp; qInstallMessageHandler(myMessageHandler);&nbsp; &nbsp;&nbsp; &nbsp; ...&nbsp; &nbsp; return app.exec();}

四季花海

这是一个挂钩默认消息处理程序的工作示例。谢谢@Ross Rogers!// -- main.cpp// Get the default Qt message handler.static const QtMessageHandler QT_DEFAULT_MESSAGE_HANDLER = qInstallMessageHandler(0);void myCustomMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg){&nbsp; &nbsp; // Handle the messages!&nbsp; &nbsp; // Call the default handler.&nbsp; &nbsp; (*QT_DEFAULT_MESSAGE_HANDLER)(type, context, msg);}int main(int argc, char *argv[]){&nbsp; &nbsp; qInstallMessageHandler(myCustomMessageHandler);&nbsp; &nbsp; QApplication a(argc, argv);&nbsp; &nbsp; qDebug() << "Wello Horld!";&nbsp; &nbsp; return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP