Write to file and stdout

You can also use Base.BufferStream for this, which is a fully implemented IO type (and it might be public API at some point).

function tee_io(captures...)
	bs = Base.BufferStream()

	t = @async begin
		while !eof(bs)
			data = readavailable(bs)
			isempty(data) && continue

			for s in captures
				write(s, data)
			end
		end
	end

	function closeme()
		close(bs)
		wait(t)
	end

	return (io=bs, close=closeme)
end

Example use, tee’ing the same data into stdout, stderr and into a buffer (created by Base.sprint):

julia> sprint() do buffer
               
               tee = tee_io(stdout, stderr, buffer)

               # use the tee
               println(tee.io, "Hello!")
               show(tee.io, "text/plain", rand(4))
               println(tee.io)

               # close it
               tee.close()
       end
Hello!
4-element Vector{Float64}:
 0.6197851577079866
 0.11502872199346159
 0.09949018487645467
 0.35495614153953925
Hello!
4-element Vector{Float64}:
 0.6197851577079866
 0.11502872199346159
 0.09949018487645467
 0.35495614153953925
"Hello!\n4-element Vector{Float64}:\n 0.6197851577079866\n 0.11502872199346159\n 0.09949018487645467\n 0.35495614153953925\n"

5 Likes