From e30f31551e2f4731835deca51c043ae5cf099c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Mart=C3=ADn=20Rico?= Date: Sun, 28 Oct 2018 02:35:17 +0200 Subject: [PATCH] issue a warning if publishing on a not active publisher (#574) * issue a warning if publishing on a not active publisher * Adding a logger private member in LifecyclePublisher for avoiding creating a new one echa call --- rclcpp/resource/logging.hpp.em | 3 +- .../rclcpp_lifecycle/lifecycle_publisher.hpp | 29 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/rclcpp/resource/logging.hpp.em b/rclcpp/resource/logging.hpp.em index b6a869f..89a22d7 100644 --- a/rclcpp/resource/logging.hpp.em +++ b/rclcpp/resource/logging.hpp.em @@ -91,7 +91,8 @@ def is_supported_feature_combination(feature_combination): */ #define RCLCPP_@(severity)@(suffix)(logger, @(''.join([p + ', ' for p in get_macro_parameters(feature_combination).keys()]))...) \ static_assert( \ - ::std::is_same::type, ::rclcpp::Logger>::value, \ + ::std::is_same::type, \ + typename ::rclcpp::Logger>::value, \ "First argument to logging macros must be an rclcpp::Logger"); \ RCUTILS_LOG_@(severity)@(suffix)_NAMED( \ @{params = get_macro_parameters(feature_combination).keys()}@ diff --git a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_publisher.hpp b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_publisher.hpp index 526cd62..867714e 100644 --- a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_publisher.hpp +++ b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_publisher.hpp @@ -20,6 +20,8 @@ #include "rclcpp/publisher.hpp" +#include "rclcpp/logging.hpp" + namespace rclcpp_lifecycle { /// base class with only @@ -62,8 +64,10 @@ public: std::shared_ptr allocator) : rclcpp::Publisher( node_base, topic, publisher_options, allocator), - enabled_(false) - {} + enabled_(false), + logger_(rclcpp::get_logger("LifecyclePublisher")) + { + } ~LifecyclePublisher() {} @@ -77,6 +81,10 @@ public: publish(std::unique_ptr & msg) { if (!enabled_) { + RCLCPP_WARN(logger_, + "Trying to publish message on the topic '%s', but the publisher is not activated", + this->get_topic_name()); + return; } rclcpp::Publisher::publish(msg); @@ -92,6 +100,10 @@ public: publish(const std::shared_ptr & msg) { if (!enabled_) { + RCLCPP_WARN(logger_, + "Trying to publish message on the topic '%s', but the publisher is not activated", + this->get_topic_name()); + return; } rclcpp::Publisher::publish(msg); @@ -107,6 +119,10 @@ public: publish(std::shared_ptr msg) { if (!enabled_) { + RCLCPP_WARN(logger_, + "Trying to publish message on the topic '%s', but the publisher is not activated", + this->get_topic_name()); + return; } rclcpp::Publisher::publish(msg); @@ -122,6 +138,10 @@ public: publish(const MessageT & msg) { if (!enabled_) { + RCLCPP_WARN(logger_, + "Trying to publish message on the topic '%s', but the publisher is not activated", + this->get_topic_name()); + return; } rclcpp::Publisher::publish(msg); @@ -146,6 +166,10 @@ public: publish(std::shared_ptr & msg) { if (!enabled_) { + RCLCPP_WARN(logger_, + "Trying to publish message on the topic '%s', but the publisher is not activated", + this->get_topic_name()); + return; } rclcpp::Publisher::publish(msg); @@ -171,6 +195,7 @@ public: private: bool enabled_ = false; + rclcpp::Logger logger_; }; } // namespace rclcpp_lifecycle