PDA

View Full Version : Generating Log files



srohit24
13th July 2009, 08:57
Hi

I want to generate a log file for my application. Does Qt have any inbuilt function for that??

I am using qDebug to return some information during the execution of the process.

I want to put all my qDebug and qWarnings into a log file along with the time of generation of the text.

I can stream the qDebug into a file,before i do that, i want to know if there are any inbuilt function.

spirit
13th July 2009, 09:47
have a look at qInstallMsgHandler, you can install your message handler which will save qDebug info into file. also read this (http://www.qtcentre.org/forum/f-qt-programming-2/t-redirect-qdebug-to-file-19534.html/?highlight=qDebug) thread.

srohit24
13th July 2009, 16:51
thanks mate. the thread and the qInstallMsgHandler helped.

But the log file keeps on writing in the same line. it doesnt go to the next line.
there is a endl in the code.

any ideas as to how to fix this?

spirit
13th July 2009, 17:17
that code works fine for me. :)

srohit24
14th July 2009, 06:47
I am using this code

void myMessageHandler(QtMsgType type, const char *msg)
{
QString txt;
QDateTime date;

txt = msg;
txt.prepend(" - ");
txt.prepend(date.currentDateTime().toString());

QFile outFile("log.txt");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << txt <<endl<<flush;


}

int main(int argc, char *argv[])
{
qInstallMsgHandler(myMessageHandler);
QApplication a(argc, argv);
MainWindow w;
return a.exec();
}

I am able to get all the data in 1 line

Tue Jul 14 10:15:54 2009 - i am in debug
Tue Jul 14 10:16:06 2009 - i am in debug


One more question, will qDebug work only if the app is built in debug mode??

nish
14th July 2009, 07:15
try to pass "\n" instead of endl.. QTextStream takes care of unix and windows line deliminaters.
qDebug will work in debug mode only i think.

srohit24
14th July 2009, 13:24
i am not able to see the text in the new line only if i open the file in notepad.

If I open the same file in wordpad its all written in different lines.

weird

thanks for the info.

spirit
14th July 2009, 15:23
I've just ran code again and got the following result


Tue Jul 14 16:19:47 2009 - "00in.png"
Tue Jul 14 16:19:47 2009 - "00out.png"
Tue Jul 14 16:19:47 2009 - "01in.png"
...

i.e. works fine, btw in release mode I had the same result.
PS. what platform do you use? what is Qt's version? I've tested on windows.

srohit24
15th July 2009, 06:56
I am using Qt 2.1 and it is working even in release mode.

I am able to get each statement in a new line if I open the file in wordpad.

thanks for the help

nish
15th July 2009, 07:08
HOLY COW!! ... Qt 2.1...!!!!!!

spirit
15th July 2009, 07:21
I am using Qt 2.1 and it is working even in release mode.

I am able to get each statement in a new line if I open the file in wordpad.

thanks for the help
wow, I don't even know what to say :p

srohit24
15th July 2009, 08:55
I have installed this

qt-sdk-win-opensource-2009.02.1.exe

spirit
15th July 2009, 08:57
so, this is not Qt 2.1, this is Qt 4.5.x.
is the problem solved?