29 #ifdef TYTI_PYLOGHOOK_USE_BOOST 30 #include <boost/python.hpp> 32 #include <pybind11/pybind11.h> 40 #ifdef TYTI_PYLOGHOOK_USE_BOOST 42 inline boost::python::object LogHookMakeObject(T t)
44 return boost::python::object(boost::python::make_function(
46 boost::python::default_call_policies(),
47 boost::mpl::vector<void, const char*>()
52 inline pybind11::object LogHookMakeObject(T t)
54 return pybind11::cpp_function(std::forward<T>(t));
59 void redirect_syspipe(T t,
const char* pipename)
61 assert(Py_IsInitialized());
63 PyObject* out = PySys_GetObject(pipename);
69 if (out == NULL || out == Py_None)
71 std::string register_read_write = std::string(
"import sys\n\ 72 sys.") + pipename + std::string(
" = type(\"\",(object,),{\"write\":lambda self, txt: None, \"flush\":lambda self: None})()\n");
74 PyRun_SimpleString(register_read_write.c_str());
75 out = PySys_GetObject(pipename);
78 PyObject_SetAttrString(out,
"write",
79 detail::LogHookMakeObject(std::forward<T>(t)).ptr());
100 inline void redirect_stdout(T t)
102 detail::redirect_syspipe(std::forward<T>(t),
"stdout");
106 inline void redirect_stderr(T t)
108 detail::redirect_syspipe(std::forward<T>(t),
"stderr");