Testing applications for mobiles or embedded systems is very hard most of the time because I'm not always able to debug the application on the device. One solution was to write some debugging lines inside the program, and when the program passes each of these lines, the line will be written to a text file. Then I can simply open the text file and see how the application went or where it stopped.
Here it how it goes:
- Before the main(), I write:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using namespace std; ofstream logfile; void SimpleLoggingHandler(QtMsgType type, const char *msg) { switch (type) { case QtDebugMsg: logfile << QTime::currentTime().toString().toAscii().data() << " Debug: " << msg << "\n" ; break ; case QtCriticalMsg: logfile << QTime::currentTime().toString().toAscii().data() << " Critical: " << msg << "\n" ; break ; case QtWarningMsg: logfile << QTime::currentTime().toString().toAscii().data() << " Warning: " << msg << "\n" ; break ; case QtFatalMsg: logfile << QTime::currentTime().toString().toAscii().data() << " Fatal: " << msg << "\n" ; abort (); } } |
where each line of
1 | logfile << QTime::currentTime().toString().toAscii().data() << " Debug: " << msg << "\n" ; |
- Then inside the main():
1 2 | logfile.open( "E:/myFile.txt" , ios::app); qInstallMsgHandler(SimpleLoggingHandler); |
- Now at any class, if I want to write a message, I will just include:
1 | #include <QDebug> |
and write the line I want. For example:
1 | qDebug()<< "[XmlClass] Receiving xml data from httpclient" ; |