PDA

View Full Version : Stdout and redirection in linux



themolecule
15th January 2008, 04:37
I have written a little application that works fine... it prints all kinds of useful things to the terminal using printf()...

but I cannot seem to redirect output!

It seems like it may be something in qt, but I can't find it.

If I write a simple "hello world" app using printf then

a.out > file

works fine, with "hello world" in the file called "file".

but the full-blown application never writes anything to the redirected file, no matter what I do. I've seen this before with other unix apps where output is visible when normally run, but redirected output never gets to the file.

Is there some environment variable or include flag? It seems like if I don't use Qt then redirection works, but something in some qt file does something to stdout so that I cannot capture it.

bender86
15th January 2008, 07:50
Try adding CONFIG += console in pro file. In windows this is needed to see console output, but on unix it should be default.

wysota
15th January 2008, 10:16
but the full-blown application never writes anything to the redirected file, no matter what I do. I've seen this before with other unix apps where output is visible when normally run, but redirected output never gets to the file.

Could you describe how you print data to stdout and how you perform the redirection? The exact statements please...

themolecule
15th January 2008, 14:29
this works:

main.cpp:


#include <stdio.h>

int main(int argc, char *argv[])
{
printf("hello world.\n");
}


g++ main.cpp
./a.out > file

"file" now contains "hello world\n".

but this doesn't work:

main2.pro:


TEMPLATE = app
TARGET = main2
DESTDIR = .

CONFIG += release

SOURCES += main2.cpp


main2.cpp:


#include <QCoreApplication>

#include <QString>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

printf("hello world.\n");

return app.exec();
}


qmake
make
./main2 > file

now "file" is empty (after you ctrl-c to quit)

while
./main2

prints "hello world.\n" to the terminal.

Do I have to freopen because of QCoreApplication? Does it have to do with the ctrl-c which could be killing the program before it flushes the buffer?

thanks,
-chris

PS:

./main2 >file 2>&1

doesn't work either!

wysota
15th January 2008, 15:43
Try removing the exec() call or quit the loop using a timer and see if the problem persists. It probably won't thus it seems that the problem is with you killing the process before exec() returns.

themolecule
15th January 2008, 16:02
I could remove the exec() for testing, but I need it, because my app needs an event loop.

And, if I run the app for a long time, ie, generate a lot of lines of output, it seems like it would have nothing to do with how quickly ctrl-c is pressed.

---

Just tested... commenting out exec() does cause output to redirect, but I need exec to start the event loop.

Why does it print to the terminal normally but not to stdout when redirecting? Perhaps QCoreApplication puts it on a different file pointer (ie, not 1 or 2 -- stdout or stderr)?

wysota
15th January 2008, 17:06
I could remove the exec() for testing, but I need it, because my app needs an event loop.

And, if I run the app for a long time, ie, generate a lot of lines of output, it seems like it would have nothing to do with how quickly ctrl-c is pressed.
No, but you can leave the loop in a gracious way and not by killing the application.




Why does it print to the terminal normally but not to stdout when redirecting?
I have no idea.

Perhaps QCoreApplication puts it on a different file pointer (ie, not 1 or 2 -- stdout or stderr)?

No, I don't think it does. Try using qDebug() instead of printf(), it should work just fine.