Skip to content

Commit

Permalink
feat(logger_level_configure): add util to set logger level externally…
Browse files Browse the repository at this point in the history
… 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
TakaHoribe authored Sep 27, 2023
1 parent 7fdda42 commit bb89363
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/tier4_autoware_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ament_auto_add_library(tier4_autoware_utils SHARED
src/math/trigonometry.cpp
src/ros/msg_operation.cpp
src/ros/marker_helper.cpp
src/ros/logger_level_configure.cpp
src/system/backtrace.cpp
)

Expand Down
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_
1 change: 1 addition & 0 deletions common/tier4_autoware_utils/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<depend>diagnostic_msgs</depend>
<depend>geometry_msgs</depend>
<depend>libboost-dev</depend>
<depend>logging_demo</depend>
<depend>pcl_conversions</depend>
<depend>pcl_ros</depend>
<depend>rclcpp</depend>
Expand Down
61 changes: 61 additions & 0 deletions common/tier4_autoware_utils/src/ros/logger_level_configure.cpp
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

0 comments on commit bb89363

Please sign in to comment.