Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Oh, you. I had a decent answer all prettied-up before I saw the last line of your example: execv(). Well, poop. The original idea was replacing each child process' stdout with an instance of this blog post's tee class, and split the stream into the original stdout, and the log file:</p> <p><a href="http://www.shallowsky.com/blog/programming/python-tee.html" rel="nofollow">http://www.shallowsky.com/blog/programming/python-tee.html</a></p> <p>But, since you're using execv(), the child process' tee instance would just get clobbered, so that won't work.</p> <p>Unfortunately for you, there is no "out of the box" solution to your problem that I can find. The closest thing would be to spawn the actual tee program in a subprocess; if you wanted to be more cross-platform, you could fork a simple Python substitute.</p> <p>First thing to know when coding a tee substitute: tee really is a simple program. In all the true C implementations I've seen, it's not much more complicated than this:</p> <pre><code>while((character = read()) != EOF) { /* Write to all of the output streams in here, then write to stdout. */ } </code></pre> <p>Unfortunately, you can't just join two streams together. That would be really useful (so that the input of one stream would automatically be forwarded out of another), but we've no such luxury without coding it ourselves. So, Eli and I are going to have very similar answers. The difference is that, in my answer, the Python 'tee' is going to run in a separate process, via a pipe; that way, the parent thread is still useful!</p> <p>(Remember: copy the blog post's tee class, too.)</p> <pre><code>import os, sys # Open it for writing in binary mode. logFile=open("bar", "bw") # Verbose names, but I wanted to get the point across. # These are file descriptors, i.e. integers. parentSideOfPipe, childSideOfPipe = os.pipe() # 'Tee' subprocess. pid = os.fork() if pid == 0: while True: char = os.read(parentSideOfPipe, 1) logFile.write(char) os.write(1, char) # Actual command pid = os.fork() if pid == 0: os.dup2(childSideOfPipe, 1) os.execv(cmd) </code></pre> <p>I'm sorry if that's not what you wanted, but it's the best solution I can find.</p> <p>Good luck with the rest of your project!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload