日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

3.QT中的debug相关的函数,以及文件锁的使用

發(fā)布時間:2024/9/27 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3.QT中的debug相关的函数,以及文件锁的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


1? 新建項目T33Debug

main.cpp

#include <QDebug>

#include <QFile>

#include <QMutex>?? //文件鎖

?

void MyMessageHandler(QtMsgType type,const QMessageLogContext &context,const QString &msg)

{

??? //使用一個文件鎖,當(dāng)在寫文件的時候,要等寫完之后才能繼續(xù)執(zhí)行

??? static QMutex mutex;

??? mutex.lock();

?

??? QString strContext;

??? //QMessageLogContext中保存的有文件名,文件行號,方法等信息

??? strContext.sprintf("%s %d %s:\r\n\t",context.file,context.line,context.function);

?

??? QString output = strContext + msg + "\r\n";

?

??? if(type == QtDebugMsg)

??? {

??????? QFile file("debug.log");

??????? file.open(QFile::WriteOnly | QFile::Append);

??????? file.write(output.toUtf8());

??????? file.close();

??? }

??? if(type == QtWarningMsg)

??? {

??????? QFile file("warning.log");

??????? file.open(QFile::WriteOnly|QFile::Append);

??? ????file.write(output.toUtf8());

??????? file.close();

??? }

??? if(type == QtCriticalMsg)

??? {

??????? QFile file("critical.log");

??????? file.open(QFile::WriteOnly | QFile::Append);

??????? file.write(output.toUtf8());

??????? file.close();

??? }

??? if(type == QtFatalMsg)

??? {

??????? QFile file("fatal.log");

??????? file.open(QFile::WriteOnly | QFile::Append);

??????? file.write(output.toUtf8());

??????? file.close();

??? }

?

??? printf("%s",output.toUtf8().data());

??? mutex.unlock();

}

?

int main()

{

??? //To suppress the output at runtime, install your own message

??? //handler with qInstallMessageHandler().

??? qInstallMessageHandler(MyMessageHandler);

??? qDebug() << "output debug";

??? qDebug("%s,%d","outputdebug",1);

?

??? qWarning() << "warning";

??? qCritical() << "cridical";

??? //可以放開下面的一句,然后發(fā)現(xiàn)目錄也有相應(yīng)的fatal.log文件

??? //qFatal("fatal info");

}

運(yùn)行結(jié)果:

輸入目錄(E:\QT\build-T33Debug-Desktop_Qt_5_3_MinGW_32bit-Debug)的文件如下:

?

總結(jié)

以上是生活随笔為你收集整理的3.QT中的debug相关的函数,以及文件锁的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。