ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
logger.hpp
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 #ifndef RCLCPP__LOGGER_HPP_
16 #define RCLCPP__LOGGER_HPP_
17 
18 #include <memory>
19 #include <string>
20 
21 #include "rclcpp/visibility_control.hpp"
22 
23 #include "rcl/node.h"
24 #include "rcutils/logging.h"
25 #include "rcpputils/filesystem_helper.hpp"
26 
36 // TODO(dhood): determine this automatically from `RCLCPP_LOG_MIN_SEVERITY`
37 #ifndef RCLCPP_LOGGING_ENABLED
38 #define RCLCPP_LOGGING_ENABLED 1
39 #endif
40 
41 namespace rclcpp
42 {
43 
44 // Forward declaration is used for friend statement.
45 namespace node_interfaces
46 {
47 class NodeLogging;
48 }
49 
50 class Logger;
51 
53 
63 RCLCPP_PUBLIC
64 Logger
65 get_logger(const std::string & name);
66 
68 
75 RCLCPP_PUBLIC
76 Logger
77 get_node_logger(const rcl_node_t * node);
78 
80 
87 RCLCPP_PUBLIC
88 rcpputils::fs::path
90 
91 class Logger
92 {
93 public:
95  enum class Level
96  {
97  Unset = RCUTILS_LOG_SEVERITY_UNSET,
98  Debug = RCUTILS_LOG_SEVERITY_DEBUG,
99  Info = RCUTILS_LOG_SEVERITY_INFO,
100  Warn = RCUTILS_LOG_SEVERITY_WARN,
101  Error = RCUTILS_LOG_SEVERITY_ERROR,
102  Fatal = RCUTILS_LOG_SEVERITY_FATAL,
103  };
104 
105 private:
106  friend Logger rclcpp::get_logger(const std::string & name);
107  friend ::rclcpp::node_interfaces::NodeLogging;
108 
110 
114  Logger()
115  : name_(nullptr) {}
116 
118 
121  explicit Logger(const std::string & name)
122  : name_(new std::string(name)) {}
123 
124  std::shared_ptr<const std::string> name_;
125 
126 public:
127  RCLCPP_PUBLIC
128  Logger(const Logger &) = default;
129 
131 
136  RCLCPP_PUBLIC
137  const char *
138  get_name() const
139  {
140  if (!name_) {
141  return nullptr;
142  }
143  return name_->c_str();
144  }
145 
147 
158  RCLCPP_PUBLIC
159  Logger
160  get_child(const std::string & suffix)
161  {
162  if (!name_) {
163  return Logger();
164  }
165  return Logger(*name_ + "." + suffix);
166  }
167 
169 
174  RCLCPP_PUBLIC
175  void
176  set_level(Level level);
177 };
178 
179 } // namespace rclcpp
180 
181 #endif // RCLCPP__LOGGER_HPP_
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
RCLCPP_PUBLIC Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
Definition: logger.hpp:160
Level
An enum for the type of logger level.
Definition: logger.hpp:96
@ Info
The info log level.
@ Warn
The warn log level.
@ Fatal
The fatal log level.
@ Error
The error log level.
@ Debug
The debug log level.
@ Unset
The unset log level.
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
Structure which encapsulates a ROS Node.
Definition: node.h:42