# Redirect stdout, stderr to string/stream C++ (embedded)

**URL:** <https://discourse.julialang.org/t/redirect-stdout-stderr-to-string-stream-c-embedded/124210>\
**Category:** New to Julia\
**Tags:** embedding\
**Created:** [December 27, 2024, 3:02pm UTC](https://discourse.julialang.org/t/redirect-stdout-stderr-to-string-stream-c-embedded/124210 "2024-12-27T15:02:23Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 27, 2024, 5:16pm UTC](https://discourse.julialang.org/t/redirect-stdout-stderr-to-string-stream-c-embedded/124210/4 "2024-12-27T17:16:57Z")

</div>

> [@Nelson-numerical-sof](#):
>
> Yes, I would like also redirect display output into stdout to have in a std::string

Just re-assigning `Base.stdout` or calling `redirect` won’t accomplish this, because the default display backend saves a reference to the original `stdout`. (Also, the whole point of `display` is that the output can be specific to your enviroment, e.g. some objects might display graphically in Jupyter or Pluto or similar environments.)

The right way to capture `display` output is to call [`pushdisplay`](https://docs.julialang.org/en/v1/base/io-network/#Base.Multimedia.pushdisplay) to push a new display backend to the top of the stack — this will get called first for any code that invokes `display(x)`, allowing you to do whatever you want, displaying objects as text or other supported formats as desired, or capturing the output in a string or array. This is what IJulia does, for example. It’s also what [Pluto.jl does](https://github.com/fonsp/Pluto.jl/blob/caf74569883b74fba8024c627a802a79e62456e9/src/runner/PlutoRunner/src/io/stdout.jl#L59).

However, I want to step back a bit. Why are you doing this? Are you creating a new interactive environment for Julia? If not, i.e. for non-interactive use, I wouldn’t recommend communicating with `libjulia` via strings, and especially not by capturing `stdout` — better to pass data back and forth directly, in native formats.

---

_[View the full topic](https://discourse.julialang.org/t/redirect-stdout-stderr-to-string-stream-c-embedded/124210)._
