-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger_level_configure): add util to set logger level externally…
… in runtime (#5131) * add logger_level_configure Signed-off-by: Takamasa Horibe <[email protected]> * add usage Signed-off-by: Takamasa Horibe <[email protected]> * fix spell Signed-off-by: Takamasa Horibe <[email protected]> * fix from reviewer comment Signed-off-by: Takamasa Horibe <[email protected]> --------- Signed-off-by: Takamasa Horibe <[email protected]>
- Loading branch information
1 parent
7fdda42
commit bb89363
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
68 changes: 68 additions & 0 deletions
68
common/tier4_autoware_utils/include/tier4_autoware_utils/ros/logger_level_configure.hpp
This file contains 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,68 @@ | ||
// Copyright 2023 Tier IV, 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. | ||
|
||
// =============== Note =============== | ||
// This is a util class implementation of the logger_config_component provided by ROS 2 | ||
// https://github.com/ros2/demos/blob/humble/logging_demo/src/logger_config_component.cpp | ||
// | ||
// When ROS 2 officially supports the set_logger_level option in release version, this class can be | ||
// removed. | ||
// https://github.com/ros2/ros2/issues/1355 | ||
|
||
// =============== How to use =============== | ||
// ___In your_node.hpp___ | ||
// #include "tier4_autoware_utils/ros/logger_level_configure.hpp" | ||
// class YourNode : public rclcpp::Node { | ||
// ... | ||
// | ||
// // Define logger_configure as a node class member variable | ||
// std::unique_ptr<tier4_autoware_utils::LoggerLevelConfigure> logger_configure_; | ||
// } | ||
// | ||
// ___In your_node.cpp___ | ||
// YourNode::YourNode() { | ||
// ... | ||
// | ||
// // Set up logger_configure | ||
// logger_configure_ = std::make_unique<LoggerLevelConfigure>(this); | ||
// } | ||
|
||
#ifndef TIER4_AUTOWARE_UTILS__ROS__LOGGER_LEVEL_CONFIGURE_HPP_ | ||
#define TIER4_AUTOWARE_UTILS__ROS__LOGGER_LEVEL_CONFIGURE_HPP_ | ||
|
||
#include "logging_demo/srv/config_logger.hpp" | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
namespace tier4_autoware_utils | ||
{ | ||
class LoggerLevelConfigure | ||
{ | ||
private: | ||
using ConfigLogger = logging_demo::srv::ConfigLogger; | ||
|
||
public: | ||
explicit LoggerLevelConfigure(rclcpp::Node * node); | ||
|
||
private: | ||
rclcpp::Logger ros_logger_; | ||
rclcpp::Service<ConfigLogger>::SharedPtr srv_config_logger_; | ||
|
||
void onLoggerConfigService( | ||
const ConfigLogger::Request::SharedPtr request, | ||
const ConfigLogger::Response::SharedPtr response); | ||
}; | ||
|
||
} // namespace tier4_autoware_utils | ||
#endif // TIER4_AUTOWARE_UTILS__ROS__LOGGER_LEVEL_CONFIGURE_HPP_ |
This file contains 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
61 changes: 61 additions & 0 deletions
61
common/tier4_autoware_utils/src/ros/logger_level_configure.cpp
This file contains 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 2023 Tier IV, 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. | ||
|
||
#include "tier4_autoware_utils/ros/logger_level_configure.hpp" | ||
|
||
#include <rcutils/logging.h> | ||
|
||
namespace tier4_autoware_utils | ||
{ | ||
LoggerLevelConfigure::LoggerLevelConfigure(rclcpp::Node * node) : ros_logger_(node->get_logger()) | ||
{ | ||
using std::placeholders::_1; | ||
using std::placeholders::_2; | ||
|
||
srv_config_logger_ = node->create_service<ConfigLogger>( | ||
"~/config_logger", std::bind(&LoggerLevelConfigure::onLoggerConfigService, this, _1, _2)); | ||
} | ||
|
||
void LoggerLevelConfigure::onLoggerConfigService( | ||
const ConfigLogger::Request::SharedPtr request, const ConfigLogger::Response::SharedPtr response) | ||
{ | ||
int logging_severity; | ||
const auto ret_level = rcutils_logging_severity_level_from_string( | ||
request->level.c_str(), rcl_get_default_allocator(), &logging_severity); | ||
|
||
if (ret_level != RCUTILS_RET_OK) { | ||
response->success = false; | ||
RCLCPP_WARN_STREAM( | ||
ros_logger_, "Failed to change logger level for " | ||
<< request->logger_name | ||
<< " due to an invalid logging severity: " << request->level); | ||
return; | ||
} | ||
|
||
const auto ret_set = | ||
rcutils_logging_set_logger_level(request->logger_name.c_str(), logging_severity); | ||
|
||
if (ret_set != RCUTILS_RET_OK) { | ||
response->success = false; | ||
RCLCPP_WARN_STREAM(ros_logger_, "Failed to set logger level for " << request->logger_name); | ||
return; | ||
} | ||
|
||
response->success = true; | ||
RCLCPP_INFO_STREAM( | ||
ros_logger_, "Logger level [" << request->level << "] is set for " << request->logger_name); | ||
return; | ||
} | ||
|
||
} // namespace tier4_autoware_utils |