SWIG passes stream from python to C ++

C ++
I have a logger class in C ++. This registrar is heavily used in my library. The recorder allows you to set a standard STL stream for use as an output stream.

Python
A Python library using the "above SWIG-wrapped C ++ library" uses a standard python entry using StreamHandler. Sort of:

logger = logging.getLogger("base_logger") #create and set console handler ch = logging.StreamHandler() ch.stream = sys.stdout logger.addHandler(ch) 

How to pass python stream to C ++ library as STL stream using SWIG?

This way you can use C ++ to use your python stream. Sort of:

 ch = logger.handlers[NEEDED_HANDLER] Swig_wrapped_lib.set_stream(ch.stream) 
+4
source share
1 answer

From a technical point of view, you need to implement a swig (in) memory card that converts a python stream object to std :: ostream. However, I am afraid that this is very nontrivial.

+1
source

Source: https://habr.com/ru/post/1338332/


All Articles