Standard Output and Standard Error
A process can emit ordinary output and diagnostics through separate streams, letting a caller capture or redirect them independently.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace an export
A report command writes a CSV header and rows to standard output. A diagnostic about a bad row belongs to standard error for that interface. Mixing it into stdout can render downstream CSV parsing invalid. See CSV field structure.
Test both streams
A missing-file case can produce an error message, no data on ordinary output, and a nonzero exit status. Assert all promised outcomes without assuming terminal display order between streams. Capturing output gives a testing boundary.[1]
Make a pipeline able to read only result records
A converter writes JSON results and may also report one skipped row. If both messages use standard output, another parser can receive the warning amid its JSON data. Writing diagnostics to standard error gives consumers a separate channel for those observations. See capturing test output.
stdout: {"accepted":12}
stderr: skipped input row 3
Process exit status: defined by whether skipped records are permitted
Check three observable outcomes in the same test
Assert the data bytes, the diagnostic channel and the exit status independently. For an unrecoverable error, decide whether partial output is legal. A program can emit valid-looking data and still return an error status.