# Redirect\_stdout fails to restore ccall prints

**URL:** <https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262>\
**Category:** General Usage\
**Created:** [January 27, 2022, 1:41am UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262 "2022-01-27T01:41:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Codzilla](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Codzilla](https://discourse.julialang.org/u/Codzilla)\
**Post date:** [January 27, 2022, 1:41am UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/1 "2022-01-27T01:41:01Z")

</div>

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)

```julia
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

```

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 27, 2022, 4:16pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/2 "2022-01-27T16:16:15Z")

</div>

A smaller MWE, but the behaviour is the same, the putchar from C appears on stdout. Just so you know it is not related to the included modules

```julia
julia> logfile = open("testLog.txt","w")
IOStream(<file testLog.txt>)
julia> redirect_stdout(logfile) do
           ccall(:putchar, Cint, (Cint,), 'A')
       end
65
julia> close(logfile)

```

---

<div class="post-metadata">

**Author:** ![Codzilla](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Codzilla](https://discourse.julialang.org/u/Codzilla)\
**Post date:** [January 27, 2022, 4:27pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/3 "2022-01-27T16:27:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 27, 2022, 4:28pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/4 "2022-01-27T16:28:20Z")

</div>

oh, yours is outputting ?

Mine doesn’t write anything to the log file

---

<div class="post-metadata">

**Author:** ![Codzilla](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Codzilla](https://discourse.julialang.org/u/Codzilla)\
**Post date:** [January 27, 2022, 4:33pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/5 "2022-01-27T16:33:08Z")

</div>

try flushing the cstdio as such

```julia
logfile = open("testLog.txt","w")
redirect_stdout(logfile) do
    ccall(:putchar, Cint, (Cint,), 'A')
    Base.Libc.flush_cstdio()
end
close(logfile)
ccall(:putchar, Cint, (Cint,), 'B')

```

Runing this, I get the string “AB” in the logfile

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 27, 2022, 4:34pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/6 "2022-01-27T16:34:54Z")

</div>

This looks like [https://github.com/JuliaLang/julia/issues/31236](https://github.com/JuliaLang/julia/issues/31236)

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?

---

<div class="post-metadata">

**Author:** ![Codzilla](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Codzilla](https://discourse.julialang.org/u/Codzilla)\
**Post date:** [January 27, 2022, 4:43pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/7 "2022-01-27T16:43:44Z")

</div>

My issue is slightly different. I can redirect ccall to the file once but then I can’t get it to print to stdout again.  
For example,

```julia
temp = stdout

logfile = open("testLog.txt","w")

redirect_stdout(logfile)
ccall(:putchar, Cint, (Cint,), 'A')
Base.Libc.flush_cstdio()

redirect_stdout(temp)
close(logfile)

ccall(:putchar, Cint, (Cint,), 'B')

```

Prints ‘AB’ in the log file which is weird since I closed the file and because the stdout has been redirected to the initial standard output

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 27, 2022, 4:51pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/8 "2022-01-27T16:51:26Z")

</div>

Ah sorry I misread what you wrote.

It seems this was fixed in 1.7, at least I cannot reproduce what you see with 1.7.1.

---

<div class="post-metadata">

**Author:** ![Codzilla](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Codzilla](https://discourse.julialang.org/u/Codzilla)\
**Post date:** [January 27, 2022, 5:22pm UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/9 "2022-01-27T17:22:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 28, 2022, 8:16am UTC](https://discourse.julialang.org/t/redirect-stdout-fails-to-restore-ccall-prints/75262/10 "2022-01-28T08:16:51Z")

</div>

I added this example to my Czoo module  
[https://github.com/lawless-m/Czoo.jl](https://github.com/lawless-m/Czoo.jl)

PRs are very welcome
