Qt工作笔记-自定义打印及存日志及stderr转stdout(Linux程序调试技巧,提高开发效率)
如下的代碼:
#include <QCoreApplication> #include <QDebug> #include <QByteArray> #include <iostream> #include <stdio.h> #include <QDebug> using namespace std;int main(int argc, char *argv[]) {cout << "The std cout output msg" << endl;qDebug() << "The qDebug output msg";qInfo() << "The qInfo output msg";qWarning() << "The qWarning output msg";qCritical() << "The qCritical output msg";qFatal("The qFatal output msg");return 0; }在Linux中程序有2個關鍵,一個是標準輸出,一個是標準錯誤,在C語言中,有stderr和stdout。
?
程序運行截圖如下:
如果把標準輸出重定向到1.txt文件中:那么屏幕就只會輸出stderr
然后把標準錯誤放到標準輸出上這樣屏幕就不會打印東西了:
所有的數(shù)據(jù)都將在1.txt中:
下面來說下這個的作用,本人在搞開發(fā)的時候,經(jīng)常改別人的程序,或者在xx程序中進行二次開發(fā),這樣的話就有個問題,大量的打印。
當別人程序里面只有cout作為輸出的時候,
自己可以使用qDebug()進行打印,然后把標準輸出重定向,這樣標準錯誤就會打印到屏幕上了。
?
下面問題來了,當那個項目里面又有人cout、qDebug這樣改怎么搞,下面提供3個解決思路。
第一個,將qDebug改為stdout,自己使用qInfo或其他幾個進行輸出。然后就和上面一樣了,代碼如下:
#include <QCoreApplication> #include <QDebug> #include <QByteArray> #include <iostream> #include <stdio.h> #include <QDebug> using namespace std;void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg){QByteArray localMsg = msg.toLocal8Bit();switch(type){case QtDebugMsg:fprintf(stdout, "qDebug: %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(messageOutput);cout << "The std cout output msg" << endl;qDebug() << "The qDebug output msg";qInfo() << "The qInfo output msg";qWarning() << "The qWarning output msg";qCritical() << "The qCritical output msg";qFatal("The qFatal output msg");return 0; }程序運行截圖如下:
這種方式是不是很有用
?
第二種就不演示了,就提一下。把QtDebugMsg中不提供打印,然后自己用其他幾個輸出。
?
第三個:記錄到文件中,這里可以加個當前時間,這樣就和日志一樣了,比如自己用Warning記錄到文件里面:
程序源碼如下:
#include <QCoreApplication> #include <QDebug> #include <QByteArray> #include <iostream> #include <QDateTime> #include <QTextStream> #include <stdio.h> #include <QDebug> #include <QFile> using namespace std;void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg){QByteArray localMsg = msg.toLocal8Bit();switch(type){case QtDebugMsg:fprintf(stdout, "qDebug: %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:{QFile file("myOut.txt");if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){break;}QTextStream out(&file);out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") <<localMsg << "\n";file.close();}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(messageOutput);cout << "The std cout output msg" << endl;qDebug() << "The qDebug output msg";qInfo() << "The qInfo output msg";qWarning() << "The qWarning output msg";qCritical() << "The qCritical output msg";qFatal("The qFatal output msg");return 0; }程序運行截圖如下:
相應的文件:
是不是改別人代碼的時候好調(diào)試多了。
總結
以上是生活随笔為你收集整理的Qt工作笔记-自定义打印及存日志及stderr转stdout(Linux程序调试技巧,提高开发效率)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++笔记-二维棋盘数组使用BFS(宽度
- 下一篇: Java笔记-Linux环境中因编码问题