Skip to content

Commit bfafe80

Browse files
committed
ADD ConsoleMessageHandler at simu init
1 parent 9822063 commit bfafe80

File tree

4 files changed

+206
-1
lines changed

4 files changed

+206
-1
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
#pragma once
29+
30+
#include <sofa/helper/logging/MessageHandler.h>
31+
#include <pybind11/pybind11.h>
32+
33+
/// Forward declaration
34+
namespace sofa {
35+
namespace helper {
36+
namespace logging {
37+
class Message;
38+
}
39+
}
40+
}
41+
42+
namespace sofapython3
43+
{
44+
45+
/// Makes an alias for the pybind11 namespace to increase readability.
46+
namespace py { using namespace pybind11; }
47+
48+
using namespace pybind11::literals;
49+
using sofa::helper::logging::Message ;
50+
using sofa::helper::logging::MessageHandler ;
51+
52+
class PyMessageHandler: public MessageHandler
53+
{
54+
public:
55+
virtual void process(Message& m) override ;
56+
57+
PyMessageHandler();
58+
~PyMessageHandler() override;
59+
};
60+
61+
void moduleAddMessageHandler(py::module &m);
62+
63+
} /// namespace sofapython3
64+

bindings/Sofa/src/SofaPython3/Sofa/Helper/Binding_MessageHandler_doc.h

Whitespace-only changes.

bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ using namespace pybind11::literals;
4040
using sofa::simulation::Simulation;
4141

4242
#include <sofa/core/visual/VisualParams.h>
43+
#include <sofa/helper/logging/MessageDispatcher.h>
44+
#include <sofa/helper/logging/ConsoleMessageHandler.h>
4345
#include "Submodule_Simulation_doc.h"
4446

4547
namespace py = pybind11;
@@ -56,7 +58,11 @@ PYBIND11_MODULE(Simulation, simulation)
5658

5759
simulation.def("print", [](Node* n){ sofa::simulation::getSimulation()->print(n); }, sofapython3::doc::simulation::print);
5860
simulation.def("animate", [](Node* n, SReal dt=0.0){ sofa::simulation::getSimulation()->animate(n, dt); },sofapython3::doc::simulation::animate);
59-
simulation.def("init", [](Node* n){ sofa::simulation::getSimulation()->init(n); }, sofapython3::doc::simulation::init);
61+
simulation.def("init", [](Node* n){
62+
sofa::simulation::getSimulation()->init(n);
63+
MessageDispatcher::clearHandlers();
64+
MessageDispatcher::addHandler(new sofa::helper::logging::ConsoleMessageHandler());
65+
}, sofapython3::doc::simulation::init);
6066
simulation.def("initVisual", [](Node* n){ n->getVisualLoop()->initStep(sofa::core::visual::VisualParams::defaultInstance()); });
6167
simulation.def("reset", [](Node* n){ sofa::simulation::getSimulation()->reset(n); }, sofapython3::doc::simulation::reset);
6268
simulation.def("load", [](const std::string & name) {

0 commit comments

Comments
 (0)