Redirecting output from console programs

Discussion in 'C Programming' started by hilbert00, Jul 6, 2008.

  1. hilbert00

    hilbert00

    Joined:
    Jul 6, 2008
    Messages:
    1
    Dear all,

    in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help.

    In our program instead of using cout to print messages we implemented a custom ostream able to do the following:
    • Allow the user to choose where to send the messages: on the stdout, in to a file or to any other ostream
    • Effectively print the messages only if the verbosity level is reached. I.e. every message has its own verbosity level and if this is below the global user defined verbosity level it is not printed
    • Prepend each message with the module name and the verbosity level.

    Said so, we would like to execute within our software a third party program. The easiest way would be to use the system() call, but in this way all the output will be printed to the standard output and not passing through our custom ostream.

    The second step was to try to redirect the cout to our ostream before calling system (and restoring it afterward), but this fails because system() calls fork() and in the new process the stdout and cout are still in sync.

    The third step was to look at pipes and we were quite close to what we want. In fact we can redirect the stdout and the stderr to files and, when the program execution is over, we can read back both files passing through our ostream. The problem is that the execution of the external command can last several minutes and during this time interval no messages are printed out, making the user feeling the program is hanging...

    So in the end, what we would like to have is the possibility to catch the stdout of an external program and redirect it in real time to a custom ostream, possibly using ostreams and not pipes.

    Can you help us?

    Thanks,

    hilbert
    hilbert00, Jul 6, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jrefactors@hotmail.com
    Replies:
    12
    Views:
    1,618
    Dave Thompson
    Jan 10, 2005
  2. pratap
    Replies:
    3
    Views:
    444
    James Kanze
    Jul 19, 2007
  3. jan.rebada@gmail.com

    Reading console output and writing to console

    jan.rebada@gmail.com, Jan 26, 2008, in forum: C++
    Replies:
    5
    Views:
    3,416
    James Kanze
    Mar 4, 2008
  4. drhilbert
    Replies:
    7
    Views:
    385
    drhilbert
    Jul 7, 2008
  5. Vadim Dobrovolsky
    Replies:
    1
    Views:
    147
    Ryan Davis
    May 6, 2009
Loading...

Share This Page