rclcpp  master
C++ ROS Client Library API
logger.hpp
Go to the documentation of this file.
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 
22 
23 #include "rcl/node.h"
24 
34 // TODO(dhood): determine this automatically from `RCLCPP_LOG_MIN_SEVERITY`
35 #ifndef RCLCPP_LOGGING_ENABLED
36 #define RCLCPP_LOGGING_ENABLED 1
37 #endif
38 
39 namespace rclcpp
40 {
41 
42 // Forward declaration is used for friend statement.
43 namespace node_interfaces
44 {
45 class NodeLogging;
46 }
47 
48 class Logger;
49 
51 
62 Logger
63 get_logger(const std::string & name);
64 
66 
74 Logger
75 get_node_logger(const rcl_node_t * node);
76 
77 class Logger
78 {
79 private:
80  friend Logger rclcpp::get_logger(const std::string & name);
81  friend ::rclcpp::node_interfaces::NodeLogging;
82 
84 
88  Logger()
89  : name_(nullptr) {}
90 
92 
95  explicit Logger(const std::string & name)
96  : name_(new std::string(name)) {}
97 
99 
100 public:
102  Logger(const Logger &) = default;
103 
105 
111  const char *
112  get_name() const
113  {
114  if (!name_) {
115  return nullptr;
116  }
117  return name_->c_str();
118  }
119 
121 
133  Logger
134  get_child(const std::string & suffix)
135  {
136  if (!name_) {
137  return Logger();
138  }
139  return Logger(*name_ + "." + suffix);
140  }
141 };
142 
143 } // namespace rclcpp
144 
145 #endif // RCLCPP__LOGGER_HPP_
Definition: logger.hpp:77
Logger get_logger(const std::string &name)
Return a named logger.
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
const char * get_name() const
Get the name of this logger.
Definition: logger.hpp:112
Logger get_node_logger(const rcl_node_t *node)
Return a named logger using an rcl_node_t.
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
Definition: logger.hpp:134