Qt4/Qt5 qDebug输出到文件

来源:互联网 发布:apache modwsgi 编辑:程序博客网 时间:2024/05/19 17:10

在Qt中qDebug()可以可以打印出十分细致的log,用过你就会喜欢。现在要将qDebug() 等重定向到文件中。

但是Qt4.xxx版本(Qt5以下)和Qt5以上版本中qDebug()重定向是有区别的。

      一、在Qt4.xxx版本(Qt5以下)

    使用使用Qt的MessageHandler可以将消息重定向。在Qt4.x时代,安装一个消息处理器是用qInstallMsgHandler() 函数来安装这个函数接受的一个函数指针作为参数,函数的定义是

void FunctionName(QtMsgType,constchar*);

如笔者自定义函数

void messageOutput(QtMsgType, const char *msg)
{ 
    static QMutex mutex;
    mutex.lock();
    QString txtMessage;
    switch (type) {
    case QtDebugMsg:
        txtMessage = QString("[Debug] %1").arg(msg);
        break;
    case QtWarningMsg:
        txtMessage = QString("[Warning] %1").arg(msg);
        break;
    case QtCriticalMsg:
        txtMessage = QString("[Critical] %1").arg(msg);
        break;
    case QtFatalMsg:
        txtMessage = QString("[Fatal] %1").arg(msg);
        abort();
    default:
        break;
    }
    txtMessage += QString("\r\n");
    QFile file(QObject::tr("C:\\qDebug.txt"));
    if(file.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        QTextStream out(&file);
        out<<txtMessage;
    }
     file.flush();
     file.close();
    mutex.unlock();
}
 
int main(int argc, char *argv[])
{
    //安装处理程序
    qInstallMsgHandler(MessageOutput);
    QApplication a(argc, argv);
    // 打印信息
    qDebug("This is a debug message.");
    qWarning("This is a warning message.");
    qCritical("This is a critical message.");
    qFatal("This is a fatal message.");
    MainWindow w;
    w.show();
    return a.exec();
}
qDebug.txt 输出信息如下
[10:30:58.792][Debug] This is a debug message.
[10:30:59.754][Warning] This is a warning message.
[10:31:00.056][Critical] This is a critical message.

二、在Qt5以上版本中
上述函数是编译不通过,因为qInstallMsgHandler()函数已经被废弃掉。因为Qt 5.x后,这个函数被qInstallMessageHandler() 所替代了。
这个函数同样接受一个函数指针作为参数,函数的定义是 void FunctionName(QtMsgType,const QMessageLogContext& cosnt QString&),可见,该函数中多了一个QMessageLogContext参数。这个参数包含了debug的一些基本信息(代码所在文件信息、代码所在行号、错误类型、版本号、当前函数名),例如该debug所在的函数,所在的文件,所在的行数等。可以让debug更加详细。
如笔者下面例子
void MessageOutput(QtMsgType type,const QMessageLogContext& context,const QString& msg)
{
    QString txtMessage;
    QMutex mutex;
    //加锁
    mutex.lock();
    //设置log输出格式
    txtMessage += QString("[%1][%2][%3]")
            .arg(QTime::currentTime().toString("hh:mm:ss.zzz"))
            .arg(context.file)
            .arg(context.function);
    switch (type) {
    case QtDebugMsg:
        txtMessage += QString("[Debug] %1").arg(msg);
        break;
    case QtWarningMsg:
        txtMessage += QString("[Warning] %1").arg(msg);
        break;
    case QtCriticalMsg:
        txtMessage += QString("[Critical] %1").arg(msg);
        break;
    case QtFatalMsg:
        txtMessage += QString("[Fatal] %1").arg(msg);
        abort();
        break;
    default:
        txtMessage += QString("[UnKnown] %1").arg(msg);
        break;
    }
    txtMessage += QString(",{%1}").arg(context.line);
    txtMessage += QString("\r\n");
    //输出到文件(写,追加模式)
    QFile file("C:\\qDebug.txt");
    if(file.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        QTextStream out(&file);
        out<<txtMessage;
    }
    file.flush();
    file.close();
    // 解锁
     mutex.unlock();
}
int main(int argc, char *argv[])
{
    //安装休息处理程序
    qInstallMessageHandler(MessageOutput);
    QApplication a(argc, argv);
    // 打印信息
    qDebug("This is a debug message.");
    qWarning("This is a warning message.");
    qCritical("This is a critical message.");
    qFatal("This is a fatal message.");
    MainWindow w;
    w.show();
    return a.exec();
}

qDebug.txt输出信息如下:
[10:30:58.792][..\qDebug_5\main.cpp][int __cdecl main(int,char *[])][Debug] This is a debug message.,{64}
[10:30:59.754][..\qDebug_5\main.cpp][int __cdecl main(int,char *[])][Warning] This is a warning message.,{65}
[10:31:00.056][..\qDebug_5\main.cpp][int __cdecl main(int,char *[])][Critical] This is a critical message.,{66}
可见 Qt5以上版本qDebug可以输出更加详细信息来格式log。

0 1
原创粉丝点击