Qt two ways write Debug() into file

来源:互联网 发布:北大光华知乎 编辑:程序博客网 时间:2024/05/18 00:09

Here we can handle we can print different kind of message in file. For example QtDebugMsg, QtWarningMsg etc. We can constantly open a file and print to it and close it. Or we can open it in constructor of a class and flush/close in the destructor of the class. If we want to execute the application and want to get the debug information out then the first one is better (since file will be released). If we want to see the debug informationafter closing the app then second one should be OK.


void debugOutput(QtMsgType type, const char *msg) {  QFile debugfile("C:\\Data\\debug.txt"); Q_ASSERT(debugfile.open(QIODevice::WriteOnly | QIODevice::Text  | QIODevice::Append));      switch (type)      {         case QtDebugMsg:         {             QTextStream out(&debugfile);                  out << msg;                          }         break;           default :         break;      } debugfile.flush(); debugfile.close(); } #define TRACE_FUNC_ENTRY qDebug()<< __PRETTY_FUNCTION__ << ">" << endl ;#define TRACE_FUNC_EXIT qDebug()<< __PRETTY_FUNCTION__ << "<" << endl ;


原创粉丝点击