ROS 2 rclcpp + rcl - humble  humble
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 <string>
16 
17 #include "rcl_logging_interface/rcl_logging_interface.h"
18 
19 #include "rclcpp/exceptions.hpp"
20 #include "rclcpp/logger.hpp"
21 #include "rclcpp/logging.hpp"
22 
23 namespace rclcpp
24 {
25 
26 Logger
27 get_logger(const std::string & name)
28 {
29 #if RCLCPP_LOGGING_ENABLED
30  return rclcpp::Logger(name);
31 #else
32  (void)name;
33  return rclcpp::Logger();
34 #endif
35 }
36 
37 Logger
39 {
40  const char * logger_name = rcl_node_get_logger_name(node);
41  if (nullptr == logger_name) {
42  auto logger = rclcpp::get_logger("rclcpp");
43  RCLCPP_ERROR(
44  logger, "failed to get logger name from node at address %p",
45  static_cast<void *>(const_cast<rcl_node_t *>(node)));
46  return logger;
47  }
48  return rclcpp::get_logger(logger_name);
49 }
50 
51 rcpputils::fs::path
53 {
54  char * log_dir = NULL;
55  auto allocator = rcutils_get_default_allocator();
56  rcl_logging_ret_t ret = rcl_logging_get_logging_directory(allocator, &log_dir);
57  if (RCL_LOGGING_RET_OK != ret) {
58  rclcpp::exceptions::throw_from_rcl_error(ret);
59  }
60  std::string path{log_dir};
61  allocator.deallocate(log_dir, allocator.state);
62  return path;
63 }
64 
65 void
67 {
68  rcutils_ret_t rcutils_ret = rcutils_logging_set_logger_level(
69  get_name(),
70  static_cast<RCUTILS_LOG_SEVERITY>(level));
71  if (rcutils_ret != RCUTILS_RET_OK) {
72  if (rcutils_ret == RCUTILS_RET_INVALID_ARGUMENT) {
73  exceptions::throw_from_rcl_error(
74  RCL_RET_INVALID_ARGUMENT, "Invalid parameter",
75  rcutils_get_error_state(), rcutils_reset_error);
76  }
77  exceptions::throw_from_rcl_error(
78  RCL_RET_ERROR, "Couldn't set logger level",
79  rcutils_get_error_state(), rcutils_reset_error);
80  }
81 }
82 
83 } // namespace rclcpp
RCLCPP_PUBLIC void set_level(Level level)
Set level for current logger.
Definition: logger.cpp:66
RCLCPP_PUBLIC const char * get_name() const
Get the name of this logger.
Definition: logger.hpp:138
Level
An enum for the type of logger level.
Definition: logger.hpp:96
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:38
RCLCPP_PUBLIC rcpputils::fs::path get_logging_directory()
Get the current logging directory.
Definition: logger.cpp:52
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Definition: logger.cpp:27
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:512
Structure which encapsulates a ROS Node.
Definition: node.h:42
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:34
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:28