Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I capture stdout/stderr separately and maintain original order?

I've written a Windows application using the native win32 API. My app will launch other processes and capture the output and highlight stderr output in red.

In order to accomplish this I create a separate pipe for stdout and stderr and use them in the STARTUPINFO structure when calling CreateProcess. I then launch a separate thread for each stdout/stderr handle that reads from the pipe and logs the output to a window.

This works fine in most cases. The problem I am having is that if the child process logs to stderr and stdout in quick succession, my app will sometimes display the output in the incorrect order. I'm assuming this is due to using two threads to read from each handle.

Is it possible to capture stdout and stderr in the original order they were written to, while being able to distinguish between the two?

like image 562
flashk Avatar asked Oct 09 '09 19:10

flashk


People also ask

Does each process have its own stdout?

A process executes a program; you can have multiple processes executing the same program, but each process has its own copy of the program within its own address space and executes it independently of the other copies.” Every process is initialized with three open file descriptors called stdin , stdout , and stderr .

Is stdout shared between processes?

Yes they share the same location.

Should logs go to stderr or stdout?

Only error logs should go to stderr. This is a pretty common convention supported by the 12factor design principles and the original intent behind io streams in operating systems. With all logs dumped to stderr, it's significantly more difficult to sift through output from using pipes.

What is the difference between stderr and stdout?

stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error. It is invoked whenever a command faces an error, then that error message gets stored in this data stream.


1 Answers

I'm pretty sure it can't be done, short of writing the spawned program to write in packets and add a time-stamp to each. Without that, you can normally plan on buffering happening in the standard library of the child process, so by the time they're even being transmitted through the pipe to the parent, there's a good chance that they're already out of order.

like image 130
Jerry Coffin Avatar answered Sep 22 '22 04:09

Jerry Coffin