在QT中使用日誌系統

2021-09-13 19:50:26 字數 840 閱讀 2619

當寫好的軟體發布出去後,使用者遇到宕機或一些其他的bug,我們該怎麼追蹤這些問題呢,這時候日誌系統很好的幫助了我們。最近也是參照網路大牛的部落格**實現了log。下面看看**吧。

void outputmessage(qtmsgtype type, const qmessagelogcontext &context, const qstring &msg)

qstring context_info = qstring("file:(%1) line:(%2)").arg(qstring(context.file)).arg(context.line);

qstring current_date_time = qdatetime::currentdatetime().tostring("yyyy-mm-dd hh:mm:ss ddd");

qstring current_date = qstring("(%1)").arg(current_date_time);

qstring message = qstring("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);

qfile file("log.txt");

qtextstream text_stream(&file);

text_stream << message << "\r\n";

file.flush();

file.close();``

mutex.unlock();

}int main(int argc, char *ar**)

不用新增其他標頭檔案,直接複製即可使用。

在Qt中使用sleep

在qt中使用sleep 關於sleep函式,我們先來看一下他的作用 sleep函式是使呼叫sleep函式的執行緒休眠,執行緒主動放棄時間片。當經過指定的時間間隔後,再啟動執行緒,繼續執行 sleep函式並不能起到定時的作用,主要作用是延時。在一些多執行緒中可能會看到sleep 0 其主要目的是讓出時...

在QT中使用sqlite

sqlite sql 是一款開源輕量級的資料庫軟體,不需要server,可以整合在其他軟體中,非常適合嵌入式系統。qt5以上版本可以直接使用sqlite qt自帶驅動 引入sql模組 在qt專案檔案 pro檔案 中,加入sql模組 qt sql include include include檢查連線...

在Qt中使用中文路徑

在qt中字串預設使用latin 1編碼,不支援中文,需要轉化成支援中文的格式,如gbk等。使用qtextcodec可以設定qt使用的字元編碼。qtextcodec codec qtextcodec codecforname gbk qtextcodec setcodecforlocale codec...