如何redirectqDebug,qWarning,qCritical等输出?

我正在使用大量的qDebug() <<语句来debugging输出。 有没有任何跨平台的方式,我可以redirect到debugging输出到一个文件,而不诉诸壳shell脚本? 我猜测open()和dup2()会在Linux中完成这个工作,但是它能在Windows下用MinGW编译吗?

也许有一个Qt的方式来做到这一点?

您必须使用qInstallMsgHandler函数来安装消息处理程序,然后可以使用QTextStreamdebugging消息写入文件。 这里是一个示例:

 #include <QtGlobal> #include <stdio.h> #include <stdlib.h> void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); switch (type) { case QtDebugMsg: fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtInfoMsg: fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtWarningMsg: fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtCriticalMsg: fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtFatalMsg: fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); abort(); } } int main(int argc, char **argv) { qInstallMessageHandler(myMessageOutput); // Install the handler QApplication app(argc, argv); ... return app.exec(); } 

取自qInstallMsgHandler的文档(我只添加了注释):

  • QtMsgHandler qInstallMessageHandler(QtMsgHandler处理程序)

在上面的例子中,函数myMessageOutput使用stderr ,你可能想用其他文件stream来replace,或者完全重写函数!

一旦你编写并安装了这个函数,你所有的qDebug (以及qWarningqCritical等等)消息都将被redirect到你正在处理的文件中。

从这里所有的功劳都归功于精神 。

 #include <QApplication> #include <QtDebug> #include <QFile> #include <QTextStream> void myMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & msg) { QString txt; switch (type) { case QtDebugMsg: txt = QString("Debug: %1").arg(msg); break; case QtWarningMsg: txt = QString("Warning: %1").arg(msg); break; case QtCriticalMsg: txt = QString("Critical: %1").arg(msg); break; case QtFatalMsg: txt = QString("Fatal: %1").arg(msg); break; } QFile outFile("log"); outFile.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream ts(&outFile); ts << txt << endl; } int main( int argc, char * argv[] ) { QApplication app( argc, argv ); qInstallMessageHandler(myMessageHandler); ... return app.exec(); } 

那么,我会说,当你需要将你的debugging输出redirect到与stderr不同的东西的时候,你可以考虑一些日志logging工具。 如果你觉得你需要一个,我build议使用QxtLogger“QxtLogger类是一个易于使用,易于扩展日志logging工具” )。

这是一个挂钩默认消息处理程序的工作示例。

谢谢@罗斯·罗杰斯!

 // -- 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) { // Handle the messages! // Call the default handler. (*QT_DEFAULT_MESSAGE_HANDLER)(type, context, msg); } int main(int argc, char *argv[]) { qInstallMessageHandler(myCustomMessageHandler); QApplication a(argc, argv); qDebug() << "Wello Horld!"; return 0; } 

如果应用程序是从Qt Creator运行到debug.log文件的,那么当它被编译并作为独立应用程序运行时,这是一个跨平台的解决scheme,用于login到控制台。

main.cpp

 #include <QApplication> #include <QtGlobal> #include <QtDebug> #include <QTextStream> #include <QTextCodec> #include <QLocale> #include <QTime> #include <QFile> const QString logFilePath = "debug.log"; bool logToFile = false; void customMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QHash<QtMsgType, QString> msgLevelHash({{QtDebugMsg, "Debug"}, {QtInfoMsg, "Info"}, {QtWarningMsg, "Warning"}, {QtCriticalMsg, "Critical"}, {QtFatalMsg, "Fatal"}}); QByteArray localMsg = msg.toLocal8Bit(); QTime time = QTime::currentTime(); QString formattedTime = time.toString("hh:mm:ss.zzz"); QByteArray formattedTimeMsg = formattedTime.toLocal8Bit(); QString logLevelName = msgLevelHash[type]; QByteArray logLevelMsg = logLevelName.toLocal8Bit(); if (logToFile) { QString txt = QString("%1 %2: %3 (%4)").arg(formattedTime, logLevelName, msg, context.file); QFile outFile(logFilePath); outFile.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream ts(&outFile); ts << txt << endl; outFile.close(); } else { fprintf(stderr, "%s %s: %s (%s:%u, %s)\n", formattedTimeMsg.constData(), logLevelMsg.constData(), localMsg.constData(), context.file, context.line, context.function); fflush(stderr); } if (type == QtFatalMsg) abort(); } int main(int argc, char *argv[]) { QByteArray envVar = qgetenv("QTDIR"); // check if the app is ran in Qt Creator if (envVar.isEmpty()) logToFile = true; qInstallMessageHandler(customMessageOutput); // custom message handler for debugging QApplication a(argc, argv); // ...and the rest of 'main' follows 

日志格式化由QString("%1 %2: %3 (%4)").arg... (用于文件)和fprintf(stderr, "%s %s: %s (%s:%u, %s)\n"... (用于控制台)。

启示: https : //gist.github.com/polovik/10714049 。