Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bindings/SofaRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ project(Bindings.SofaRuntime)

set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaRuntime/Module_SofaRuntime.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaRuntime/PythonMessageHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaRuntime/Timer/Submodule_Timer.cpp
)

set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaRuntime/PythonMessageHandler.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaRuntime/Timer/Submodule_Timer.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/SofaRuntime/Timer/Submodule_Timer_doc.h
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ using sofapython3::SceneLoaderPY3;
#include <sofa/core/logging/PerComponentLoggingMessageHandler.h>
using sofa::helper::logging::MessageDispatcher;
using sofa::helper::logging::MainPerComponentLoggingMessageHandler;
using sofa::helper::logging::MainConsoleMessageHandler;
#include <SofaPython3/SofaRuntime/PythonMessageHandler.h>

#include <sofa/core/init.h>
#include <sofa/helper/init.h>
Expand Down Expand Up @@ -155,9 +155,9 @@ PYBIND11_MODULE(SofaRuntime, m) {

m.def("init", []() {
MessageDispatcher::clearHandlers();
MessageDispatcher::addHandler(&MainConsoleMessageHandler::getInstance());
MessageDispatcher::addHandler(&MainPythonMessageHandler::getInstance());
MessageDispatcher::addHandler(&MainPerComponentLoggingMessageHandler::getInstance());
});
}, "redirect SOFA messages to Python's sys.stdout");

m.add_object("DataRepository", py::cast(&sofa::helper::system::DataRepository));
m.add_object("PluginRepository", py::cast(&sofa::helper::system::PluginRepository));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <SofaPython3/SofaRuntime/PythonMessageHandler.h>
#include <pybind11/pybind11.h>
#include <sofa/helper/logging/Message.h>

namespace sofapython3
{

namespace
{

using namespace std::string_literals;
const std::string red { "\033[31m"s };
const std::string green { "\033[32m"s };
const std::string orange { "\033[38;5;214m"s };
const std::string magenta { "\033[35m"s };
const std::string blue { "\033[34m"s };
const std::string reset { "\033[0m"s };

std::string format(const std::string& type, const std::string& color)
{
return color + "[" + type + "] " + reset;
};

const std::string& getPrefixText(sofa::helper::logging::Message::Type type)
{
static const std::string advice = format("SUGGESTION", green);
static const std::string deprecated = format("DEPRECATED", orange);
static const std::string warning = format("WARNING", orange);
static const std::string info = format("INFO", green);
static const std::string error = format("ERROR", red);
static const std::string fatal = format("FATAL", magenta);
static const std::string empty = format("EMPTY", reset);
static const std::string nothing{};

switch (type)
{
case sofa::helper::logging::Message::Advice : return advice;
case sofa::helper::logging::Message::Deprecated : return deprecated;
case sofa::helper::logging::Message::Warning : return warning;
case sofa::helper::logging::Message::Info : return info;
case sofa::helper::logging::Message::Error : return error;
case sofa::helper::logging::Message::Fatal : return fatal;
case sofa::helper::logging::Message::TEmpty : return empty;

case sofa::helper::logging::Message::TypeCount:
return nothing;
}
}

}

namespace py { using namespace pybind11; }

void PythonMessageHandler::process(sofa::helper::logging::Message &m)
{
if (!m.sender().empty())
{
py::print(getPrefixText(m.type()), format(m.sender(), blue), m.messageAsString());
}
else
{
py::print(getPrefixText(m.type()), m.messageAsString());
}
}

PythonMessageHandler& MainPythonMessageHandler::getInstance()
{
static PythonMessageHandler s_instance;
return s_instance;
}

} // namespace sofapython3

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once

#include <sofa/helper/logging/MessageHandler.h>

namespace sofapython3
{

class PythonMessageHandler : public sofa::helper::logging::MessageHandler
{
public:
PythonMessageHandler() = default;
void process(sofa::helper::logging::Message& m) override ;
};

class MainPythonMessageHandler
{
public:
static PythonMessageHandler& getInstance() ;
};
} // namespace sofapython3
Loading