diff --git a/include/cantera/cython/wrappers.h b/include/cantera/cython/wrappers.h index 9cc7acea5..69fc5f6ef 100644 --- a/include/cantera/cython/wrappers.h +++ b/include/cantera/cython/wrappers.h @@ -1,7 +1,10 @@ +#include "cantera/base/logger.h" #include "cantera/thermo/ThermoPhase.h" #include "cantera/transport/TransportBase.h" #include "cantera/kinetics/Kinetics.h" +#include "Python.h" + // Wrappers for preprocessor defines std::string get_cantera_version() { @@ -13,6 +16,23 @@ int get_sundials_version() return SUNDIALS_VERSION; } +class PythonLogger : public Cantera::Logger +{ +public: + virtual void write(const std::string& s) { + // 1000 bytes is the maximum size permitted by PySys_WriteStdout + static const size_t N = 999; + for (size_t i = 0; i < s.size(); i+=N) { + PySys_WriteStdout("%s", s.substr(i, N).c_str()); + } + } + + virtual void error(const std::string& msg) { + std::string err = "raise Exception('''"+msg+"''')"; + PyRun_SimpleString(err.c_str()); + } +}; + // Function which populates a 1D array #define ARRAY_FUNC(PREFIX, CLASS_NAME, FUNC_NAME) \ void PREFIX ## _ ## FUNC_NAME(Cantera::CLASS_NAME* object, double* data) \ diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 6c9996b5f..05353001c 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -505,6 +505,11 @@ cdef extern from "cantera/cython/wrappers.h": cdef string get_cantera_version() cdef int get_sundials_version() + cdef cppclass CxxPythonLogger "PythonLogger": + pass + + cdef void CxxSetLogger "setLogger" (CxxPythonLogger*) + # ThermoPhase composition cdef void thermo_getMassFractions(CxxThermoPhase*, double*) except + cdef void thermo_setMassFractions(CxxThermoPhase*, double*) except + diff --git a/interfaces/cython/cantera/utils.pyx b/interfaces/cython/cantera/utils.pyx index 5e4e1779a..ca92ed084 100644 --- a/interfaces/cython/cantera/utils.pyx +++ b/interfaces/cython/cantera/utils.pyx @@ -1,6 +1,9 @@ import sys cdef int _pythonMajorVersion = sys.version_info[0] +cdef CxxPythonLogger* _logger = new CxxPythonLogger() +CxxSetLogger(_logger) + cdef string stringify(x): """ Converts Python strings to std::string. """ # This method works with both Python 2.x and 3.x.