2

I want to generate Log in my application i.e whatever i get on terminal window or cmd window to be generated as log in a text file.

e.g. I have a client server program so whatever message or warnings i get on terminal to be printed on log file along with timestamp.

Any Idea,Are QxtLog or log4Qt do the same that i want?

CowboY
  • 99
  • 2
  • 7
  • 1
    I'm not sure, maybe [this answer](http://stackoverflow.com/questions/4954140/how-to-redirect-qdebug-qwarning-qcritical-etc-output) will help you (if you're using `Qt 4.*`? Else if you're using `Qt 5.*` then read [this](http://qt-project.org/doc/qt-5.0/qtcore/qtglobal.html#qInstallMsgHandler) : the API has been changed a bit, deprecating the previous one! – Nawaz Nov 19 '13 at 09:56
  • @Nawaz: I tried that but qInstallMsgHandler() doesn't work it shows it doesn't exist. – CowboY Nov 19 '13 at 10:10
  • then try `qInstallMessageHandler()`, as I said, [the API has been changed since Qt 5.0](http://qt-project.org/doc/qt-5.0/qtcore/qtglobal.html#qInstallMsgHandler). – Nawaz Nov 19 '13 at 11:01
  • @Nawaz:i tried that also still not working – CowboY Nov 19 '13 at 12:10
  • *"still not working"* is almost always equal to NO information. – Nawaz Nov 19 '13 at 12:16
  • @Nawaz: Sorry buddy for that.I am getting this error:I used it as you told as i use Qt5 so have to replace qInstallMessageHandler() but it is showing error as follows:invalid conversion from 'void ()(QtMsgType, const char)' to 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}' [-fpermissive] – CowboY Nov 20 '13 at 10:15

2 Answers2

3

Install your Qt message handler.

FILE *fd;

void myMessageOutput(QtMsgType type, const char *msg)
{
    QString timeStamp = QTime::currentTime().toString("hh:mm:ss:zzz");
    switch (type) {
    case QtDebugMsg:
        fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
        fprintf(fd, "[Debug] %s\n", msg);
        break;
    case QtWarningMsg:
        fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
        fprintf(fd, "[Warning] %s\n", msg);
        break;
    case QtCriticalMsg:
        fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
        fprintf(fd, "[Critical] %s\n", msg);
        break;
    case QtFatalMsg:
        fprintf(fd, "[%s]", timeStamp.toStdString().c_str());
        fprintf(fd, "[Fatal] %s\n", msg);
        abort();
    }
}

int main(int argc, char **argv)
{
    fd = fopen("log.txt", "a");
    qInstallMsgHandler(myMessageOutput);
    QApplication app(argc, argv);
    ...
    return app.exec();
}
liuyi.luo
  • 1,219
  • 7
  • 11
  • Will it print all messages on terminal be it warning,error messages etc ?I mean does these msgtypes cover all the messages appearing on cmd,terminal? – CowboY Nov 19 '13 at 12:12
  • 1
    Yes. There are four debug level in Qt. Four global functions are used to write out warning and debug message. They are `qDebug()`, `qWarning()`, `qCritical()` and `qFatal()`. – liuyi.luo Nov 20 '13 at 01:37
  • I used it as you told as i use Qt5 so have to replace qInstallMessageHandler() but it is showing error as follows:invalid conversion from 'void (*)(QtMsgType, const char*)' to 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}' [-fpermissive] – CowboY Nov 20 '13 at 10:14
  • Sorry, I have no experience on Qt5. I hope [this](http://stackoverflow.com/questions/14643293/how-does-qt5-redirect-qdebug-statements-to-the-qt-creator-2-6-console) can help you. – liuyi.luo Nov 20 '13 at 10:26
1

One quick and simple approach would be to pipe the output of your software to a file.

For a more sophisticated approach I suggest considering log4cplus (http://sourceforge.net/p/log4cplus/wiki/Home/) - I don't know log4Qt, but I guess it might have a similar interface.

Log4Cplus allows to create different channels/priorities for your logs, so you can also filter your output in terms of importance, which might be interesting depending on the size of your project.

Modass
  • 197
  • 8