Skip to content

Commit

Permalink
Implement a constructor that take as input an r-value rclpcpp::Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRomualdi committed Feb 3, 2023
1 parent a51e6ce commit d3eaee4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file RosLogger.h
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

Expand All @@ -17,7 +17,7 @@

#include <BipedalLocomotion/TextLogging/Logger.h>

#include <rclcpp/rclcpp.hpp>
#include <rclcpp/logging.hpp>

namespace BipedalLocomotion
{
Expand All @@ -36,7 +36,7 @@ template <typename Mutex> class RosSink : public spdlog::sinks::base_sink<Mutex>
}

private:
rclcpp::Logger m_rosLogger;
const rclcpp::Logger m_rosLogger;

protected:
void sink_it_(const spdlog::details::log_msg& msg) override
Expand Down Expand Up @@ -77,9 +77,8 @@ template <typename Mutex> class RosSink : public spdlog::sinks::base_sink<Mutex>
using RosSink_mt = RosSink<std::mutex>;
} // namespace sinks


/**
* RosLoggerFactory implements the factory you should use to enable the sink using yaro.
* RosLoggerFactory implements the factory you should use to enable the sink using ros2.
* The ROS logger can be easily used as follows
* \code{.cpp}
* #include <BipedalLocomotion/TextLogging/Logger.h>
Expand All @@ -103,19 +102,30 @@ class RosLoggerFactory final : public LoggerFactory

/**
* Construct a new RosLoggerFactory object
* @param name the name of the logger which will be used inside the formatted messages
* @param logger a const ref to rclpcpp::Logger object for instance the output of
* `node->get_logger()`
*/
RosLoggerFactory(const rclcpp::Logger& logger);

/**
* Construct a new RosLoggerFactory object
* @param logger r-value rclpcpp::Logger object for instance the output of `node->get_logger()`.
*/
RosLoggerFactory(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 */
// the order matters here see
// https://stackoverflow.com/questions/1242830/constructor-initialization-list-evaluation-order

const rclcpp::Logger m_rosLogger; /** Associated ros logger */

const std::string m_name; /** The name of the logger */
};

} // namespace TextLogging
Expand Down
24 changes: 16 additions & 8 deletions src/TextLogging/RosImplementation/src/RosLogger.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/**
* @file RosLogger.cpp
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* @copyright 2023 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the BSD-3-Clause license.
*/

#include <mutex>
#include <utility>

#include <BipedalLocomotion/TextLogging/RosLogger.h>

#include <rclcpp/rclcpp.hpp>
#include <rclcpp/logging.hpp>

namespace BipedalLocomotion
{

template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<TextLogging::Logger> RosSink_mt(const std::string& loggerName, const rclcpp::Logger& rosLogger)
inline std::shared_ptr<TextLogging::Logger>
RosSink_mt(const std::string& loggerName, const rclcpp::Logger& rosLogger)
{
return Factory::template create<TextLogging::sinks::RosSink_mt>(loggerName, rosLogger);
}
Expand All @@ -42,20 +44,26 @@ _createLogger(const std::string& name, const rclcpp::Logger& rosLogger)
#endif // NDEBUG

// set the custom pattern
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [thread: %t] [%n] %v");
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [thread: %t] %v");
}
return logger;
}

TextLogging::RosLoggerFactory::RosLoggerFactory(const std::string_view& name)
: m_name{name}
, m_rosLogger{rclcpp::get_logger(name.data())}
: m_rosLogger{rclcpp::get_logger(name.data())}
, m_name{m_rosLogger.get_name()}
{
}

TextLogging::RosLoggerFactory::RosLoggerFactory(const rclcpp::Logger& logger)
: m_name{logger.get_name()}
, m_rosLogger{logger}
: m_rosLogger{std::forward(logger)}
, m_name{m_rosLogger.get_name()}
{
}

TextLogging::RosLoggerFactory::RosLoggerFactory(rclcpp::Logger&& logger)
: m_rosLogger{std::forward(logger)}
, m_name{m_rosLogger.get_name()}
{
}

Expand Down

0 comments on commit d3eaee4

Please sign in to comment.