Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture stdout and redirect to plog #274

Open
Ry-Kode opened this issue Oct 26, 2023 · 11 comments
Open

Capture stdout and redirect to plog #274

Ry-Kode opened this issue Oct 26, 2023 · 11 comments
Labels

Comments

@Ry-Kode
Copy link

Ry-Kode commented Oct 26, 2023

Hello,

Would it be possibe to redirect stderr so that stuff written to stderr is written to the log?

Thanks!

@SergiusTheBest
Copy link
Owner

Hi @Ry-Kode !

Could you give some more details? As there could be different solutions.

@Ry-Kode
Copy link
Author

Ry-Kode commented Nov 4, 2023

Hello Sergius,

I'm not qutie sure what kind of details I can provide. Let me explain my use case: in my project I'm using your PLOG lib (thanks again for this!!) to create a rolling logfile. I'm also using some third party libraries which generate output that is written to stderr (and sometimes stdout). I'm looking for a way to 'capture' this output and save it to the PLOG rolling log file.

Thank you

@SergiusTheBest
Copy link
Owner

Can you recompile those libraries?

@SergiusTheBest
Copy link
Owner

So you can just add #define cerr cerr; PLOGI and include plog?

@Ry-Kode
Copy link
Author

Ry-Kode commented Nov 6, 2023

Yes, that would be one option.
Unfortuantely there are some libraries for which I do not have access to the source code so I cannot recompile them.

@Ry-Kode
Copy link
Author

Ry-Kode commented Nov 19, 2023

Hello Sergius,
Can you give recommendations on best approach for the libraries which I cannot recompile?
Thank you

@SergiusTheBest
Copy link
Owner

@Ry-Kode Are they static libs (.lib) or dynamic libs (.dll)?

@Ry-Kode
Copy link
Author

Ry-Kode commented Nov 20, 2023

Static libs (.lib)

@SergiusTheBest
Copy link
Owner

You can try the following approach:

union
{
    struct
    {
        int readPipe;
        int writePipe;
    };
    int pipeHandles[2];
} pipes;

_pipe(pipes.pipeHandles, 0, O_TEXT);

_dup2(pipes.writePipe, _fileno(stderr));

auto pipeFuture = std::async(launch::async, [&]()
{
    std::array<char, 0x800> buf{};

    for(;;)
    {
        int bytesRead = _read(pipes.readPipe, buf.data(), static_cast<int>(buf.size()));
        if (bytesRead <= 0)
        {
            break;
        }

        buf[bytesRead] = 0;
        LOGV << buf.data();
    }
});

std::cerr << "test test";

_close(pipes.writePipe);
_close(_fileno(stderr));

It may work for your setup. But there is no guarantee.

@Ry-Kode
Copy link
Author

Ry-Kode commented Nov 21, 2023

Thank you for the snippet Sergius.
However, It looks like this is Windows-only. _pipe, _dup2 etc are not supported on Posix. Do you an equivalent for Linux?

@SergiusTheBest
Copy link
Owner

Posix is the same, just without the underscore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants