From 95c3318b8c1fff1d207854bb827ef9a4862cbe44 Mon Sep 17 00:00:00 2001 From: Tim Clephas Date: Fri, 3 Mar 2023 12:19:03 +0100 Subject: [PATCH] Log missing pairs only once Signed-off-by: Tim Clephas --- src/dynamic_bridge.cpp | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/dynamic_bridge.cpp b/src/dynamic_bridge.cpp index 1e1a5522..05811fe1 100644 --- a/src/dynamic_bridge.cpp +++ b/src/dynamic_bridge.cpp @@ -192,13 +192,17 @@ void update_bridge( bridge.ros1_type_name, topic_name, 10, bridge.ros2_type_name, topic_name, ros2_publisher_qos); } catch (std::runtime_error & e) { - fprintf( - stderr, - "failed to create 1to2 bridge for topic '%s' " - "with ROS 1 type '%s' and ROS 2 type '%s': %s\n", - topic_name.c_str(), bridge.ros1_type_name.c_str(), bridge.ros2_type_name.c_str(), e.what()); - if (std::string(e.what()).find("No template specialization") != std::string::npos) { - fprintf(stderr, "check the list of supported pairs with the `--print-pairs` option\n"); + static std::set logged_topic_errors; + auto entry = logged_topic_errors.emplace(topic_name); + if (entry.second) { // topic name was not already in the set, so log once + fprintf( + stderr, + "failed to create 1to2 bridge for topic '%s' " + "with ROS 1 type '%s' and ROS 2 type '%s': %s\n", + topic_name.c_str(), bridge.ros1_type_name.c_str(), bridge.ros2_type_name.c_str(), e.what()); + if (std::string(e.what()).find("No template specialization") != std::string::npos) { + fprintf(stderr, "check the list of supported pairs with the `--print-pairs` option\n"); + } } continue; } @@ -258,13 +262,17 @@ void update_bridge( bridge.ros2_type_name, topic_name, 10, bridge.ros1_type_name, topic_name, 10); } catch (std::runtime_error & e) { - fprintf( - stderr, - "failed to create 2to1 bridge for topic '%s' " - "with ROS 2 type '%s' and ROS 1 type '%s': %s\n", - topic_name.c_str(), bridge.ros2_type_name.c_str(), bridge.ros1_type_name.c_str(), e.what()); - if (std::string(e.what()).find("No template specialization") != std::string::npos) { - fprintf(stderr, "check the list of supported pairs with the `--print-pairs` option\n"); + static std::set logged_topic_errors; + auto entry = logged_topic_errors.emplace(topic_name); + if (entry.second) { // topic name was not already in the set, so log once + fprintf( + stderr, + "failed to create 2to1 bridge for topic '%s' " + "with ROS 2 type '%s' and ROS 1 type '%s': %s\n", + topic_name.c_str(), bridge.ros2_type_name.c_str(), bridge.ros1_type_name.c_str(), e.what()); + if (std::string(e.what()).find("No template specialization") != std::string::npos) { + fprintf(stderr, "check the list of supported pairs with the `--print-pairs` option\n"); + } } continue; }