skypjack / uvw

Header-only, event based, tiny and easy to use libuv wrapper in modern C++ - now available as also shared/static library!
MIT License
1.82k stars 207 forks source link

Question: Pipe process output #310

Closed mathisloge closed 5 months ago

mathisloge commented 6 months ago

Take the following code example:

    auto loop = uvw::loop::get_default();
    uvw::process_handle::disable_stdio_inheritance();

    auto process_ = loop->resource<uvw::process_handle>();
    auto out_pipe_ = loop->resource<uvw::pipe_handle>();

    process_->stdio(*out_pipe_,
                    uvw::process_handle::stdio_flags::CREATE_PIPE | uvw::process_handle::stdio_flags::WRITABLE_PIPE |
                        uvw::process_handle::stdio_flags::READABLE_PIPE);
    process_->stdio(uvw::std_in, uvw::process_handle::stdio_flags::IGNORE_STREAM);
    process_->stdio(uvw::std_err, uvw::process_handle::stdio_flags::IGNORE_STREAM);
    (3) process_->stdio(uvw::std_out, uvw::process_handle::stdio_flags::INHERIT_FD);

    out_pipe_->on<uvw::data_event>(
        [](const uvw::data_event &data, const uvw::pipe_handle &pipe) { std::cout << "Got data_event" << std::endl; });
    out_pipe_->on<uvw::listen_event>(
        [](const auto &data, const uvw::pipe_handle &pipe) { std::cout << "Got listen_event" << std::endl; });
    out_pipe_->on<uvw::write_event>(
        [](const auto &data, const uvw::pipe_handle &pipe) { std::cout << "Got write_event" << std::endl; });

    process_->spawn(path_to_application.c_str(), {});

How to I pipe the stdout into the out_pipe_? I've tried multiple things, but I got no output from any of the on handlers. e.g. I've removed (3), tried different flags, alterted the calls of the stdio etc. The spawned process writes an output every second. With (3) and the INHERIT_FD call I get the outputs to my console output.

Do you know how to do it?

skypjack commented 6 months ago

You should direct this question to libuv rather than uwv, sorry. This library is just a thin wrapper around the underlying library but doesn't add or change functionality. Once you know how to do it with libuv, I can help you translate it to uwv if in troubles. 👍