Similar to this question, but more specifically, how can I redirect C++ standard out that is called from Python to a log file? The log file is created and written to using the python logging
module with a RotatingFileHandler
. Currently using Python 2.7.x.
Asked
Active
Viewed 1,264 times
0

BigBrownBear00
- 1,378
- 2
- 14
- 24
-
When I wrote the answer, I missed the python 2.7 part... python 2.7 is dead. – Javier Gonzalez Sep 15 '20 at 14:49
1 Answers
0
I will venture an answer while hoping someone suggests something better. And I am not giving code examples because it would take me time. It also depends on how you do the wrapping of C++, I guess.
If I controlled the C++ code, and it was not too big, I would replace all uses of stdout by a logging mechanism, which I could easily redirect to a function exposed from python.
If this is not possible, I would redirect stdout (all within C++) to a custom streambuffer (something like this, but with a stream that writes to the function exposed from python). I would check boost's streambuffer.
EDIT 1
If you use PyBind11 to expose C++, check capturing-standard-output-from-ostream

Javier Gonzalez
- 399
- 4
- 10