ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
5

Where are ros2 message ostream operators for c++ logging?

asked 2020-10-29 13:17:39 -0500

updated 2020-10-30 06:24:31 -0500

Hello. I am trying to use RCLCPP_XX_STREAM function passing directly a message to the stream. The problem is that I am getting compilation errors because the message ostream operators are not found.

Below I show an example:

#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
[...]
geometry_msgs::msg::PoseStamped poseMsg = [...];
RCLCPP_INFO_STREAM(get_logger(), "posevalue:" << poseMsg);
[...]

Error:

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘geometry_msgs::msg::PoseStamped_<std::allocator<void> >::_pose_type’ {aka ‘geometry_msgs::msg::Pose_<std::allocator<void> >’})

Where are rclcpp message ostream operators for logging?

Are they available in ROS2? if not, is it planned to support them?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2022-02-17 18:45:58 -0500

m.anderson gravatar image

updated 2023-07-12 12:04:06 -0500

UPDATE: It seems the original method below is now deprecated in Humble. You should now use geometry_msgs::msg::to_yaml() instead of rosidl_generator_traits::to_yaml()

warning: ‘std::string rosidl_generator_traits::to_yaml(const Pose&)’ is deprecated: use 
geometry_msgs::msg::to_yaml() instead [-Wdeprecated-declarations]

So, the answer for Humble is

#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
[...]
geometry_msgs::msg::PoseStamped poseMsg = [...];
RCLCPP_INFO_STREAM(get_logger(), "posevalue:" << geometry_msgs::msg::(poseMsg));
[...]

ORIGINAL: I had the same issue... here's how to fix it:
use rosidl_generator_traits::to_yaml(poseMsg) instead of just poseMsg

#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
[...]
geometry_msgs::msg::PoseStamped poseMsg = [...];
RCLCPP_INFO_STREAM(get_logger(), "posevalue:" << rosidl_generator_traits::to_yaml(poseMsg));
[...]

I dug through the generated message header files and found it.

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2020-10-29 13:17:39 -0500

Seen: 1,444 times

Last updated: Jul 12 '23