ROS 2 rclcpp + rcl - rolling  rolling-20536064
ROS 2 C++ Client Library with ROS Client Library
logger.cpp
1 // Copyright 2017 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <memory>
16 #include <string>
17 #include <utility>
18 
19 #include "rcl/error_handling.h"
20 #include "rcl/logging_rosout.h"
21 
22 #include "rclcpp/exceptions.hpp"
23 #include "rclcpp/logger.hpp"
24 #include "rclcpp/logging.hpp"
25 
26 #include "./logging_mutex.hpp"
27 
28 namespace rclcpp
29 {
30 
31 Logger
32 get_logger(const std::string & name)
33 {
34 #if RCLCPP_LOGGING_ENABLED
35  return rclcpp::Logger(name);
36 #else
37  (void)name;
38  return rclcpp::Logger();
39 #endif
40 }
41 
42 Logger
44 {
45  const char * logger_name = rcl_node_get_logger_name(node);
46  if (nullptr == logger_name) {
47  auto logger = rclcpp::get_logger("rclcpp");
48  RCLCPP_ERROR(
49  logger, "failed to get logger name from node at address %p",
50  static_cast<void *>(const_cast<rcl_node_t *>(node)));
51  return logger;
52  }
53  return rclcpp::get_logger(logger_name);
54 }
55 
56 Logger
57 Logger::get_child(const std::string & suffix)
58 {
59  if (!name_) {
60  return Logger();
61  }
62 
63  rcl_ret_t rcl_ret = RCL_RET_OK;
64  std::shared_ptr<std::recursive_mutex> logging_mutex;
65  logging_mutex = get_global_logging_mutex();
66  {
67  std::lock_guard<std::recursive_mutex> guard(*logging_mutex);
68  rcl_ret = rcl_logging_rosout_add_sublogger((*name_).c_str(), suffix.c_str());
69  if (RCL_RET_NOT_FOUND == rcl_ret) {
70  rcl_reset_error();
71  } else if (RCL_RET_OK != rcl_ret) {
72  exceptions::throw_from_rcl_error(
73  rcl_ret, "failed to call rcl_logging_rosout_add_sublogger",
74  rcl_get_error_state(), rcl_reset_error);
75  }
76  }
77 
78  Logger logger(*name_ + RCUTILS_LOGGING_SEPARATOR_STRING + suffix);
79  if (RCL_RET_OK == rcl_ret) {
80  logger.logger_sublogger_pairname_.reset(
81  new std::pair<std::string, std::string>({*name_, suffix}),
82  [logging_mutex](std::pair<std::string, std::string> * logger_sublogger_pairname_ptr) {
83  std::lock_guard<std::recursive_mutex> guard(*logging_mutex);
85  logger_sublogger_pairname_ptr->first.c_str(),
86  logger_sublogger_pairname_ptr->second.c_str());
87  delete logger_sublogger_pairname_ptr;
88  if (RCL_RET_OK != rcl_ret) {
89  rcl_reset_error();
90  }
91  });
92  }
93  return logger;
94 }
95 
96 void
98 {
99  rcutils_ret_t rcutils_ret = rcutils_logging_set_logger_level(
100  get_name(),
101  static_cast<RCUTILS_LOG_SEVERITY>(level));
102  if (rcutils_ret != RCUTILS_RET_OK) {
103  if (rcutils_ret == RCUTILS_RET_INVALID_ARGUMENT) {
104  exceptions::throw_from_rcl_error(
105  RCL_RET_INVALID_ARGUMENT, "Invalid parameter",
106  rcutils_get_error_state(), rcutils_reset_error);
107  }
108  exceptions::throw_from_rcl_error(
109  RCL_RET_ERROR, "Couldn't set logger level",
110  rcutils_get_error_state(), rcutils_reset_error);
111  }
112 }
113 
116 {
117  int logger_level = rcutils_logging_get_logger_effective_level(get_name());
118 
119  if (logger_level < 0) {
120  exceptions::throw_from_rcl_error(
121  RCL_RET_ERROR, "Couldn't get logger level",
122  rcutils_get_error_state(), rcutils_reset_error);
123  }
124 
125  return static_cast<Level>(logger_level);
126 }
127 
128 } // namespace rclcpp
RCLCPP_PUBLIC void set_level(Level level)
Set level for current logger.
Definition: logger.cpp:97
RCLCPP_PUBLIC Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
Definition: logger.cpp:57
RCLCPP_PUBLIC const char * get_name() const
Get the name of this logger.
Definition: logger.hpp:124
RCLCPP_PUBLIC Level get_effective_level() const
Get effective level for current logger.
Definition: logger.cpp:115
Level
An enum for the type of logger level.
Definition: logger.hpp:84
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_logging_rosout_add_sublogger(const char *logger_name, const char *sublogger_name)
Add a subordinate logger based on a logger.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_logging_rosout_remove_sublogger(const char *logger_name, const char *sublogger_name)
Remove a subordinate logger and cleans up allocated resources.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC Logger get_node_logger(const rcl_node_t *node)
Return a named logger using an rcl_node_t.
Definition: logger.cpp:43
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Definition: logger.cpp:32
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_node_get_logger_name(const rcl_node_t *node)
Return the logger name of the node.
Definition: node.c:493
Structure which encapsulates a ROS Node.
Definition: node.h:45
#define RCL_RET_NOT_FOUND
Resource not found.
Definition: types.h:55
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:35
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:29
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24