Skip to content

Commit 92af8ea

Browse files
authored
Added log handler for handling log messages from client library with … (#398)
* Added log handler for handling log messages from client library with ROS logging
1 parent 8269e2a commit 92af8ea

File tree

8 files changed

+199
-3
lines changed

8 files changed

+199
-3
lines changed

ur_calibration/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_compile_options(-std=c++11)
66

77
find_package(catkin REQUIRED COMPONENTS
88
roscpp
9+
ur_robot_driver
910
)
1011
find_package(Eigen3 REQUIRED)
1112
find_package(yaml-cpp REQUIRED)
@@ -17,6 +18,7 @@ set(YAML_CPP_INCLUDE_DIRS ${YAML_CPP_INCLUDE_DIR})
1718
catkin_package(
1819
CATKIN_DEPENDS
1920
roscpp
21+
ur_robot_driver
2022
DEPENDS
2123
YAML_CPP
2224
ur_client_library

ur_calibration/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<depend>roscpp</depend>
5454
<depend>ur_client_library</depend>
5555
<depend>yaml-cpp</depend>
56+
<depend>ur_robot_driver</depend>
5657

5758

5859
<test_depend>rosunit</test_depend>

ur_calibration/src/calibration_correction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <ur_client_library/primary/package_header.h>
3535
#include <ur_client_library/primary/primary_parser.h>
3636

37+
#include <ur_robot_driver/urcl_log_handler.h>
38+
3739
#include <sensor_msgs/JointState.h>
3840
#include <tf/transform_listener.h>
3941
#include <ros/package.h>
@@ -162,6 +164,8 @@ int main(int argc, char* argv[])
162164
ros::init(argc, argv, "ur_calibration");
163165
ros::NodeHandle nh("~");
164166

167+
ur_driver::registerUrclLogHandler();
168+
165169
try
166170
{
167171
CalibrationCorrection my_calibration_correction(nh);

ur_robot_driver/CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ catkin_package(
3636
include
3737
LIBRARIES
3838
ur_robot_driver_plugin
39+
urcl_log_handler
3940
CATKIN_DEPENDS
4041
actionlib
4142
control_msgs
@@ -88,19 +89,24 @@ add_library(ur_robot_driver_plugin
8889
target_link_libraries(ur_robot_driver_plugin ur_client_library::urcl ${catkin_LIBRARIES})
8990
add_dependencies(ur_robot_driver_plugin ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
9091

92+
add_library(urcl_log_handler
93+
src/urcl_log_handler.cpp
94+
)
95+
target_link_libraries(urcl_log_handler ${catkin_LIBRARIES} ur_client_library::urcl)
96+
9197
add_executable(ur_robot_driver_node
9298
src/dashboard_client_ros.cpp
9399
src/hardware_interface.cpp
94100
src/hardware_interface_node.cpp
95101
)
96-
target_link_libraries(ur_robot_driver_node ${catkin_LIBRARIES} ur_client_library::urcl)
102+
target_link_libraries(ur_robot_driver_node ${catkin_LIBRARIES} ur_client_library::urcl urcl_log_handler)
97103
add_dependencies(ur_robot_driver_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
98104

99105
add_executable(dashboard_client
100106
src/dashboard_client_ros.cpp
101107
src/dashboard_client_node.cpp
102108
)
103-
target_link_libraries(dashboard_client ${catkin_LIBRARIES} ur_client_library::urcl)
109+
target_link_libraries(dashboard_client ${catkin_LIBRARIES} ur_client_library::urcl urcl_log_handler)
104110
add_dependencies(dashboard_client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
105111

106112
add_executable(robot_state_helper
@@ -117,7 +123,7 @@ if(CATKIN_ENABLE_TESTING)
117123
endif()
118124

119125

120-
install(TARGETS ur_robot_driver_plugin ur_robot_driver_node robot_state_helper dashboard_client
126+
install(TARGETS ur_robot_driver_plugin urcl_log_handler ur_robot_driver_node robot_state_helper dashboard_client
121127
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
122128
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
123129
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// -- BEGIN LICENSE BLOCK ----------------------------------------------
2+
// Copyright 2021 Universal Robots A/S
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// All source code contained in and/or linked to in this message (the “Source Code”) is subject to the copyright of
17+
// Universal Robots A/S and/or its licensors. THE SOURCE CODE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EXPRESS
18+
// OR IMPLIED, INCLUDING – BUT NOT LIMITED TO – WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
19+
// NONINFRINGEMENT. USE OF THE SOURCE CODE IS AT YOUR OWN RISK AND UNIVERSAL ROBOTS A/S AND ITS LICENSORS SHALL, TO THE
20+
// MAXIMUM EXTENT PERMITTED BY LAW, NOT BE LIABLE FOR ANY ERRORS OR MALICIOUS CODE IN THE SOURCE CODE, ANY THIRD-PARTY
21+
// CLAIMS, OR ANY OTHER CLAIMS AND DAMAGES, INCLUDING INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES,
22+
// OR ANY LOSS OF PROFITS, EXPECTED SAVINGS, OR REVENUES, WHETHER INCURRED DIRECTLY OR INDIRECTLY, OR ANY LOSS OF DATA,
23+
// USE, GOODWILL, OR OTHER INTANGIBLE LOSSES, RESULTING FROM YOUR USE OF THE SOURCE CODE. You may make copies of the
24+
// Source Code for use in connection with a Universal Robots or UR+ product, provided that you include (i) an
25+
// appropriate copyright notice (“© [the year in which you received the Source Code or the Source Code was first
26+
// published, e.g. “2021”] Universal Robots A/S and/or its licensors”) along with the capitalized section of this notice
27+
// in all copies of the Source Code. By using the Source Code, you agree to the above terms. For more information,
28+
// please contact [email protected].
29+
// -- END LICENSE BLOCK ------------------------------------------------
30+
31+
#ifndef UR_DRIVER_URCL_LOG_HANDLER_H_INCLUDED
32+
#define UR_DRIVER_URCL_LOG_HANDLER_H_INCLUDED
33+
34+
#include <ur_client_library/log.h>
35+
36+
namespace ur_driver
37+
{
38+
/*!
39+
* \brief Loghandler for handling messages logged with the C++ client library. This loghandler will log the messages
40+
* from the client library with ROS logging.
41+
* Use registerLogHandler to register this class. This class shouldn't be instantiated directly.
42+
*/
43+
class UrclLogHandler : public urcl::LogHandler
44+
{
45+
public:
46+
/*!
47+
* \brief Default constructor
48+
*/
49+
UrclLogHandler();
50+
51+
/*!
52+
* \brief Function to log a message
53+
*
54+
* \param file The log message comes from this file
55+
* \param line The log message comes from this line
56+
* \param loglevel Indicates the severity of the log message
57+
* \param log Log message
58+
*/
59+
void log(const char* file, int line, urcl::LogLevel loglevel, const char* message) override;
60+
61+
private:
62+
std::string log_name_;
63+
64+
void logMessage(const char* file, int line, ros::console::Level level, const char* message);
65+
};
66+
67+
/*!
68+
* \brief Register the UrclLoghHandler, this will start logging messages from the client library with RPS2 logging.
69+
* This function has to be called inside your node, to enable the log handler.
70+
*/
71+
void registerUrclLogHandler();
72+
73+
/*!
74+
* \brief Unregister the UrclLoghHandler, stop logging messages from the client library with ROS2 logging.
75+
*/
76+
void unregisterUrclLogHandler();
77+
78+
} // namespace ur_driver
79+
80+
#endif // UR_DRIVER_URCL_LOG_HANDLER_H_INCLUDED

ur_robot_driver/src/dashboard_client_node.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828
#include <ros/ros.h>
2929
#include <ur_robot_driver/dashboard_client_ros.h>
3030

31+
#include <ur_robot_driver/urcl_log_handler.h>
32+
3133
int main(int argc, char** argv)
3234
{
3335
// Set up ROS.
3436
ros::init(argc, argv, "dashboard_client");
3537
ros::NodeHandle priv_nh("~");
3638

39+
ur_driver::registerUrclLogHandler();
40+
3741
// The IP address under which the robot is reachable.
3842
std::string robot_ip = priv_nh.param<std::string>("robot_ip", "192.168.56.101");
3943

ur_robot_driver/src/hardware_interface_node.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <csignal>
3131
#include <ur_robot_driver/hardware_interface.h>
32+
#include <ur_robot_driver/urcl_log_handler.h>
3233

3334
std::unique_ptr<ur_driver::HardwareInterface> g_hw_interface;
3435

@@ -56,6 +57,8 @@ int main(int argc, char** argv)
5657
// register signal SIGINT and signal handler
5758
signal(SIGINT, signalHandler);
5859

60+
ur_driver::registerUrclLogHandler();
61+
5962
std::ifstream realtime_file("/sys/kernel/realtime", std::ios::in);
6063
bool has_realtime = false;
6164
if (realtime_file.is_open())
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// -- BEGIN LICENSE BLOCK ----------------------------------------------
2+
// Copyright 2021 Universal Robots A/S
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// All source code contained in and/or linked to in this message (the “Source Code”) is subject to the copyright of
17+
// Universal Robots A/S and/or its licensors. THE SOURCE CODE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EXPRESS
18+
// OR IMPLIED, INCLUDING – BUT NOT LIMITED TO – WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
19+
// NONINFRINGEMENT. USE OF THE SOURCE CODE IS AT YOUR OWN RISK AND UNIVERSAL ROBOTS A/S AND ITS LICENSORS SHALL, TO THE
20+
// MAXIMUM EXTENT PERMITTED BY LAW, NOT BE LIABLE FOR ANY ERRORS OR MALICIOUS CODE IN THE SOURCE CODE, ANY THIRD-PARTY
21+
// CLAIMS, OR ANY OTHER CLAIMS AND DAMAGES, INCLUDING INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES,
22+
// OR ANY LOSS OF PROFITS, EXPECTED SAVINGS, OR REVENUES, WHETHER INCURRED DIRECTLY OR INDIRECTLY, OR ANY LOSS OF DATA,
23+
// USE, GOODWILL, OR OTHER INTANGIBLE LOSSES, RESULTING FROM YOUR USE OF THE SOURCE CODE. You may make copies of the
24+
// Source Code for use in connection with a Universal Robots or UR+ product, provided that you include (i) an
25+
// appropriate copyright notice (“© [the year in which you received the Source Code or the Source Code was first
26+
// published, e.g. “2021”] Universal Robots A/S and/or its licensors”) along with the capitalized section of this notice
27+
// in all copies of the Source Code. By using the Source Code, you agree to the above terms. For more information,
28+
// please contact [email protected].
29+
// -- END LICENSE BLOCK ------------------------------------------------
30+
31+
#include <ros/console.h>
32+
#include <ur_robot_driver/urcl_log_handler.h>
33+
34+
namespace ur_driver
35+
{
36+
bool g_registered = false;
37+
std::unique_ptr<UrclLogHandler> g_log_handler(new UrclLogHandler);
38+
39+
UrclLogHandler::UrclLogHandler() : log_name_(std::string(ROSCONSOLE_NAME_PREFIX) + ".ur_client_library")
40+
{
41+
}
42+
43+
void UrclLogHandler::log(const char* file, int line, urcl::LogLevel loglevel, const char* message)
44+
{
45+
switch (loglevel)
46+
{
47+
case urcl::LogLevel::DEBUG:
48+
logMessage(file, line, ros::console::levels::Debug, message);
49+
break;
50+
case urcl::LogLevel::INFO:
51+
logMessage(file, line, ros::console::levels::Info, message);
52+
break;
53+
case urcl::LogLevel::WARN:
54+
logMessage(file, line, ros::console::levels::Warn, message);
55+
break;
56+
case urcl::LogLevel::ERROR:
57+
logMessage(file, line, ros::console::levels::Error, message);
58+
break;
59+
case urcl::LogLevel::FATAL:
60+
logMessage(file, line, ros::console::levels::Fatal, message);
61+
break;
62+
default:
63+
break;
64+
}
65+
}
66+
67+
void UrclLogHandler::logMessage(const char* file, int line, ros::console::Level level, const char* message)
68+
{
69+
ROSCONSOLE_DEFINE_LOCATION(true, level, log_name_);
70+
if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
71+
{
72+
ros::console::print(NULL, __rosconsole_define_location__loc.logger_, level, file, line, "", "%s", message);
73+
}
74+
}
75+
76+
void registerUrclLogHandler()
77+
{
78+
if (g_registered == false)
79+
{
80+
// Log level is decided by ROS log level
81+
urcl::setLogLevel(urcl::LogLevel::DEBUG);
82+
urcl::registerLogHandler(std::move(g_log_handler));
83+
g_registered = true;
84+
}
85+
}
86+
87+
void unregisterUrclLogHandler()
88+
{
89+
if (g_registered == true)
90+
{
91+
urcl::unregisterLogHandler();
92+
g_registered = false;
93+
}
94+
}
95+
96+
} // namespace ur_driver

0 commit comments

Comments
 (0)