-
Notifications
You must be signed in to change notification settings - Fork 564
Add Logger class and give one to nodes #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
647d07d
Add Logger class and give one to nodes
dhood 2decda3
Try to improve compiler errors when non-Logger is passed to macros
dhood 31d687f
Add define for 'disabling' loggers
dhood 6ef5970
Add/update tests
dhood bb67d17
Linter fix
dhood f7a1e66
Documentation
dhood d92b7ce
Windows fix
dhood d47cb79
Move free functions to source file (windows was upset)
dhood be6cd99
Fix windows by changing prototype ordering
dhood 2002852
Store node logger in NodeBase
dhood d9d3886
Windows is not happy with this EXPECT_ANY_THROW
dhood 4600014
Move get_logger to a NodeLogger interface
dhood 1566cd8
Move Logger into 'logger' namespace
dhood 70f5bbd
Move helper function for macro errors into macro header
dhood 05c8bc7
Remove 'logger' namespace
dhood c80ac85
Return type on separate line
dhood 8d91bf3
Update copyright year
dhood a9c0ef7
Give lifecycle nodes a logger
dhood ab695af
Add test for lifecycle node logger
dhood a519b72
Switch to static_assert for logger check
dhood c3960b7
global ns scope in macro calls
dhood 5142bab
Revert "Add test for lifecycle node logger" (make diff smaller)
dhood af66f6c
Update for rcutils function name change
dhood 05d86cf
Add reference to Node::get_logger() in doxygen
dhood 7769fb4
Rename NodeLoggerInterface to NodeLoggingInterface
dhood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright 2017 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef RCLCPP__LOGGER_HPP_ | ||
| #define RCLCPP__LOGGER_HPP_ | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "rclcpp/visibility_control.hpp" | ||
|
|
||
| /** | ||
| * \def RCLCPP_LOGGING_ENABLED | ||
| * When this define evaluates to true (default), logger factory functions will | ||
| * behave normally. | ||
| * When false, logger factory functions will create dummy loggers to avoid | ||
| * computational expense in manipulating objects. | ||
| * This should be used in combination with `RCLCPP_LOG_MIN_SEVERITY` to compile | ||
| * out logging macros. | ||
| */ | ||
| // TODO(dhood): determine this automatically from `RCLCPP_LOG_MIN_SEVERITY` | ||
| #ifndef RCLCPP_LOGGING_ENABLED | ||
| #define RCLCPP_LOGGING_ENABLED 1 | ||
| #endif | ||
|
|
||
| namespace rclcpp | ||
| { | ||
|
|
||
| // Forward declaration is used for friend statement. | ||
| namespace node_interfaces | ||
| { | ||
| class NodeLogging; | ||
| } | ||
|
|
||
| class Logger; | ||
|
|
||
| /// Return a named logger. | ||
| /** | ||
| * The returned logger's name will include any naming conventions, such as a | ||
| * name prefix. | ||
| * Currently there are no such naming conventions but they may be introduced in | ||
| * the future. | ||
| * | ||
| * \param[in] name the name of the logger | ||
| * \return a logger with the fully-qualified name including naming conventions, or | ||
| * \return a dummy logger if logging is disabled. | ||
| */ | ||
| RCLCPP_PUBLIC | ||
| Logger | ||
| get_logger(const std::string & name); | ||
|
|
||
| class Logger | ||
| { | ||
| private: | ||
| friend Logger rclcpp::get_logger(const std::string & name); | ||
| friend ::rclcpp::node_interfaces::NodeLogging; | ||
|
|
||
| /// Constructor of a dummy logger. | ||
| /** | ||
| * This is used when logging is disabled: see `RCLCPP_LOGGING_ENABLED`. | ||
| * This cannot be called directly, see `rclcpp::get_logger` instead. | ||
| */ | ||
| Logger() | ||
| : name_(nullptr) {} | ||
|
|
||
| /// Constructor of a named logger. | ||
| /** | ||
| * This cannot be called directly, see `rclcpp::get_logger` instead. | ||
| */ | ||
| explicit Logger(const std::string & name) | ||
| : name_(new std::string(name)) {} | ||
|
|
||
| std::shared_ptr<const std::string> name_; | ||
|
|
||
| public: | ||
| RCLCPP_PUBLIC | ||
| Logger(const Logger &) = default; | ||
|
|
||
| /// Get the name of this logger. | ||
| /** | ||
| * \return the full name of the logger including any prefixes, or | ||
| * \return `nullptr` if this logger is invalid (e.g. because logging is | ||
| * disabled). | ||
| */ | ||
| RCLCPP_PUBLIC | ||
| const char * | ||
| get_name() const | ||
| { | ||
| if (!name_) { | ||
| return nullptr; | ||
| } | ||
| return name_->c_str(); | ||
| } | ||
|
|
||
| /// Return a logger that is a descendant of this logger. | ||
| /** | ||
| * The child logger's full name will include any hierarchy conventions that | ||
| * indicate it is a descendant of this logger. | ||
| * For example, ```get_logger('abc').get_child('def')``` will return a logger | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: which use triple back tick here but single elsewhere?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it wasn't rendering the full expression without the triple (the others are simpler) |
||
| * with name `abc.def`. | ||
| * | ||
| * \param[in] suffix the child logger's suffix | ||
| * \return a logger with the fully-qualified name including the suffix, or | ||
| * \return a dummy logger if this logger is invalid (e.g. because logging is | ||
| * disabled). | ||
| */ | ||
| RCLCPP_PUBLIC | ||
| Logger | ||
| get_child(const std::string & suffix) | ||
| { | ||
| if (!name_) { | ||
| return Logger(); | ||
| } | ||
| return Logger(*name_ + "." + suffix); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace rclcpp | ||
|
|
||
| #endif // RCLCPP__LOGGER_HPP_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2017 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef RCLCPP__NODE_INTERFACES__NODE_LOGGING_HPP_ | ||
| #define RCLCPP__NODE_INTERFACES__NODE_LOGGING_HPP_ | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "rclcpp/logger.hpp" | ||
| #include "rclcpp/macros.hpp" | ||
| #include "rclcpp/node_interfaces/node_base_interface.hpp" | ||
| #include "rclcpp/node_interfaces/node_logging_interface.hpp" | ||
| #include "rclcpp/visibility_control.hpp" | ||
|
|
||
| namespace rclcpp | ||
| { | ||
| namespace node_interfaces | ||
| { | ||
|
|
||
| /// Implementation of the NodeLogging part of the Node API. | ||
| class NodeLogging : public NodeLoggingInterface | ||
| { | ||
| public: | ||
| RCLCPP_SMART_PTR_ALIASES_ONLY(NodeLoggingInterface) | ||
|
|
||
| RCLCPP_PUBLIC | ||
| explicit NodeLogging(rclcpp::node_interfaces::NodeBaseInterface * node_base); | ||
|
|
||
| RCLCPP_PUBLIC | ||
| virtual | ||
| ~NodeLogging(); | ||
|
|
||
| RCLCPP_PUBLIC | ||
| virtual | ||
| rclcpp::Logger | ||
| get_logger() const; | ||
|
|
||
| private: | ||
| RCLCPP_DISABLE_COPY(NodeLogging) | ||
|
|
||
| /// Handle to the NodeBaseInterface given in the constructor. | ||
| rclcpp::node_interfaces::NodeBaseInterface * node_base_; | ||
|
|
||
| rclcpp::Logger logger_; | ||
| }; | ||
|
|
||
| } // namespace node_interfaces | ||
| } // namespace rclcpp | ||
|
|
||
| #endif // RCLCPP__NODE_INTERFACES__NODE_LOGGING_HPP_ |
47 changes: 47 additions & 0 deletions
47
rclcpp/include/rclcpp/node_interfaces/node_logging_interface.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright 2017 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef RCLCPP__NODE_INTERFACES__NODE_LOGGING_INTERFACE_HPP_ | ||
| #define RCLCPP__NODE_INTERFACES__NODE_LOGGING_INTERFACE_HPP_ | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "rclcpp/logger.hpp" | ||
| #include "rclcpp/macros.hpp" | ||
| #include "rclcpp/node_interfaces/node_base_interface.hpp" | ||
| #include "rclcpp/visibility_control.hpp" | ||
|
|
||
| namespace rclcpp | ||
| { | ||
| namespace node_interfaces | ||
| { | ||
|
|
||
| /// Pure virtual interface class for the NodeLogging part of the Node API. | ||
| class NodeLoggingInterface | ||
| { | ||
| public: | ||
| RCLCPP_SMART_PTR_ALIASES_ONLY(NodeLoggingInterface) | ||
|
|
||
| /// Return the logger of the node. | ||
| /** \return The logger of the node. */ | ||
| RCLCPP_PUBLIC | ||
| virtual | ||
| rclcpp::Logger | ||
| get_logger() const = 0; | ||
| }; | ||
|
|
||
| } // namespace node_interfaces | ||
| } // namespace rclcpp | ||
|
|
||
| #endif // RCLCPP__NODE_INTERFACES__NODE_LOGGING_INTERFACE_HPP_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we think this should return a shared ptr as log4cxx does or leave it up to callers to call
make_sharedor similar?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our discussion it sounded like it might make sense to return a shared_ptr here. In particular this would allow us to implement the actual Logger with a specialized subclass that potentially overrides the get_name or other capabilities and keeps a weak backlink to the node and can output a warning if node goes out of scope and the backlink is broken. And although the current logger is trivially copyable right now we wouldn't need to keep it that way looking forward if we return the pointer instead of an instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We talked about this off-line (@dhood and I) and I pointed out that if we want to do what you describe @tfoote we don't have swap the return type to a shared pointer, instead we could have the Logger class store a shared pointer and forward all methods calls to the shared pointer. Basically we could accomplish the same thing without inheritance.
That being said, if @dhood wanted to switch this to a pointer, that's fine by me too.
Also, if we really want to return a shared pointer we can do that in the future with an API break.