开发者

SWIG pass stream from python to C++

开发者 https://www.devze.com 2023-02-09 00:31 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消