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 
32 // TODO(dhood): determine this automatically from `RCLCPP_LOG_MIN_SEVERITY`
33 #ifndef RCLCPP_LOGGING_ENABLED
34 #define RCLCPP_LOGGING_ENABLED 1
35 #endif
36 
37 namespace rclcpp
38 {
39 
40 // Forward declaration is used for friend statement.
41 namespace node_interfaces
42 {
43 class NodeLogging;
44 }
45 
46 class Logger;
47 
49 
60 Logger
61 get_logger(const std::string & name);
62 
63 class Logger
64 {
65 private:
66  friend Logger rclcpp::get_logger(const std::string & name);
67  friend ::rclcpp::node_interfaces::NodeLogging;
68 
70 
74  Logger()
75  : name_(nullptr) {}
76 
78 
81  explicit Logger(const std::string & name)
82  : name_(new std::string(name)) {}
83 
85 
86 public:
88  Logger(const Logger &) = default;
89 
91 
97  const char *
98  get_name() const
99  {
100  if (!name_) {
101  return nullptr;
102  }
103  return name_->c_str();
104  }
105 
107 
119  Logger
120  get_child(const std::string & suffix)
121  {
122  if (!name_) {
123  return Logger();
124  }
125  return Logger(*name_ + "." + suffix);
126  }
127 };
128 
129 } // namespace rclcpp
130 
131 #endif // RCLCPP__LOGGER_HPP_
Logger get_logger(const std::string &name)
Return a named logger.
Definition: allocator_common.hpp:24
Definition: logger.hpp:63
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
const char * get_name() const
Get the name of this logger.
Definition: logger.hpp:98
Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
Definition: logger.hpp:120