0

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.

BigBrownBear00
  • 1,048
  • 1
  • 11
  • 19

1 Answers1

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
  • 193
  • 1
  • 8