-
Notifications
You must be signed in to change notification settings - Fork 482
Ensure logging is initialized just once #998
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
Changes from all commits
111424a
05a8dd8
e85034d
5c25c78
0c95f16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| #include <utility> | ||
|
|
||
| #include "rcl/init.h" | ||
| #include "rcl/logging.h" | ||
| #include "rclcpp/detail/utilities.hpp" | ||
| #include "rclcpp/exceptions.hpp" | ||
| #include "rclcpp/logging.hpp" | ||
|
|
@@ -34,8 +35,27 @@ static std::vector<std::weak_ptr<rclcpp::Context>> g_contexts; | |
|
|
||
| using rclcpp::Context; | ||
|
|
||
| static | ||
| std::shared_ptr<std::mutex> | ||
| get_global_logging_configure_mutex() | ||
| { | ||
| static auto mutex = std::make_shared<std::mutex>(); | ||
| return mutex; | ||
| } | ||
|
|
||
| static | ||
| size_t & | ||
| get_logging_reference_count() | ||
| { | ||
| static size_t ref_count = 0; | ||
| return ref_count; | ||
| } | ||
|
Contributor
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. @ivanpauno meta: I won't block this PR on it, but I think this reference counting should be down in
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. IMHO, no.
Contributor
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. I see. Then if this aims to be just a temporary solution, a TODO and/or an follow-up issue would be nice. |
||
|
|
||
| Context::Context() | ||
| : rcl_context_(nullptr), shutdown_reason_("") {} | ||
| : rcl_context_(nullptr), | ||
| shutdown_reason_(""), | ||
| logging_configure_mutex_(nullptr) | ||
| {} | ||
|
|
||
| Context::~Context() | ||
| { | ||
|
|
@@ -94,6 +114,30 @@ Context::init( | |
| rcl_context_.reset(); | ||
| rclcpp::exceptions::throw_from_rcl_error(ret, "failed to initialize rcl"); | ||
| } | ||
|
|
||
| if (init_options.auto_initialize_logging()) { | ||
| logging_configure_mutex_ = get_global_logging_configure_mutex(); | ||
| if (!logging_configure_mutex_) { | ||
| throw std::runtime_error("global logging configure mutex is 'nullptr'"); | ||
| } | ||
| std::lock_guard<std::mutex> guard(*logging_configure_mutex_); | ||
| size_t & count = get_logging_reference_count(); | ||
| if (0u == count) { | ||
| ret = rcl_logging_configure( | ||
| &rcl_context_->global_arguments, | ||
| rcl_init_options_get_allocator(init_options_.get_rcl_init_options())); | ||
| if (RCL_RET_OK != ret) { | ||
| rcl_context_.reset(); | ||
| rclcpp::exceptions::throw_from_rcl_error(ret, "failed to configure logging"); | ||
| } | ||
| } else { | ||
| RCLCPP_WARN( | ||
| rclcpp::get_logger("rclcpp"), | ||
| "logging was initialized more than once"); | ||
| } | ||
| ++count; | ||
| } | ||
|
|
||
| try { | ||
| std::vector<std::string> unparsed_ros_arguments = detail::get_unparsed_ros_arguments( | ||
| argc, argv, &(rcl_context_->global_arguments), rcl_get_default_allocator()); | ||
|
|
@@ -183,6 +227,22 @@ Context::shutdown(const std::string & reason) | |
| ++it; | ||
| } | ||
| } | ||
| // shutdown logger | ||
| if (logging_configure_mutex_) { | ||
| // logging was initialized by this context | ||
| std::lock_guard<std::mutex> guard(*logging_configure_mutex_); | ||
| size_t & count = get_logging_reference_count(); | ||
| if (0u == --count) { | ||
| rcl_ret_t rcl_ret = rcl_logging_fini(); | ||
| if (RCL_RET_OK != rcl_ret) { | ||
| RCUTILS_SAFE_FWRITE_TO_STDERR( | ||
| RCUTILS_STRINGIFY(__file__) ":" | ||
| RCUTILS_STRINGIFY(__LINE__) | ||
| " failed to fini logging"); | ||
| rcl_reset_error(); | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.