Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redirecting stdout into an `IOBuffer`
"""
redirect_stdout(f::Function, stream::IOBuffer)
```julia
julia> buf = IOBuffer();
julia> redirect_stdout(buf) do
print("Hello, World!")
end
julia> String(take!(buf))
"Hello, World!"
```
"""
function Base.redirect_stdout(f::Function, stream::IOBuffer)
backup = stdout
rd, wr = redirect_stdout()
try
f()
finally
redirect_stdout(backup)
close(wr)
write(stream, read(rd))
close(rd)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.