ROS 2 rclcpp + rcl - jazzy  jazzy
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_logging_interface/rcl_logging_interface.h"
20 #include "rcl/error_handling.h"
21 #include "rcl/logging_rosout.h"
22 
23 #include "rclcpp/exceptions.hpp"
24 #include "rclcpp/logger.hpp"
25 #include "rclcpp/logging.hpp"
26 
27 #include "./logging_mutex.hpp"
28 
29 namespace rclcpp
30 {
31 
32 Logger
33 get_logger(const std::string & name)
34 {
35 #if RCLCPP_LOGGING_ENABLED
36  return rclcpp::Logger(name);
37 #else
38  (void)name;
39  return rclcpp::Logger();
40 #endif
41 }
42 
43 Logger
45 {
46  const char * logger_name = rcl_node_get_logger_name(node);
47  if (nullptr == logger_name) {
48  auto logger = rclcpp::get_logger("rclcpp");
49  RCLCPP_ERROR(
50  logger, "failed to get logger name from node at address %p",
51  static_cast<void *>(const_cast<rcl_node_t *>(node)));
52  return logger;
53  }
54  return rclcpp::get_logger(logger_name);
55 }
56 
57 rcpputils::fs::path
59 {
60  char * log_dir = NULL;
61  auto allocator = rcutils_get_default_allocator();
62  rcl_logging_ret_t ret = rcl_logging_get_logging_directory(allocator, &log_dir);
63  if (RCL_LOGGING_RET_OK != ret) {
64  rclcpp::exceptions::throw_from_rcl_error(ret);
65  }
66  std::string path{log_dir};
67  allocator.deallocate(log_dir, allocator.state);
68  return path;
69 }
70 
71 Logger
72 Logger::get_child(const std::string & suffix)
73 {
74  if (!name_) {
75  return Logger();
76  }
77 
78  rcl_ret_t rcl_ret = RCL_RET_OK;
79  std::shared_ptr<std::recursive_mutex> logging_mutex;
80  logging_mutex = get_global_logging_mutex();
81  {
82  std::lock_guard<std::recursive_mutex> guard(*logging_mutex);
83  rcl_ret = rcl_logging_rosout_add_sublogger((*name_).c_str(), suffix.c_str());
84  if (RCL_RET_NOT_FOUND == rcl_ret) {
85  rcl_reset_error();
86  } else if (RCL_RET_OK != rcl_ret) {
87  exceptions::throw_from_rcl_error(
88  rcl_ret, "failed to call rcl_logging_rosout_add_sublogger",
89  rcl_get_error_state(), rcl_reset_error);
90  }
91  }
92 
93  Logger logger(*name_ + RCUTILS_LOGGING_SEPARATOR_STRING + suffix);
94  if (RCL_RET_OK == rcl_ret) {
95  logger.logger_sublogger_pairname_.reset(
96  new std::pair<std::string, std::string>({*name_, suffix}),
97  [logging_mutex](std::pair<std::string, std::string> * logger_sublogger_pairname_ptr) {
98  std::lock_guard<std::recursive_mutex> guard(*logging_mutex);
100  logger_sublogger_pairname_ptr->first.c_str(),
101  logger_sublogger_pairname_ptr->second.c_str());
102  delete logger_sublogger_pairname_ptr;
103  if (RCL_RET_OK != rcl_ret) {
104  rcl_reset_error();
105  }
106  });
107  }
108  return logger;
109 }
110 
111 void
113 {
114  rcutils_ret_t rcutils_ret = rcutils_logging_set_logger_level(
115  get_name(),
116  static_cast<RCUTILS_LOG_SEVERITY>(level));
117  if (rcutils_ret != RCUTILS_RET_OK) {
118  if (rcutils_ret == RCUTILS_RET_INVALID_ARGUMENT) {
119  exceptions::throw_from_rcl_error(
120  RCL_RET_INVALID_ARGUMENT, "Invalid parameter",
121  rcutils_get_error_state(), rcutils_reset_error);
122  }
123  exceptions::throw_from_rcl_error(
124  RCL_RET_ERROR, "Couldn't set logger level",
125  rcutils_get_error_state(), rcutils_reset_error);
126  }
127 }
128 
131 {
132  int logger_level = rcutils_logging_get_logger_effective_level(get_name());
133 
134  if (logger_level < 0) {
135  exceptions::throw_from_rcl_error(
136  RCL_RET_ERROR, "Couldn't get logger level",
137  rcutils_get_error_state(), rcutils_reset_error);
138  }
139 
140  return static_cast<Level>(logger_level);
141 }
142 
143 } // namespace rclcpp
RCLCPP_PUBLIC void set_level(Level level)
Set level for current logger.
Definition: logger.cpp:112
RCLCPP_PUBLIC Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
Definition: logger.cpp:72
RCLCPP_PUBLIC const char * get_name() const
Get the name of this logger.
Definition: logger.hpp:137
RCLCPP_PUBLIC Level get_effective_level() const
Get effective level for current logger.
Definition: logger.cpp:130
Level
An enum for the type of logger level.
Definition: logger.hpp:97
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:44
RCLCPP_PUBLIC rcpputils::fs::path get_logging_directory()
Get the current logging directory.
Definition: logger.cpp:58
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Definition: logger.cpp:33
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:488
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