vrasneur/pyfasttext

Getting output of logs from C++

Closed this issue · 2 comments

I am trying to set the 'verbose' argument to see the output of logs within python.

Is this possible?

In the fastText source code, the logs are written to stderr (and I cannot modify the original source code in my binding).

To redirect stderr to a file, you can use the contextlib.redirect_stderr(file_object) function (which is only available starting from python 3.5).

Or if you want a lower level approach, redirect the stderr fileno using os.dup2()

Vincent

Hum... I have read the source code of the contextlib.redirect_stderr(file_object) function, and it works only for pure Python functions, not for wrapped C++ functions (as in pyfasttext).

So, you must use os.dup2(), e.g. see: https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/