moveit2
The MoveIt Motion Planning Framework for ROS 2.
logger.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2023, PickNik Robotics Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Tyler Weaver */
36 
37 #include <rclcpp/rclcpp.hpp>
38 #include <moveit/utils/logger.hpp>
39 #include <string>
40 #include <rsl/random.hpp>
41 #include <fmt/format.h>
42 
43 namespace moveit
44 {
45 
46 // This is the function that stores the global logger used by moveit.
47 // As it returns a reference to the static logger it can be changed through the
48 // `setNodeLoggerName` function.
49 rclcpp::Logger& getGlobalRootLogger()
50 {
51  static rclcpp::Logger logger = [&] {
52  // A random number is appended to the name used for the node to make it unique.
53  // This unique node and logger name is only used if a user does not set a logger
54  // through the `setNodeLoggerName` method to their node's logger.
55  auto name = fmt::format("moveit_{}", rsl::rng()());
56  try
57  {
58  static auto* moveit_node = new rclcpp::Node(name);
59  return moveit_node->get_logger();
60  }
61  catch (const std::exception& ex)
62  {
63  // rclcpp::init was not called so rcl context is null, return non-node logger
64  auto logger = rclcpp::get_logger(name);
65  RCLCPP_WARN_STREAM(logger, "exception thrown while creating node for logging: " << ex.what());
66  RCLCPP_WARN(logger, "if rclcpp::init was not called, messages from this logger may be missing from /rosout");
67  return logger;
68  }
69  }();
70  return logger;
71 }
72 
73 void setNodeLoggerName(const std::string& name)
74 {
75  static auto node = std::make_shared<rclcpp::Node>("moveit", name);
76  getGlobalRootLogger() = node->get_logger();
77 }
78 
79 rclcpp::Logger getLogger(const std::string& name)
80 {
81  return getGlobalRootLogger().get_child(name);
82 }
83 
84 } // namespace moveit
Main namespace for MoveIt.
Definition: exceptions.h:43
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition: logger.cpp:79
void setNodeLoggerName(const std::string &name)
Call once after creating a node to initialize logging namespaces.
Definition: logger.cpp:73
rclcpp::Logger & getGlobalRootLogger()
Definition: logger.cpp:49
name
Definition: setup.py:7