随笔 - 70, 文章 - 0, 评论 - 9, 引用 - 0
数据加载中……

使用qInstallMsgHandler将日志保存到文件

1  在main函数之前定义回调函数

void myMessageOutput(QtMsgType type, const char *msg)
{
    QString text;
    
switch (type)
    
{
    
case QtDebugMsg:
        text 
= QString("Debug: %1").arg(msg);
        
break;
    
case QtWarningMsg:
        text 
= QString("Warning: %1").arg(msg);
        
break;
    
case QtCriticalMsg:
        text 
= QString("Critical: %1").arg(msg);
        
break;
    
case QtFatalMsg:
        text 
= QString("Fatal: %1").arg(msg);
        abort();
    }

    QFile file(
"111.txt");
    file.open(QIODevice::WriteOnly 
| QIODevice::Append);
    QTextStream ts(
&file);
    ts
<<text<<endl;
}


2  在main函数中注册myMessageOutput

qInstallMsgHandler(myMessageOutput);


3  注册后,在该程序的任何地方使用即可。

qDebug("This is a debug message");
qWarning(
"This is a warning message");
qCritical(
"This is a critical message");
qFatal(
"This is a fatal message");

posted on 2011-03-22 17:25 seahouse 阅读(2158) 评论(0)  编辑 收藏 引用 所属分类: Qt


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理