Dmitry Romanov
Dmitry Romanov

Reputation: 14090

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. Something 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)

Upvotes: 4

Views: 1115

Answers (1)

Martin
Martin

Reputation: 12598

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.

Upvotes: 1

Related Questions