plog
plog copied to clipboard
Capture stdout and redirect to plog
Hello,
Would it be possibe to redirect stderr so that stuff written to stderr is written to the log?
Thanks!
Hi @Ry-Kode !
Could you give some more details? As there could be different solutions.
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
Can you recompile those libraries?
So you can just add #define cerr cerr; PLOGI and include plog?
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.
Hello Sergius, Can you give recommendations on best approach for the libraries which I cannot recompile? Thank you
@Ry-Kode Are they static libs (.lib) or dynamic libs (.dll)?
Static libs (.lib)
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.
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?
Posix is the same, just without the underscore.