ROS 2 rclcpp + rcl - kilted  kilted
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 
81 // TODO(ahcorde): Remove deprecated class on the next release (in Rolling after Kilted).
82 #if !defined(_WIN32)
83 # pragma GCC diagnostic push
84 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
85 #else // !defined(_WIN32)
86 # pragma warning(push)
87 # pragma warning(disable: 4996)
88 #endif
90 
97 [[deprecated("use rclcpp::get_log_directory instead of rclcpp::get_logging_directory")]]
98 RCLCPP_PUBLIC
99 rcpputils::fs::path
101 
102 // remove warning suppression
103 #if !defined(_WIN32)
104 # pragma GCC diagnostic pop
105 #else // !defined(_WIN32)
106 # pragma warning(pop)
107 #endif
108 
110 
117 RCLCPP_PUBLIC
118 std::filesystem::path
120 
121 class Logger
122 {
123 public:
125  enum class Level
126  {
127  Unset = RCUTILS_LOG_SEVERITY_UNSET,
128  Debug = RCUTILS_LOG_SEVERITY_DEBUG,
129  Info = RCUTILS_LOG_SEVERITY_INFO,
130  Warn = RCUTILS_LOG_SEVERITY_WARN,
131  Error = RCUTILS_LOG_SEVERITY_ERROR,
132  Fatal = RCUTILS_LOG_SEVERITY_FATAL,
133  };
134 
135 private:
136  friend Logger rclcpp::get_logger(const std::string & name);
137  friend ::rclcpp::node_interfaces::NodeLogging;
138 
140 
144  Logger()
145  : name_(nullptr) {}
146 
148 
151  explicit Logger(const std::string & name)
152  : name_(new std::string(name)) {}
153 
154  std::shared_ptr<const std::string> name_;
155  std::shared_ptr<std::pair<std::string, std::string>> logger_sublogger_pairname_ = nullptr;
156 
157 public:
159 
164  RCLCPP_PUBLIC
165  const char *
166  get_name() const
167  {
168  if (!name_) {
169  return nullptr;
170  }
171  return name_->c_str();
172  }
173 
175 
186  RCLCPP_PUBLIC
187  Logger
188  get_child(const std::string & suffix);
189 
191 
196  RCLCPP_PUBLIC
197  void
198  set_level(Level level);
199 
201 
214  RCLCPP_PUBLIC
215  Level
216  get_effective_level() const;
217 };
218 
219 } // namespace rclcpp
220 
221 #endif // RCLCPP__LOGGER_HPP_
RCLCPP_PUBLIC void set_level(Level level)
Set level for current logger.
Definition: logger.cpp:141
RCLCPP_PUBLIC Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
Definition: logger.cpp:101
RCLCPP_PUBLIC const char * get_name() const
Get the name of this logger.
Definition: logger.hpp:166
RCLCPP_PUBLIC Level get_effective_level() const
Get effective level for current logger.
Definition: logger.cpp:159
Level
An enum for the type of logger level.
Definition: logger.hpp:126
@ 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:87
RCLCPP_PUBLIC rcpputils::fs::path get_logging_directory()
Get the current logging directory.
Definition: logger.cpp:67
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