# Suppress non-julia console output

**URL:** <https://discourse.julialang.org/t/suppress-non-julia-console-output/46593>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 14, 2020, 4:20pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593 "2020-09-14T16:20:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)\
**Post date:** [September 14, 2020, 4:20pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/1 "2020-09-14T16:20:13Z")

</div>

I’m calling c code from julia, which prints to console when it is run. I’d like to not see these print statements. How do I do that? The `Suppressor.@suppress_out` macro doesn’t work in this case.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 14, 2020, 5:14pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/2 "2020-09-14T17:14:31Z")

</div>

How do you call it from Julia? If you are calling an external program you may do so with [`Base.pipeline`](https://docs.julialang.org/en/v1/base/base/#Base.pipeline-Tuple%7BBase.AbstractCmd%7D).

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [September 14, 2020, 7:14pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/3 "2020-09-14T19:14:12Z")

</div>

`redirect_stdout` is your friend 😉

```julia
help?> redirect_stdout
search: redirect_stdout redirect_stdin redirect_stderr

  redirect_stdout([stream]) -> (rd, wr)

  Create a pipe to which all C and Julia level stdout output will be redirected. Returns a tuple (rd, wr) representing the pipe ends. Data written to stdout may now be read from the rd end of the pipe. The wr end is given for convenience in case the
  old stdout object was cached by the user and needs to be replaced elsewhere.

  If called with the optional stream argument, then returns stream itself.

  │ Note
  │
  │ stream must be a TTY, a Pipe, or a socket.

  ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  redirect_stdout(f::Function, stream)

  Run the function f while redirecting stdout to stream. Upon completion, stdout is restored to its prior setting.

  │ Note
  │
  │ stream must be a TTY, a Pipe, or a socket.

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 14, 2020, 8:44pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/4 "2020-09-14T20:44:45Z")

</div>

Funny, I thought `Suppressor.@suppress_out` did use `redirect_stdout` internally.

---

<div class="post-metadata">

**Author:** ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)\
**Post date:** [September 15, 2020, 7:16am UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/5 "2020-09-15T07:16:23Z")

</div>

Thanks for the help. Problem: it doesn’t seem to work. This function is run in parallel, too. So every single worker is spamming my console.

```julia
mktemp() do path, io
    redirect_stdout(io) do
         verbose_function(args...)
    end
end

```

This snippet still prints to the console

---

<div class="post-metadata">

**Author:** ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)\
**Post date:** [September 15, 2020, 7:18am UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/6 "2020-09-15T07:18:20Z")

</div>

I’m calling a julia wrapper around c code. So everything I see is julia, but in the background there’s verbose c code.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [September 15, 2020, 12:01pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/7 "2020-09-15T12:01:36Z")

</div>

this works

```julia
julia> str = mktemp() do path, io
           redirect_stdout(io) do
                ccall(:printf, Cint, (Cstring,), "Hello World!")
                Base.Libc.flush_cstdio()
           end
           readline(path) 
       end;

julia> str
"Hello World!"

```

but `verbose_function` might be printing to stderr as well; Or there could be some complication with parallelization (each worker gets it’s own stdout ?? this seems unlikely?)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 15, 2020, 10:15pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/8 "2020-09-15T22:15:03Z")

</div>

If this works, then it seems the parallelization is the culprit. @Luapulu, the methods you ask the workers to execute have `redirect_stdout`, or you call `redirect_stdout` only in the main thread, and then start passing tasks to workers only after?

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 16, 2020, 2:03pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/9 "2020-09-16T14:03:49Z")

</div>

I noticed there’s a new similar package, waiting to be registered: [https://github.com/JuliaDocs/IOCapture.jl](https://github.com/JuliaDocs/IOCapture.jl)

---

<div class="post-metadata">

**Author:** ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)\
**Post date:** [September 16, 2020, 3:07pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/10 "2020-09-16T15:07:42Z")

</div>

I think the `Base.Libc.flush_cstdio()` did the job. Thanks, it works!

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [September 16, 2020, 3:13pm UTC](https://discourse.julialang.org/t/suppress-non-julia-console-output/46593/11 "2020-09-16T15:13:35Z")

</div>

yes, depending on the buffering mode (full, line, unbuffered) you need to flush more or less often. you could potentially change this with `ccall` to `setvbuf` (from glibc), but I’ve never succeeded 😉

glad it worked!
