SWIG pass stream from python to C++
C++
I have some logger class in C++. This logger is heavily used in my library. The logger allows to set standart STL stream to use as output stream.Python
Python library which uses "above SWIG wrapped C++ library" heavily uses python standard logging with StreamHandler. Somethi开发者_StackOverflowng like:logger = logging.getLogger("base_logger")
#create and set console handler
ch = logging.StreamHandler()
ch.stream = sys.stdout
logger.addHandler(ch)
How to pass a python stream to C++ library as STL stream using SWIG?
So one could make C++ to use your python stream. Something like:
ch = logger.handlers[NEEDED_HANDLER]
Swig_wrapped_lib.set_stream(ch.stream)
Technically, you need to implement a swig typemap (in) that converts the python stream object into an std::ostream. However, I fear that this is highly non-trivial.
精彩评论