Julia: Redirect output to an IOBuffer

Created on 20 Aug 2015  Â·  6Comments  Â·  Source: JuliaLang/julia

It'd be really nice to be able to redirect stdout/err to an IOBuffer:

julia> b = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> redirect_stdout(b)
ERROR: MethodError: `redirect_stdout` has no method matching redirect_stdout(::Base.AbstractIOBuffer{Array{UInt8,1}})

I thought I saw another similar issue the other day, but was unable to locate it.

(Aside/another issue it would be nice to write this as a do block.)

O help wanted

Most helpful comment

What about my proposed implementation in #32567?

All 6 comments

+1

I think this would be hard if you want redirect_stdout to redirect C STDOUT. You could redirect to a pipe and then copy to the IOBuffer, but I think you'd have to wait until a task yield to do the copy, which might be counterintuitive. But if you call read_stdout with no arguments you can just use the pipe directly.

I'm not sure if it's really valid, but here is what we currently do in RCall to capture STDOUT:
https://github.com/JuliaStats/RCall.jl/blob/c1ff136864964cf2ac04b679f0c1b3b243df7e37/src/iface.jl#L35-L46

(this code is about to be deprecated in favour of using R's API hooks)

What about my proposed implementation in #32567?

I think it is quite useful.

I am trying to redirect output to a Poptart GUI.

I found a solution in stackoverflow, but supporting IOBuffer makes the code way cleaner.

The current redirect_stdout doesn't work with IOContext, which makes it even less useful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixrehren picture felixrehren  Â·  3Comments

StefanKarpinski picture StefanKarpinski  Â·  3Comments

musm picture musm  Â·  3Comments

ararslan picture ararslan  Â·  3Comments

i-apellaniz picture i-apellaniz  Â·  3Comments