There is probably something that I don’t understand with redirect_stdout but if I try using it and returning to the normal stdout it doesn’t work for ccall outputs. (using IJulia and Jupyter notebook with Julia Version 1.6.1)
using Random, RNGTest
rng = RNGTest.wrap(MersenneTwister(), UInt64);
logfile = open("testLog.txt","w")
redirect_stdout(logfile) do
RNGTest.smallcrushTestU01(rng) # executes `ccall`
end
close(logfile)
println("this prints normally")
RNGTest.smallcrushTestU01(rng) #still outputs to logfile
This indeed reproduces the same behaviour. However a second ccall(:putchar, Cint, (Cint,), 'A') at the end is missing to show how it fails to restore the ccall prints to stdout.
The solution given on that issue was to call Base.Libc.flush_cstdio() manually. I don’t see why this shouldn’t be done automatically by the redirecting function (here redirect_stdout)… maybe file an issue?
Indeed I just tried with 1.7 and it works like a charm. Also testing around, I found that the weird behaviour only happens on Jupyter Notebook. I’ve tried with Julia 1.4 and the same bug/behaviour happened.