ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
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 <filesystem>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "rclcpp/visibility_control.hpp"
24 
25 #include "rcl/node.h"
26 #include "rcutils/logging.h"
27 #include "rcpputils/filesystem_helper.hpp"
28 
38 // TODO(dhood): determine this automatically from `RCLCPP_LOG_MIN_SEVERITY`
39 #ifndef RCLCPP_LOGGING_ENABLED
40 #define RCLCPP_LOGGING_ENABLED 1
41 #endif
42 
43 namespace rclcpp
44 {
45 
46 // Forward declaration is used for friend statement.
47 namespace node_interfaces
48 {
49 class NodeLogging;
50 }
51 
52 class Logger;
53 
55 
65 RCLCPP_PUBLIC
66 Logger
67 get_logger(const std::string & name);
68 
70 
77 RCLCPP_PUBLIC
78 Logger
79 get_node_logger(const rcl_node_t * node);
80 
82 
89 RCLCPP_PUBLIC
90 std::filesystem::path
92 
93 class Logger
94 {
95 public:
97  enum class Level
98  {
99  Unset = RCUTILS_LOG_SEVERITY_UNSET,
100  Debug = RCUTILS_LOG_SEVERITY_DEBUG,
101  Info = RCUTILS_LOG_SEVERITY_INFO,
102  Warn = RCUTILS_LOG_SEVERITY_WARN,
103  Error = RCUTILS_LOG_SEVERITY_ERROR,
104  Fatal = RCUTILS_LOG_SEVERITY_FATAL,
105  };
106 
107 private:
108  friend Logger rclcpp::get_logger(const std::string & name);
109  friend ::rclcpp::node_interfaces::NodeLogging;
110 
112 
116  Logger()
117  : name_(nullptr) {}
118 
120 
123  explicit Logger(const std::string & name)
124  : name_(new std::string(name)) {}
125 
126  std::shared_ptr<const std::string> name_;
127  std::shared_ptr<std::pair<std::string, std::string>> logger_sublogger_pairname_ = nullptr;
128 
129 public:
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 
163 
168  RCLCPP_PUBLIC
169  void
170  set_level(Level level);
171 
173 
186  RCLCPP_PUBLIC
187  Level
188  get_effective_level() const;
189 };
190 
191 } // namespace rclcpp
192 
193 #endif // RCLCPP__LOGGER_HPP_
RCLCPP_PUBLIC void set_level(Level level)
Set level for current logger.
Definition: logger.cpp:113
RCLCPP_PUBLIC Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
Definition: logger.cpp:73
RCLCPP_PUBLIC const char * get_name() const
Get the name of this logger.
Definition: logger.hpp:138
RCLCPP_PUBLIC Level get_effective_level() const
Get effective level for current logger.
Definition: logger.cpp:131
Level
An enum for the type of logger level.
Definition: logger.hpp:98
@ 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:45
RCLCPP_PUBLIC std::filesystem::path get_log_directory()
Get the current logging directory.
Definition: logger.cpp:59
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Definition: logger.cpp:34
Structure which encapsulates a ROS Node.
Definition: node.h:45