Skip to content

Commit d7e0d6d

Browse files
firesurferLennart NachtigallFelix ExnerLennart Nachtigall
authored
Introduced tf_prefix into log handler (#713)
* Introduced tf_prefix into log handler * added default argument to prefix --------- Co-authored-by: Lennart Nachtigall <[email protected]> Co-authored-by: Felix Exner <[email protected]> Co-authored-by: Lennart Nachtigall <[email protected]>
1 parent 79bfddc commit d7e0d6d

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

ur_robot_driver/include/ur_robot_driver/urcl_log_handler.hpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,22 @@
4343
#ifndef UR_ROBOT_DRIVER__URCL_LOG_HANDLER_HPP_
4444
#define UR_ROBOT_DRIVER__URCL_LOG_HANDLER_HPP_
4545

46+
#include <string>
4647
#include "ur_client_library/log.h"
47-
4848
namespace ur_robot_driver
4949
{
50+
51+
/*!
52+
* \brief Register the UrclLoghHandler, this will start logging messages from the client library with ROS2 logging.
53+
* This function has to be called inside your node, to enable the log handler.
54+
*/
55+
void registerUrclLogHandler(const std::string& tf_prefix = "");
56+
57+
/*!
58+
* \brief Unregister the UrclLoghHandler, stop logging messages from the client library with ROS2 logging.
59+
*/
60+
void unregisterUrclLogHandler();
61+
5062
/*!
5163
* \brief Loghandler for handling messages logged with the C++ client library. This loghandler will log the messages
5264
* from the client library with ROS2s logging.
@@ -69,18 +81,31 @@ class UrclLogHandler : public urcl::LogHandler
6981
* \param log Log message
7082
*/
7183
void log(const char* file, int line, urcl::LogLevel loglevel, const char* message) override;
72-
};
7384

74-
/*!
75-
* \brief Register the UrclLoghHandler, this will start logging messages from the client library with ROS2 logging.
76-
* This function has to be called inside your node, to enable the log handler.
77-
*/
78-
void registerUrclLogHandler();
85+
/**
86+
* @brief getTFPrefix - obtain the currently set tf_prefix
87+
* @return
88+
*/
89+
const std::string& getTFPrefix() const
90+
{
91+
return tf_prefix_;
92+
}
7993

80-
/*!
81-
* \brief Unregister the UrclLoghHandler, stop logging messages from the client library with ROS2 logging.
82-
*/
83-
void unregisterUrclLogHandler();
94+
private:
95+
std::string tf_prefix_{};
96+
97+
/**
98+
* @brief setTFPrefix - set the tf_prefix the logger will append to the node name
99+
* @param tf_prefix
100+
*/
101+
void setTFPrefix(const std::string& tf_prefix)
102+
{
103+
tf_prefix_ = tf_prefix;
104+
}
105+
106+
// Declare the register method as a friend so that we can access setTFPrefix from it
107+
friend void registerUrclLogHandler(const std::string& tf_prefix);
108+
};
84109

85110
} // namespace ur_robot_driver
86111

ur_robot_driver/src/dashboard_client_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int main(int argc, char** argv)
5151
std::string robot_ip = node->declare_parameter<std::string>("robot_ip", "192.168.56.101");
5252
node->get_parameter<std::string>("robot_ip", robot_ip);
5353

54-
ur_robot_driver::registerUrclLogHandler();
54+
ur_robot_driver::registerUrclLogHandler(""); // Set empty tf_prefix at the moment
5555

5656
ur_robot_driver::DashboardClientROS client(node, robot_ip);
5757

ur_robot_driver/src/hardware_interface.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,11 @@ URPositionHardwareInterface::on_activate(const rclcpp_lifecycle::State& previous
417417
// Amount of allowed timed out reads before the reverse interface will be dropped
418418
const int keep_alive_count = std::stoi(info_.hardware_parameters["keep_alive_count"]);
419419

420+
// Obtain the tf_prefix which is needed for the logging handler so that log messages from different arms are
421+
// distiguishable in the log
422+
const std::string tf_prefix = info_.hardware_parameters.at("tf_prefix");
420423
RCLCPP_INFO(rclcpp::get_logger("URPositionHardwareInterface"), "Initializing driver...");
421-
registerUrclLogHandler();
424+
registerUrclLogHandler(tf_prefix);
422425
try {
423426
rtde_comm_has_been_started_ = false;
424427
ur_driver_ = std::make_unique<urcl::UrDriver>(

ur_robot_driver/src/urcl_log_handler.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,33 @@ UrclLogHandler::UrclLogHandler() = default;
5656
void UrclLogHandler::log(const char* file, int line, urcl::LogLevel loglevel, const char* message)
5757
{
5858
rcutils_log_location_t location = { "", file, static_cast<size_t>(line) };
59+
60+
const auto logger_name = "UR_Client_Library:" + tf_prefix_;
5961
switch (loglevel) {
6062
case urcl::LogLevel::DEBUG:
61-
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_DEBUG, "UR_Client_Library", "%s", message);
63+
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_DEBUG, logger_name.c_str(), "%s", message);
6264
break;
6365
case urcl::LogLevel::INFO:
64-
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_INFO, "UR_Client_Library", "%s", message);
66+
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_INFO, logger_name.c_str(), "%s", message);
6567
break;
6668
case urcl::LogLevel::WARN:
67-
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_WARN, "UR_Client_Library", "%s", message);
69+
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_WARN, logger_name.c_str(), "%s", message);
6870
break;
6971
case urcl::LogLevel::ERROR:
70-
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_ERROR, "UR_Client_Library", "%s", message);
72+
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_ERROR, logger_name.c_str(), "%s", message);
7173
break;
7274
case urcl::LogLevel::FATAL:
73-
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_FATAL, "UR_Client_Library", "%s", message);
75+
rcutils_log(&location, RCUTILS_LOG_SEVERITY::RCUTILS_LOG_SEVERITY_FATAL, logger_name.c_str(), "%s", message);
7476
break;
7577
default:
7678
break;
7779
}
7880
}
7981

80-
void registerUrclLogHandler()
82+
void registerUrclLogHandler(const std::string& tf_prefix)
8183
{
8284
if (g_registered == false) {
85+
g_log_handler->setTFPrefix(tf_prefix);
8386
// Log level is decided by ROS2 log level
8487
urcl::setLogLevel(urcl::LogLevel::DEBUG);
8588
urcl::registerLogHandler(std::move(g_log_handler));

0 commit comments

Comments
 (0)