ROS 2 rclcpp + rcl - jazzy  jazzy
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 #include <utility>
21 
22 #include "rclcpp/visibility_control.hpp"
23 
24 #include "rcl/node.h"
25 #include "rcutils/logging.h"
26 #include "rcpputils/filesystem_helper.hpp"
27 
37 // TODO(dhood): determine this automatically from `RCLCPP_LOG_MIN_SEVERITY`
38 #ifndef RCLCPP_LOGGING_ENABLED
39 #define RCLCPP_LOGGING_ENABLED 1
40 #endif
41 
42 namespace rclcpp
43 {
44 
45 // Forward declaration is used for friend statement.
46 namespace node_interfaces
47 {
48 class NodeLogging;
49 }
50 
51 class Logger;
52 
54 
64 RCLCPP_PUBLIC
65 Logger
66 get_logger(const std::string & name);
67 
69 
76 RCLCPP_PUBLIC
77 Logger
78 get_node_logger(const rcl_node_t * node);
79 
81 
88 RCLCPP_PUBLIC
89 rcpputils::fs::path
91 
92 class Logger
93 {
94 public:
96  enum class Level
97  {
98  Unset = RCUTILS_LOG_SEVERITY_UNSET,
99  Debug = RCUTILS_LOG_SEVERITY_DEBUG,
100  Info = RCUTILS_LOG_SEVERITY_INFO,
101  Warn = RCUTILS_LOG_SEVERITY_WARN,
102  Error = RCUTILS_LOG_SEVERITY_ERROR,
103  Fatal = RCUTILS_LOG_SEVERITY_FATAL,
104  };
105 
106 private:
107  friend Logger rclcpp::get_logger(const std::string & name);
108  friend ::rclcpp::node_interfaces::NodeLogging;
109 
111 
115  Logger()
116  : name_(nullptr) {}
117 
119 
122  explicit Logger(const std::string & name)
123  : name_(new std::string(name)) {}
124 
125  std::shared_ptr<const std::string> name_;
126  std::shared_ptr<std::pair<std::string, std::string>> logger_sublogger_pairname_ = nullptr;
127 
128 public:
130 
135  RCLCPP_PUBLIC
136  const char *
137  get_name() const
138  {
139  if (!name_) {
140  return nullptr;
141  }
142  return name_->c_str();
143  }
144 
146 
157  RCLCPP_PUBLIC
158  Logger
159  get_child(const std::string & suffix);
160 
162 
167  RCLCPP_PUBLIC
168  void
169  set_level(Level level);
170 
172 
185  RCLCPP_PUBLIC
186  Level
187  get_effective_level() const;
188 };
189 
190 } // namespace rclcpp
191 
192 #endif // RCLCPP__LOGGER_HPP_
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
@ 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: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
Structure which encapsulates a ROS Node.
Definition: node.h:45