Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
KerstinKeller committed Feb 14, 2025
1 parent dca99e2 commit 1d681a7
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 517 deletions.
38 changes: 38 additions & 0 deletions lang/python/nanobind_core/samples/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2025 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ========================= eCAL LICENSE =================================
import ecal.nanobind_core as ecal_core


def main():
ecal_core.initialize('logging', ecal_core.ALL)

ecal_core.log(ecal_core.LogLevel.INFO, "Hello Hello")
ecal_core.log(ecal_core.LogLevel.WARNING, "Help")


all_logging = ecal_core.get_logging()

for log in all_logging.log_messages:
print(log)

ecal_core.finalize()



if __name__ == "__main__":
main()
31 changes: 27 additions & 4 deletions lang/python/nanobind_core/src/core/pubsub/py_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <core/pubsub/py_subscriber.h>
#include <ecal/pubsub/subscriber.h>

#include <nanobind/stl/function.h>

#include <exception>

namespace nb = nanobind;
Expand All @@ -35,24 +37,45 @@ using namespace eCAL;
void AddPubsubSubscriber(nanobind::module_& module)
{
// --- Wrap the CSubscriber class ------------------------------------------------
// TODO: Currently, providing the callback as last argument leads to runtime errors, so this needs to be adresses.
nb::class_<CSubscriber>(module, "Subscriber")
// Define CPublisher class
.def("__init__", [](CSubscriber* t, nb::str topic_name, const SDataTypeInformation& datatype_info/*, const PubEventCallbackT& event_callback_*/, const Subscriber::Configuration& config_) { new (t) CSubscriber(topic_name.c_str(), datatype_info/*, event_callback_*/, config_); },
.def("__init__", [](CSubscriber* t, nb::str topic_name, const SDataTypeInformation& datatype_info, const Subscriber::Configuration& config_/*, const nb::callable& event_callback_*/)
{
SubEventCallbackT event_callback_cpp = nullptr;
//if (!event_callback_.is_none())
//{
// event_callback_cpp = [event_callback_](auto&&... args) {
// try {
// nb::gil_scoped_acquire acquire;
// // Call the Python callback, forwarding the arguments.
// event_callback_(std::forward<decltype(args)>(args)...);
// }
// catch (std::exception e)
// {
// std::cout << "Error invoking callback: " << e.what() << std::endl;
// }
// };
//}
new (t) CSubscriber(topic_name.c_str(), datatype_info, event_callback_cpp, config_);
},
nb::arg("topic_name"),
nb::arg("data_type_info") = SDataTypeInformation(),
nb::arg("config") = GetSubscriberConfiguration()
//nb::arg("event_callback") = nullptr
nb::arg("config") = GetSubscriberConfiguration()/*,
nb::arg("event_callback") = nb::none()*/
)

.def("set_receive_callback",
[](CSubscriber& self, nb::object py_callback) {
[](CSubscriber& self, nb::callable py_callback) {
// Wrap the Python callback with a lambda that acquires the GIL.
// Adjust the callback parameters according to the actual signature of ReceiveCallbackT.
auto wrapped_callback = [py_callback](auto&&... args) {
//auto wrapped_callback = [py_callback](const STopicId& publisher_id_, const SDataTypeInformation& data_type_info_, const SReceiveCallbackData& data_) {
try {
nb::gil_scoped_acquire acquire;
// Call the Python callback, forwarding the arguments.
py_callback(std::forward<decltype(args)>(args)...);
//py_callback(nb::cast(publisher_id_), nb::cast(data_type_info_), nb::cast(data_));
}
catch (std::exception e)
{
Expand Down
14 changes: 7 additions & 7 deletions lang/python/nanobind_core/src/core/pubsub/py_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ void AddPubsubTypes(nanobind::module_& module)
return self < other;
},
"Less-than comparison")
.def("__repr__",
[](const eCAL::STopicId& self) {
std::ostringstream oss;
oss << self; // uses the provided operator<< overload
return oss.str();
},
"Return the string representation of STopicId");
.def("__repr__",
[](const eCAL::STopicId& self) {
std::ostringstream oss;
oss << self; // uses the provided operator<< overload
return oss.str().c_str();
},
"Return the string representation of TopicId");

//-------------------------------------------------------------------------
// Bind SReceiveCallbackData struct
Expand Down
112 changes: 47 additions & 65 deletions lang/python/nanobind_core/src/core/py_log.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,47 @@
///* ========================= eCAL LICENSE =================================
// *
// * Copyright (C) 2016 - 2025 Continental Corporation
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// *
// * ========================= eCAL LICENSE =================================
//*/
//
///**
// * @file log.h
// * @brief eCAL logging interface
//**/
//
//#pragma once
//
//#include <ecal/os.h>
//#include <ecal/log_level.h>
//#include <ecal/types/logging.h>
//
//#include <list>
//#include <string>
//
//namespace eCAL
//{
// namespace Logging
// {
// /**
// * @brief Log a message.
// *
// * @param level_ The level.
// * @param msg_ The log message string.
// **/
// ECAL_API void Log(eLogLevel level_, const std::string& msg_);
//
// /**
// * @brief Get logging as serialized protobuf string.
// *
// * @param [out] log_ String to store the logging information.
// *
// * @return True if succeeded.
// **/
// ECAL_API bool GetLogging(std::string& log_);
//
// /**
// * @brief Get logging as struct.
// *
// * @param [out] log_ Target struct to store the logging information.
// *
// * @return True if succeeded.
// **/
// ECAL_API bool GetLogging(Logging::SLogging& log_);
// }
//}
//
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2025 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

/**
* @file log.h
* @brief eCAL logging interface
**/

#include <core/py_log.h>
#include <ecal/log.h>

#include <optional>

#include <nanobind/stl/optional.h>
#include <nanobind/stl/string.h>

namespace nb = nanobind;
using namespace eCAL::Logging;

void AddLog(nanobind::module_& module)
{
module.def("log", &Log, "Log a message", nb::arg("level"), nb::arg("message"));

module.def("get_logging", []() -> std::optional<SLogging> {
SLogging log_output;
if (GetLogging(log_output)) return log_output;
return std::nullopt;
}, "Get logging as Logging | None");
}


92 changes: 27 additions & 65 deletions lang/python/nanobind_core/src/core/py_log.h
Original file line number Diff line number Diff line change
@@ -1,65 +1,27 @@
///* ========================= eCAL LICENSE =================================
// *
// * Copyright (C) 2016 - 2025 Continental Corporation
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// *
// * ========================= eCAL LICENSE =================================
//*/
//
///**
// * @file log.h
// * @brief eCAL logging interface
//**/
//
//#pragma once
//
//#include <ecal/os.h>
//#include <ecal/log_level.h>
//#include <ecal/types/logging.h>
//
//#include <list>
//#include <string>
//
//namespace eCAL
//{
// namespace Logging
// {
// /**
// * @brief Log a message.
// *
// * @param level_ The level.
// * @param msg_ The log message string.
// **/
// ECAL_API void Log(eLogLevel level_, const std::string& msg_);
//
// /**
// * @brief Get logging as serialized protobuf string.
// *
// * @param [out] log_ String to store the logging information.
// *
// * @return True if succeeded.
// **/
// ECAL_API bool GetLogging(std::string& log_);
//
// /**
// * @brief Get logging as struct.
// *
// * @param [out] log_ Target struct to store the logging information.
// *
// * @return True if succeeded.
// **/
// ECAL_API bool GetLogging(Logging::SLogging& log_);
// }
//}
//
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2025 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

/**
* @file log.h
* @brief eCAL logging interface
**/

#include <nanobind/nanobind.h>

void AddLog(nanobind::module_& module);
Loading

0 comments on commit 1d681a7

Please sign in to comment.