Tuesday, August 31, 2010

File-based Logging in Qt

[This entry is a refinement of an article I found here]

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:

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
logfile << QTime::currentTime().toString().toAscii().data() << " Debug: " << msg << "\n";
represents the format before each message I write. In this example I write the time, then the type of message, then the message.


- Then inside the main():
logfile.open("E:/myFile.txt", ios::app);
qInstallMsgHandler(SimpleLoggingHandler);


- Now at any class, if I want to write a message, I will just include:
#include <QDebug>

and write the line I want. For example:
qDebug()<< "[XmlClass] Receiving xml data from httpclient";
[I added class name between square brackets to know the class I am in]

9 comments:

  1. (Y)

    but must include
    #include"QTime"
    #include"fstream"

    in main.cpp to make it work

    ReplyDelete
  2. Looks like I forget to mention some small points every now and then.
    Thanks Mamdouh for the feedback.

    ReplyDelete
  3. Sir,
    Could you help !!
    How do i implement the above code in my code

    ReplyDelete
  4. You just put all the above code (except the last two lines) in your main.cpp

    The last two lines:
    #include

    qDebug()<< "your text here";

    are put in any code (class) you want.

    ReplyDelete
  5. Thank you sir,
    sonia

    ReplyDelete
  6. sonia here,
    I want to how to build a user login application using qt?
    do we require a log file for that?

    ReplyDelete
  7. I'm afraid I don't understand the question.
    Do you want the application to "log" (print to a file) some data from inside the code, or to "login" with something like a username or password?

    ReplyDelete
  8. login (username or passwd ) ....
    please help me....

    ReplyDelete
  9. Login (username and password) is something else, not like this blog entry at all.

    If you login to an offline application (I mean you want to validate username/password offline), you can check QSettings class (with some encryption to secure the data)
    http://doc.qt.nokia.com/stable/qsettings.html

    If you want to login to a server (like a Gmail or Flickr application) then you should check QtNetwork module.
    http://doc.qt.nokia.com/4.7/qtnetwork.html
    http://doc.qt.nokia.com/4.7/qnetworkaccessmanager.html
    http://doc.qt.nokia.com/4.7/qauthenticator.html

    I hope this is answering your question.

    ReplyDelete