|
| 1 | +/********************************************************************* |
| 2 | +Copyright 2019, CNRS, University of Lille, INRIA |
| 3 | +
|
| 4 | +This file is part of sofaPython3 |
| 5 | +
|
| 6 | +sofaPython3 is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +sofaPython3 is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*********************************************************************/ |
| 19 | +/******************************************************************** |
| 20 | + Contributors: |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +********************************************************************/ |
| 27 | + |
| 28 | +#include "Binding_MessageHandler.h" |
| 29 | +#include "Binding_MessageHandler_doc.h" |
| 30 | + |
| 31 | +#include <SofaPython3/DataHelper.h> |
| 32 | +#include <SofaPython3/PythonFactory.h> |
| 33 | +#include <SofaPython3/PythonEnvironment.h> |
| 34 | +using sofapython3::PythonEnvironment; |
| 35 | + |
| 36 | +#include <pybind11/pybind11.h> |
| 37 | + |
| 38 | +#include <sofa/core/objectmodel/Base.h> |
| 39 | + |
| 40 | +PYBIND11_DECLARE_HOLDER_TYPE(PyMessageHandler, |
| 41 | + sofapython3::py_shared_ptr<PyMessageHandler>, true) |
| 42 | + |
| 43 | +namespace sofapython3 |
| 44 | +{ |
| 45 | + using sofa::core::objectmodel::Event; |
| 46 | + |
| 47 | + void PyMessageHandler::process(Message& /*m*/) { |
| 48 | + } |
| 49 | + |
| 50 | + PyMessageHandler::PyMessageHandler() { |
| 51 | + } |
| 52 | + |
| 53 | + PyMessageHandler::~PyMessageHandler() { |
| 54 | + } |
| 55 | + |
| 56 | + class MessageHandler_Trampoline : public PyMessageHandler, public PythonTrampoline |
| 57 | + { |
| 58 | + public: |
| 59 | + MessageHandler_Trampoline() = default; |
| 60 | + |
| 61 | + ~MessageHandler_Trampoline() override = default; |
| 62 | + virtual void process(Message& m) override ; |
| 63 | + }; |
| 64 | + |
| 65 | + void MessageHandler_Trampoline::process(Message& m) |
| 66 | + { |
| 67 | + PythonEnvironment::gil acquire {"MessageHandler"}; |
| 68 | + py::object self = py::cast(this); |
| 69 | + |
| 70 | + if( py::hasattr(self, "process") ) |
| 71 | + { |
| 72 | + auto typeStr = [&m]() -> std::string { |
| 73 | + switch (m.type()) |
| 74 | + { |
| 75 | + case Message::Type::Info: |
| 76 | + return "Info"; |
| 77 | + case Message::Type::Error: |
| 78 | + return "Error"; |
| 79 | + case Message::Type::Fatal: |
| 80 | + return "Fatal"; |
| 81 | + case Message::Type::Advice: |
| 82 | + return "Advice"; |
| 83 | + case Message::Type::Warning: |
| 84 | + return "Warning"; |
| 85 | + case Message::Type::Deprecated: |
| 86 | + return "Deprecated"; |
| 87 | + default: |
| 88 | + return "Other"; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + auto component = [&m]() { |
| 93 | + sofa::helper::logging::SofaComponentInfo* nfo = dynamic_cast<sofa::helper::logging::SofaComponentInfo*>(m.componentInfo().get()); |
| 94 | + if( nfo != nullptr ) |
| 95 | + return py::cast(nfo->m_component); |
| 96 | + return py::object(); |
| 97 | + }; |
| 98 | + |
| 99 | + py::dict msg("type"_a=typeStr, |
| 100 | + "isEmpty"_a=m.empty(), |
| 101 | + "sender"_a=m.sender(), |
| 102 | + "message"_a=m.messageAsString(), |
| 103 | + "component"_a=component |
| 104 | + ); |
| 105 | + |
| 106 | + py::object fct = self.attr("process")(msg); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + void moduleAddMessageHandler(py::module &m) { |
| 112 | + py::class_<PyMessageHandler, |
| 113 | + MessageHandler_Trampoline, |
| 114 | + std::unique_ptr<PyMessageHandler>> f(m, "MessageHandler", |
| 115 | + py::dynamic_attr(), |
| 116 | + py::multiple_inheritance()); |
| 117 | + |
| 118 | + f.def(py::init([]() |
| 119 | + { |
| 120 | + return new MessageHandler_Trampoline(); |
| 121 | + })); |
| 122 | + |
| 123 | + f.def("process", &PyMessageHandler::process); |
| 124 | + f.def("__enter__", [](PyMessageHandler* self){ |
| 125 | + PythonEnvironment::gil acquire {"MessageHandler"}; |
| 126 | + sofa::helper::logging::MessageDispatcher::addHandler(self); |
| 127 | + }); |
| 128 | + f.def("__exit__", [](PyMessageHandler* self){ |
| 129 | + PythonEnvironment::gil acquire {"MessageHandler"}; |
| 130 | + sofa::helper::logging::MessageDispatcher::rmHandler(self); |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | +} |
0 commit comments