Results 1 to 7 of 7

Thread: Stdout and redirection in linux

  1. #1
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Stdout and redirection in linux

    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.

  2. #2
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Stdout and redirection in linux

    Try adding CONFIG += console in pro file. In windows this is needed to see console output, but on unix it should be default.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Stdout and redirection in linux

    Quote Originally Posted by themolecule View Post
    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...

  4. #4
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Stdout and redirection in linux

    this works:

    main.cpp:
    Qt Code:
    1. #include <stdio.h>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. printf("hello world.\n");
    6. }
    To copy to clipboard, switch view to plain text mode 

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

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

    but this doesn't work:

    main2.pro:
    Qt Code:
    1. TEMPLATE = app
    2. TARGET = main2
    3. DESTDIR = .
    4.  
    5. CONFIG += release
    6.  
    7. SOURCES += main2.cpp
    To copy to clipboard, switch view to plain text mode 

    main2.cpp:
    Qt Code:
    1. #include <QCoreApplication>
    2.  
    3. #include <QString>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. printf("hello world.\n");
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by themolecule; 15th January 2008 at 14:52.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Stdout and redirection in linux

    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.

  6. #6
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Stdout and redirection in linux

    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)?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Stdout and redirection in linux

    Quote Originally Posted by themolecule View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.