博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt工作笔记-自定义打印及存日志及stderr转stdout(Linux程序调试技巧,提高开发效率)
阅读量:1950 次
发布时间:2019-04-27

本文共 3741 字,大约阅读时间需要 12 分钟。

如下的代码:

#include 
#include
#include
#include
#include
#include
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

然后把标准错误放到标准输出上这样屏幕就不会打印东西了:

所有的数据都将在1.txt中:

下面来说下这个的作用,本人在搞开发的时候,经常改别人的程序,或者在xx程序中进行二次开发,这样的话就有个问题,大量的打印。

当别人程序里面只有cout作为输出的时候,

自己可以使用qDebug()进行打印,然后把标准输出重定向,这样标准错误就会打印到屏幕上了。

 

下面问题来了,当那个项目里面又有人cout、qDebug这样改怎么搞,下面提供3个解决思路。

第一个,将qDebug改为stdout,自己使用qInfo或其他几个进行输出。然后就和上面一样了,代码如下:

#include 
#include
#include
#include
#include
#include
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 
#include
#include
#include
#include
#include
#include
#include
#include
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") <
<< "\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;}

程序运行截图如下:

相应的文件:

是不是改别人代码的时候好调试多了。

转载地址:http://cnmif.baihongyu.com/

你可能感兴趣的文章
JSON的构建
查看>>
json-lib——JsonConfig详细使用说明
查看>>
JSONConfig处理日期
查看>>
Gson处理json数据,转换javaBean的时候,替换输出字段名,解析日期的坑
查看>>
给json对象添加属性
查看>>
中国移动DNS大全
查看>>
ORA-04031: 无法分配 4064 字节的共享内存
查看>>
mysql 单节点数据库压力测试
查看>>
显示数据库的所有存储过程(mysql)
查看>>
javascript try catch示例
查看>>
不同浏览器onmousedown事件的处理
查看>>
oracle 查询表结构并导出EXCEL
查看>>
Javascript实现鼠标替换图片的简单方法
查看>>
iframe 迫使页面总是单独显示
查看>>
iframe的使用
查看>>
window.open的使用
查看>>
select下拉选中,页面跳转
查看>>
级联下拉选择月份显示总天数
查看>>
表单校验原理
查看>>
对姓名进行首字母大写处理
查看>>