-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #587 from ami-iit/ros/text_logging
Implement the ROS2 sink for the TextLogging
- Loading branch information
Showing
20 changed files
with
379 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/RosLogger.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @file RosLogger.h | ||
* @authors Giulio Romualdi | ||
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#ifndef BIPEDAL_LOCOMOTION_BINDINGS_SYSTEM_ROS_LOGGER_H | ||
#define BIPEDAL_LOCOMOTION_BINDINGS_SYSTEM_ROS_LOGGER_H | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace BipedalLocomotion | ||
{ | ||
namespace bindings | ||
{ | ||
namespace TextLogging | ||
{ | ||
|
||
void CreateRosLoggerFactory(pybind11::module& module); | ||
|
||
} // namespace TextLogging | ||
} // namespace bindings | ||
} // namespace BipedalLocomotion | ||
|
||
#endif // BIPEDAL_LOCOMOTION_BINDINGS_SYSTEM_ROS_LOGGER_H |
18 changes: 18 additions & 0 deletions
18
bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/RosModule.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @file RosModule.h | ||
* @authors Giulio Romualdi | ||
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#ifndef BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_ROS_MODULE_H | ||
#define BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_ROS_MODULE_H | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace BipedalLocomotion::bindings::TextLogging | ||
{ | ||
void CreateRosModule(pybind11::module& module); | ||
} // namespace BipedalLocomotion::bindings::TextLogging | ||
|
||
#endif // BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_ROS_MODULE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @file RosLoggerFactory.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#include <pybind11/cast.h> | ||
#include <pybind11/chrono.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <BipedalLocomotion/TextLogging/RosLogger.h> | ||
#include <BipedalLocomotion/bindings/TextLogging/RosLogger.h> | ||
|
||
#include <string_view> | ||
|
||
namespace BipedalLocomotion | ||
{ | ||
namespace bindings | ||
{ | ||
namespace TextLogging | ||
{ | ||
|
||
void CreateRosLoggerFactory(pybind11::module& module) | ||
{ | ||
namespace py = ::pybind11; | ||
using namespace BipedalLocomotion::TextLogging; | ||
|
||
py::class_<RosLoggerFactory, // | ||
LoggerFactory, | ||
std::shared_ptr<RosLoggerFactory>>(module, | ||
"RosLoggerFactory") | ||
.def(py::init<const std::string_view&>(), py::arg("name")); | ||
} | ||
|
||
} // namespace TextLogging | ||
} // namespace bindings | ||
} // namespace BipedalLocomotion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @file RosModule.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
#include <BipedalLocomotion/bindings/TextLogging/RosModule.h> | ||
#include <BipedalLocomotion/bindings/TextLogging/RosLogger.h> | ||
|
||
namespace BipedalLocomotion::bindings::TextLogging | ||
{ | ||
void CreateRosModule(pybind11::module& module) | ||
{ | ||
CreateRosLoggerFactory(module); | ||
} | ||
} // namespace BipedalLocomotion::bindings::TextLogging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright (C) 2020 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
# This software may be modified and distributed under the terms of the | ||
# BSD-3-Clause license. | ||
|
||
if(FRAMEWORK_COMPILE_RosImplementation) | ||
|
||
add_bipedal_locomotion_library( | ||
NAME TextLoggingRosImplementation | ||
SOURCES src/RosLogger.cpp | ||
PUBLIC_HEADERS include/BipedalLocomotion/TextLogging/RosLogger.h | ||
PUBLIC_LINK_LIBRARIES BipedalLocomotion::TextLogging rclcpp::rclcpp | ||
INSTALLATION_FOLDER TextLogging) | ||
|
||
endif() |
123 changes: 123 additions & 0 deletions
123
src/TextLogging/RosImplementation/include/BipedalLocomotion/TextLogging/RosLogger.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* @file RosLogger.h | ||
* @authors Giulio Romualdi | ||
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the BSD-3-Clause license. | ||
*/ | ||
|
||
#ifndef BIPEDAL_LOCOMOTION_TEXT_LOGGING_ROS_LOGGER_H | ||
#define BIPEDAL_LOCOMOTION_TEXT_LOGGING_ROS_LOGGER_H | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <string> | ||
|
||
#include <spdlog/common.h> | ||
#include <spdlog/sinks/base_sink.h> | ||
|
||
#include <BipedalLocomotion/TextLogging/Logger.h> | ||
|
||
#include <rclcpp/logging.hpp> | ||
|
||
namespace BipedalLocomotion | ||
{ | ||
namespace TextLogging | ||
{ | ||
|
||
namespace sinks | ||
{ | ||
|
||
template <typename Mutex> class RosSink : public spdlog::sinks::base_sink<Mutex> | ||
{ | ||
public: | ||
RosSink(const rclcpp::Logger& rosLogger) | ||
: m_rosLogger(rosLogger) | ||
{ | ||
} | ||
|
||
private: | ||
rclcpp::Logger m_rosLogger; | ||
|
||
protected: | ||
void sink_it_(const spdlog::details::log_msg& msg) override | ||
{ | ||
// log_msg is a struct containing the log entry info like level, timestamp, thread id etc. | ||
// msg.raw contains pre formatted log | ||
spdlog::memory_buf_t formatted; | ||
|
||
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted); | ||
|
||
if (msg.level == spdlog::level::level_enum::trace) | ||
{ | ||
RCLCPP_INFO(m_rosLogger, formatted.data()); | ||
|
||
} else if (msg.level == spdlog::level::level_enum::debug) | ||
{ | ||
RCLCPP_DEBUG(m_rosLogger, formatted.data()); | ||
} else if (msg.level == spdlog::level::level_enum::info) | ||
{ | ||
RCLCPP_INFO(m_rosLogger, formatted.data()); | ||
} else if (msg.level == spdlog::level::level_enum::warn) | ||
{ | ||
RCLCPP_WARN(m_rosLogger, formatted.data()); | ||
} else if (msg.level == spdlog::level::level_enum::err) | ||
{ | ||
RCLCPP_ERROR(m_rosLogger, formatted.data()); | ||
} else | ||
{ | ||
RCLCPP_FATAL(m_rosLogger, formatted.data()); | ||
} | ||
} | ||
|
||
void flush_() override | ||
{ | ||
} | ||
}; | ||
|
||
using RosSink_mt = RosSink<std::mutex>; | ||
} // namespace sinks | ||
|
||
/** | ||
* RosLoggerFactory implements the factory you should use to enable the sink using ros. | ||
* The ROS logger can be easily used as follows | ||
* \code{.cpp} | ||
* #include <BipedalLocomotion/TextLogging/Logger.h> | ||
* #include <BipedalLocomotion/TextLogging/RosLogger.h> | ||
* #include <BipedalLocomotion/TextLogging/LoggerBuilder.h> | ||
* | ||
* // Change the logger | ||
* BipedalLocomotion::TextLogging::LoggerBuilder::setFactory(std::make_shared<BipedalLocomotion::TextLogging::RosLoggerFactory>())); | ||
* | ||
* BipedalLocomotion::log()->info("My info"); | ||
* \endcode | ||
*/ | ||
class RosLoggerFactory final : public LoggerFactory | ||
{ | ||
public: | ||
/** | ||
* Construct a new RosLoggerFactory object | ||
* @param name the name of the logger which will be used inside the formatted messages | ||
*/ | ||
RosLoggerFactory(const std::string_view& name = "blf"); | ||
|
||
/** | ||
* Construct a new RosLoggerFactory object | ||
* @param name the name of the logger which will be used inside the formatted messages | ||
*/ | ||
RosLoggerFactory(const rclcpp::Logger& logger); | ||
|
||
/** | ||
* Create the ROSLogger as a singleton | ||
* @return the pointer to TextLogging::Logger that streams the output using ROS | ||
*/ | ||
std::shared_ptr<TextLogging::Logger> const createLogger() final; | ||
|
||
private: | ||
const std::string m_name; /** The name of the logger */ | ||
const rclcpp::Logger m_rosLogger; /** Associated ros logger */ | ||
}; | ||
|
||
} // namespace TextLogging | ||
} // namespace BipedalLocomotion | ||
|
||
#endif // BIPEDAL_LOCOMOTION_TEXT_LOGGING_ROS_LOGGER_H |
Oops, something went wrong.