ROS 2 rclcpp + rcl - rolling  rolling-20536064
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 
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 
79 class Logger
80 {
81 public:
83  enum class Level
84  {
85  Unset = RCUTILS_LOG_SEVERITY_UNSET,
86  Debug = RCUTILS_LOG_SEVERITY_DEBUG,
87  Info = RCUTILS_LOG_SEVERITY_INFO,
88  Warn = RCUTILS_LOG_SEVERITY_WARN,
89  Error = RCUTILS_LOG_SEVERITY_ERROR,
90  Fatal = RCUTILS_LOG_SEVERITY_FATAL,
91  };
92 
93 private:
94  friend Logger rclcpp::get_logger(const std::string & name);
95  friend ::rclcpp::node_interfaces::NodeLogging;
96 
98 
102  Logger()
103  : name_(nullptr) {}
104 
106 
109  explicit Logger(const std::string & name)
110  : name_(new std::string(name)) {}
111 
112  std::shared_ptr<const std::string> name_;
113  std::shared_ptr<std::pair<std::string, std::string>> logger_sublogger_pairname_ = nullptr;
114 
115 public:
117 
122  RCLCPP_PUBLIC
123  const char *
124  get_name() const
125  {
126  if (!name_) {
127  return nullptr;
128  }
129  return name_->c_str();
130  }
131 
133 
144  RCLCPP_PUBLIC
145  Logger
146  get_child(const std::string & suffix);
147 
149 
154  RCLCPP_PUBLIC
155  void
156  set_level(Level level);
157 
159 
172  RCLCPP_PUBLIC
173  Level
174  get_effective_level() const;
175 };
176 
177 } // namespace rclcpp
178 
179 #endif // RCLCPP__LOGGER_HPP_
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
@ 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:43
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Definition: logger.cpp:32
Structure which encapsulates a ROS Node.
Definition: node.h:45