From 8e215d865fe6754f7071bde283de8c6613d6643c Mon Sep 17 00:00:00 2001 From: Tyler Weaver Date: Wed, 25 Oct 2023 14:13:56 -0600 Subject: [PATCH] Test for exception thrown durring making a node --- moveit_core/utils/src/logger.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/moveit_core/utils/src/logger.cpp b/moveit_core/utils/src/logger.cpp index 2d21d431c3..98a736b128 100644 --- a/moveit_core/utils/src/logger.cpp +++ b/moveit_core/utils/src/logger.cpp @@ -45,8 +45,21 @@ namespace moveit rclcpp::Logger& get_global_logger_ref() { - static std::shared_ptr moveit_node = std::make_shared("moveit"); - static rclcpp::Logger logger = moveit_node->get_logger(); + static std::shared_ptr moveit_node; + static rclcpp::Logger logger = [&] { + try + { + moveit_node = std::make_shared("moveit"); + } + catch (...) + { + // rclcpp::init was not called so rcl context is null, return non-node logger + auto logger = rclcpp::get_logger("moveit"); + RCLCPP_WARN(logger, "rclcpp::init was not called, messages from this logger may be missing from /rosout"); + return logger; + } + return moveit_node->get_logger(); + }(); return logger; } @@ -68,7 +81,17 @@ rclcpp::Logger make_child_logger(const std::string& name) if (child_nodes.find(name) == child_nodes.end()) { std::string ns = get_logger().get_name(); - child_nodes[name] = std::make_shared(name, ns); + try + { + child_nodes[name] = std::make_shared(name, ns); + } + catch (...) + { + // rclcpp::init was not called so rcl context is null, return non-node logger + auto logger = rclcpp::get_logger(ns.append(".").append(name)); + RCLCPP_WARN(logger, "rclcpp::init was not called, messages from this logger may be missing from /rosout"); + return logger; + } } #endif