Capturing standard output and error streams in Julia.
Author JuliaDocs
25 Stars
Updated Last
1 Year Ago
Started In
September 2020


version Run tests pkgeval codecov

Provides the IOCapture.capture(f) function which evaluates the function f, captures the standard output and standard error, and returns it as a string, together with the return value. For example:

julia> c = IOCapture.capture() do
           return 42

julia> c.value, c.output
(42, "test\n")

See the docstring for full documentation.

Known limitations

Separately stored stdout or stderr objects

The capturing does not work properly if f prints to the stdout object that has been stored in a separate variable or object, e.g.:

julia> const original_stdout = stdout;

julia> c = IOCapture.capture() do
           println("output to stdout")
           println(original_stdout, "output to original stdout")
output to original stdout

julia> c.output
"output to stdout\n"

Relatedly, it is possible to run into errors if the stdout or stderr objects from within a capture are being used in a subsequent capture or outside of the capture:

julia> c = IOCapture.capture() do
           return stdout

julia> println(c.value, "test")
ERROR: IOError: stream is closed or unusable
 [1] check_open at ./stream.jl:328 [inlined]
 [2] uv_write_async(::Base.PipeEndpoint, ::Ptr{UInt8}, ::UInt64) at ./stream.jl:959

This is because stdout and stderr within an capture actually refer to the temporary redirect streams which get cleaned up at the end of the capture call.

ANSI color / escape sequences

On Julia 1.5 and earlier, setting color to true has no effect, because the ability to set IOContext attributes on redirected streams was added in 1.6. I.e. on those older Julia versions the captured output will generally not contain ANSI color escape sequences.

Similar packages

  • Suppressor.jl provides similar functionality, but with a macro-based interface.

Required Packages

No packages found.