Skip to content

Commit

Permalink
Make LogManager class accessible by pybind11
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Apr 14, 2023
1 parent 9c1f4b4 commit 90b72d4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
13 changes: 6 additions & 7 deletions include/hal_core/python_bindings/python_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@

namespace hal
{
/**
* Wrapper for LogManager::set_level_of_channel()
* @param channel_name - Name of log channel
* @param level - One of trace, debug, info, warn, err, critical, off
*/
void set_log_level_of_channel(std::string channel_name, std::string level);

namespace py = pybind11;

/**
Expand Down Expand Up @@ -326,6 +319,12 @@ namespace hal
*/
void netlist_modification_decorator_init(py::module& m);

/**
* Initializes Python bindings for the HAL LogManager in a python module.
*
* @param[in] m - the python module
*/
void log_init(py::module& m);
/**
* @}
*/
Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/src/python/python_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace hal {
: QThread(parent), mScript(script), mSingleStatement(singleStatement),
mAbortRequested(false), mSpamCount(0)
{
qDebug() << "+++PythonThread" << hex << (quintptr) this;
// qDebug() << "+++PythonThread" << hex << (quintptr) this;
}

PythonThread::~PythonThread()
{
qDebug() << "---PythonThread" << hex << (quintptr) this;
// qDebug() << "---PythonThread" << hex << (quintptr) this;
}

void PythonThread::run()
Expand Down
41 changes: 41 additions & 0 deletions src/python_bindings/bindings/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "hal_core/python_bindings/python_bindings.h"
#include "hal_core/utilities/log.h"
#include <iostream>

namespace hal
{
void log_init(py::module& m)
{
py::class_<LogManager, RawPtrWrapper<LogManager>>(m, "LogManager")
.def(py::init([](){
LogManager* lm = LogManager::get_instance();
return RawPtrWrapper<LogManager>(lm); }))

.def("set_level_of_channel", &LogManager::set_level_of_channel, py::arg("channel_name"), py::arg("level"), R"(
Set a channel's severity level.
:param str channel_name: Name of the channel.
:param str level: The severity level, one out of {}.
)")

.def_static("get_channel", [](std::string channel_name) { LogManager::get_instance()->get_channel(channel_name); } , py::arg("channel_name"), R"(
Main purpose of get_channel() method in PYTHON is to create channel if not existing.
:param str channel_name: Name of the channel.
)")

.def("get_channels", &LogManager::get_channels, R"(
Returns all channels' names.
:returns: Set of channel names.
:rtype: set[str]
)")

.def("get_available_log_levels", &LogManager::get_available_log_levels, R"(
Get all available severity levels.
:returns: Set of severity levels.
:rtype: set[str]
)");
}
}
16 changes: 2 additions & 14 deletions src/python_bindings/python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

namespace hal
{
void set_log_level_of_channel(std::string channel_name, std::string level)
{
LogManager* lm = LogManager::get_instance();
log_info(channel_name, "Set log level of channel '{}' to '{}'.", channel_name, level);
if (lm) lm->set_level_of_channel(channel_name,level);
}

#ifdef PYBIND11_MODULE
PYBIND11_MODULE(hal_py, m)
{
Expand All @@ -22,13 +15,6 @@ namespace hal
m.def(
"log_info", [](std::string& message) { log_info("python_context", message); }, R"( some documentation info)");

m.def("set_log_level_of_channel", set_log_level_of_channel, py::arg("channel_name"), py::arg("level"), R"(
Set log level for channel.
:param str channel_name: Name of channel.
:param str level: Selected level, one out of [trace, debug, info, warn, err, critical, off].
)");

data_container_init(m);

core_utils_init(m);
Expand Down Expand Up @@ -89,6 +75,8 @@ namespace hal

netlist_modification_decorator_init(m);

log_init(m);

#ifndef PYBIND11_MODULE
return m.ptr();
#endif // PYBIND11_MODULE
Expand Down

0 comments on commit 90b72d4

Please sign in to comment.