# Capture the stdout output of a ccall?

**URL:** <https://discourse.julialang.org/t/capture-the-stdout-output-of-a-ccall/68262>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [September 16, 2021, 2:06pm UTC](https://discourse.julialang.org/t/capture-the-stdout-output-of-a-ccall/68262 "2021-09-16T14:06:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 16, 2021, 2:06pm UTC](https://discourse.julialang.org/t/capture-the-stdout-output-of-a-ccall/68262/1 "2021-09-16T14:06:30Z")

</div>

I’m `ccall`ing a function in a shared lib which writes something to stdout (i.e. it appears it the terminal). Is it possible to capture this text output? Among other things, I tried

```julia
mktemp() do path, io
    redirect_stdout(io) do
        println("test1")
        ccall(...)
        println("test2")
    end
    flush(io)
    s = read(path, String)
end

```

but this only produces `s == "test1\ntest2\n"` .

Any ideas how to capture the stdout output triggered by the `ccall` as well?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 16, 2021, 2:19pm UTC](https://discourse.julialang.org/t/capture-the-stdout-output-of-a-ccall/68262/2 "2021-09-16T14:19:34Z")

</div>

Maybe it prints to `stderr`?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 16, 2021, 2:26pm UTC](https://discourse.julialang.org/t/capture-the-stdout-output-of-a-ccall/68262/3 "2021-09-16T14:26:03Z")

</div>

No, it doesn’t. Replacing `redirect_stdout` with `redirect_stderr` just gives `s == ""`.

---

<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:** [September 16, 2021, 2:27pm UTC](https://discourse.julialang.org/t/capture-the-stdout-output-of-a-ccall/68262/4 "2021-09-16T14:27:38Z")

</div>

Did you try with Julia 1.7? I think I have seen it capture more things from standard streams.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 16, 2021, 2:41pm UTC](https://discourse.julialang.org/t/capture-the-stdout-output-of-a-ccall/68262/5 "2021-09-16T14:41:31Z")

</div>

> [@carstenbauer](#):
>
> No, it doesn’t. Replacing `redirect_stdout` with `redirect_stderr` just gives `s == ""` .

I just tried with a simple example and it did capture it properly, so not sure why it doesn’t work for you.
