天天看點

3.QT中的debug相關的函數,以及檔案鎖的使用



建立項目t33debug

main.cpp

#include

<qdebug>

<qfile>

<qmutex>  

//檔案鎖

void

mymessagehandler(qtmsgtype

type,const

qmessagelogcontext

&context,const

qstring

&msg)

{

//使用一個檔案鎖,當在寫檔案的時候,要等寫完之後才能繼續執行

static

qmutex

mutex;

mutex.lock();

strcontext;

//在qmessagelogcontext中儲存的有檔案名,檔案行号,方法等資訊

strcontext.sprintf("%s

%d

%s:\r\n\t",context.file,context.line,context.function);

output

=

strcontext

+

msg

"\r\n";

if(type

==

qtdebugmsg)

qfile

file("debug.log");

file.open(qfile::writeonly

|

qfile::append);

file.write(output.toutf8());

file.close();

}

qtwarningmsg)

file("warning.log");

file.open(qfile::writeonly|qfile::append);

    file.write(output.toutf8());

qtcriticalmsg)

file("critical.log");

qtfatalmsg)

file("fatal.log");

printf("%s",output.toutf8().data());

mutex.unlock();

int

main()

//to

suppress

the

at

runtime,

install

your

own

message

//handler

with

qinstallmessagehandler().

qinstallmessagehandler(mymessagehandler);

qdebug()

<<

"output

debug";

qdebug("%s,%d","outputdebug",1);

qwarning()

"warning";

qcritical()

"cridical";

//可以放開下面的一句,然後發現目錄也有相應的fatal.log檔案

//qfatal("fatal

info");

運作結果:

3.QT中的debug相關的函數,以及檔案鎖的使用

輸入目錄(e:\qt\build-t33debug-desktop_qt_5_3_mingw_32bit-debug)的檔案如下:

3.QT中的debug相關的函數,以及檔案鎖的使用

繼續閱讀