Results 1 to 9 of 9

Thread: Accessing program's own stdout/stderr

  1. #1
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Accessing program's own stdout/stderr

    I have a program where I am porting some old DOS code, and wrapping it in a Qt application.
    The problem I have is that the DOS code makes extensive use of writing to stdout and stderr for its data, and I want to capture it all and put it into a QTextEdit.

    I've noticed there is some functionality for this kind of thing _when_ running a child process via QProcess.

    I came across a couple suggestions, but they are relatively old - one referred to KProcess, but that was a 1998 post, and the second suggested using QSocketNotifier somehow.

    I tried using the QSocketNotifier, but I never get any notices even though I know data has been written via printf(). Here's how I setup the access:

    Qt Code:
    1. if (stdErrorReader == NULL)
    2. {
    3. stdErrorReader = new QFile(this);
    4. if (stdErrorReader != NULL)
    5. {
    6. stdErrorReader->open(2,QIODevice::ReadOnly|QIODevice::Text);
    7. connect(stdErrorReader,SIGNAL(readyRead()),this,SLOT(stdErrorDataAvailable()));
    8. if (stdErrorNotifier == NULL)
    9. {
    10. stdErrorNotifier = new QSocketNotifier(2,QSocketNotifier::Read,this);
    11. if (stdErrorNotifier != NULL)
    12. {
    13. connect(stdErrorNotifier,SIGNAL(activated(int)),this,SLOT(stdErrorDataAvailable(int)));
    14. }
    15. }
    16. }
    17. }
    18. if (stdOutReader == NULL)
    19. {
    20. stdOutReader = new QFile(this);
    21. if (stdOutReader != NULL)
    22. {
    23. stdOutReader->open(1,QIODevice::ReadOnly|QIODevice::Text);
    24. connect(stdOutReader,SIGNAL(readyRead()),this,SLOT(stdOutDataAvailable()));
    25. if (stdOutNotifier == NULL)
    26. {
    27. stdOutNotifier = new QSocketNotifier(1,QSocketNotifier::Read,this);
    28. if (stdOutNotifier != NULL)
    29. {
    30. connect(stdOutNotifier,SIGNAL(activated(int)),this,SLOT(stdOutDataAvailable(int)));
    31. }
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    The 'stdOutDataAvailable(int)' and 'stdOutDataAvailable()' slots never get called.

    What am I doing wrong?


    Is there a way to access the Application's QProcess() class so that I can use the methodology via QProcess?

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing program's own stdout/stderr


  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Accessing program's own stdout/stderr

    have you checked that the open calls don't fail?

    you can also close stdout and stderr and open a regular file (for that filedescriptor) and just display that file's contents (see the manpages on close, dup etc).

    HTH

  4. #4
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing program's own stdout/stderr

    Quote Originally Posted by MrDeath View Post
    Please note my original post - I would love to use those functions, but have not figured out how to access the main program's process object. Thus my question...

    Note - those functions are not static functions. Thus you need an instance of QProcess() associated with the process you want to read from. How do I get that for the main program? Where's the interface to get it via QApplication or QCoreApplication?

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

    Default Re: Accessing program's own stdout/stderr

    If you write to your own stdout (as I assume the application does), you can't read it back in. You have to substitute stdout and stderr with some mechanisms that will redirect the data elsewhere. Otherwise you have to modify all calls to printf() and fprintf() in the original code to send the data to some other place. You should be able to simply undef printf and define a new function or macro with this name and then rebuild the application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing program's own stdout/stderr

    Quote Originally Posted by caduel View Post
    have you checked that the open calls don't fail?

    you can also close stdout and stderr and open a regular file (for that filedescriptor) and just display that file's contents (see the manpages on close, dup etc).

    HTH
    Yes, I verified - the QFile::Open(int, OpenMode) call is returning 'true' for both stderr, and stdout.

    I haven't tried closing stderr/stdout and putting new files in their place - I've done it before, I was just hoping not to here.

    I am, however, trying to support Windows in this as well (my primary test environment right now). I may try moving it to Linux and testing to see if that changes things at all as well.

  7. #7
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing program's own stdout/stderr

    Thanks for the tip on stderr/stdout. Never came across that before, but I haven't tried to port legacy code this way before either.

    Well, for various reasons I can't get it to work on Linux well enough to even compile (legacy code issues).

    I did try replacing stderr, stdout but that didn't work either. (And I've done that in the past and had it work too, but not under Qt.)

    So I finally broke down and replaced all the printf/fprintf(stderr) calls with a new function that converts the stuff to QString, and then calls qDebug(), which is then captured via a handler to be redirected to the GUI location I want it to go.

    Problem is that the _vsnprintf() in VS2008/Qt4.5.1 seems to be borked - it never prints the text to the buffer, despite having a 32k buffer and data that is no where near 32k in size (likely 80 characters in size at most). It does set the buffer to all spaces, with proper termination; but no data.

    I originally pulled some code I had written to add 'printf()' style formatting to the std::string class that managed some dynamic memory to do the conversion with _vsnprintf(), but it just kept allocating memory and never actually writing anything. (And I know the function worked when I wrote and tested it!)

    So needless to say, I have to go on to other things for now, but I really want to resolve this issue - and I'm just plain out of ideas right now.

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

    Default Re: Accessing program's own stdout/stderr

    Can't you undef _vsnprintf() and replace it with your own routine? At worst just reassign the function pointer to something else, it should be doable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing program's own stdout/stderr

    And that would require writing something to interpret the whole printf format strings, which I'd rather not do - not to mention, I don't have time for it. vsnprintf/_vsnprintf are great functions, and there's no reason they should be failing like this. Not sure what I'm going to do about it though...

Similar Threads

  1. Configure antialias for Qt 4 programs (Debian Testing)
    By alecs1 in forum General Discussion
    Replies: 0
    Last Post: 10th April 2008, 23:38
  2. Distributing Qt programs
    By scwizard in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2007, 23:36

Tags for this Thread

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.