Skip to content

Commit d7c830a

Browse files
mergify[bot]nadavelkabetsahcorde
authored
Feature: add logger_name property to subscription, publisher, service and client (#1471) (#1474)
(cherry picked from commit 4859c8a) Signed-off-by: Nadav Elkabets <[email protected]> Signed-off-by: Alejandro Hernández Cordero <[email protected]> Co-authored-by: Nadav Elkabets <[email protected]> Co-authored-by: Alejandro Hernández Cordero <[email protected]>
1 parent e54285f commit d7c830a

File tree

13 files changed

+127
-2
lines changed

13 files changed

+127
-2
lines changed

rclpy/rclpy/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ def service_name(self) -> str:
224224
with self.handle:
225225
return self.__client.service_name
226226

227+
@property
228+
def logger_name(self) -> str:
229+
"""Get the name of the logger associated with the node of the client."""
230+
with self.handle:
231+
return self.__client.get_logger_name()
232+
227233
def destroy(self) -> None:
228234
"""
229235
Destroy a container for a ROS service client.

rclpy/rclpy/impl/_rclpy_pybind11.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class Client(Destroyable, Generic[SrvRequestT, SrvResponseT]):
188188
) -> None:
189189
"""Configure whether introspection is enabled."""
190190

191+
def get_logger_name(self) -> str:
192+
"""Get the name of the logger associated with the node of the client."""
193+
191194

192195
class Context(Destroyable):
193196

@@ -276,6 +279,9 @@ class Service(Destroyable, Generic[SrvRequestT, SrvResponseT]):
276279
) -> None:
277280
"""Configure whether introspection is enabled."""
278281

282+
def get_logger_name(self) -> str:
283+
"""Get the name of the logger associated with the node of the service."""
284+
279285

280286
class TypeDescriptionService(Destroyable):
281287

rclpy/rclpy/publisher.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ def topic_name(self) -> str:
8989
def handle(self) -> '_rclpy.Publisher[MsgT]':
9090
return self.__publisher
9191

92+
@property
93+
def logger_name(self) -> str:
94+
"""Get the name of the logger associated with the node of the publisher."""
95+
with self.handle:
96+
return self.__publisher.get_logger_name()
97+
9298
def destroy(self) -> None:
9399
"""
94100
Destroy a container for a ROS publisher.

rclpy/rclpy/service.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ def service_name(self) -> str:
114114
with self.handle:
115115
return self.__service.name
116116

117+
@property
118+
def logger_name(self) -> str:
119+
"""Get the name of the logger associated with the node of the service."""
120+
with self.handle:
121+
return self.__service.get_logger_name()
122+
117123
def destroy(self) -> None:
118124
"""
119125
Destroy a container for a ROS service server.

rclpy/rclpy/subscription.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ def callback(self, value: Union[Callable[[MsgT], None],
134134
'Subscription.__init__(): callback should be either be callable with one argument'
135135
'(to get only the message) or two (to get message and message info)')
136136

137+
@property
138+
def logger_name(self) -> str:
139+
"""Get the name of the logger associated with the node of the subscription."""
140+
with self.handle:
141+
return self.__subscription.get_logger_name()
142+
137143
def __enter__(self) -> 'Subscription[MsgT]':
138144
return self
139145

rclpy/src/rclpy/client.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ Client::get_service_name()
170170
return rcl_client_get_service_name(rcl_client_.get());
171171
}
172172

173+
const char *
174+
Client::get_logger_name() const
175+
{
176+
const char * node_logger_name = rcl_node_get_logger_name(node_.rcl_ptr());
177+
if (!node_logger_name) {
178+
throw RCLError("Node logger name not set");
179+
}
180+
181+
return node_logger_name;
182+
}
183+
173184
void
174185
define_client(py::object module)
175186
{
@@ -194,6 +205,9 @@ define_client(py::object module)
194205
"Take a received response from an earlier request")
195206
.def(
196207
"configure_introspection", &Client::configure_introspection,
197-
"Configure whether introspection is enabled");
208+
"Configure whether introspection is enabled")
209+
.def(
210+
"get_logger_name", &Client::get_logger_name,
211+
"Get the name of the logger associated with the node of the client.");
198212
}
199213
} // namespace rclpy

rclpy/src/rclpy/client.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ class Client : public Destroyable, public std::enable_shared_from_this<Client>
111111
const char *
112112
get_service_name();
113113

114+
/// Get the name of the logger associated with the node of the client.
115+
/**
116+
*
117+
* \return logger_name, or
118+
* \return None on failure
119+
*/
120+
const char *
121+
get_logger_name() const;
122+
114123
private:
115124
Node node_;
116125
std::shared_ptr<rcl_client_t> rcl_client_;

rclpy/src/rclpy/service.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ Service::get_qos_profile()
155155
return rclpy::convert_to_qos_dict(&options->qos);
156156
}
157157

158+
const char *
159+
Service::get_logger_name() const
160+
{
161+
const char * node_logger_name = rcl_node_get_logger_name(node_.rcl_ptr());
162+
if (!node_logger_name) {
163+
throw RCLError("Node logger name not set");
164+
}
165+
166+
return node_logger_name;
167+
}
168+
158169
void
159170
Service::configure_introspection(
160171
Clock & clock, py::object pyqos_service_event_pub,
@@ -197,6 +208,9 @@ define_service(py::object module)
197208
"Take a request from a given service")
198209
.def(
199210
"configure_introspection", &Service::configure_introspection,
200-
"Configure whether introspection is enabled");
211+
"Configure whether introspection is enabled")
212+
.def(
213+
"get_logger_name", &Service::get_logger_name,
214+
"Get the name of the logger associated with the node of the service.");
201215
}
202216
} // namespace rclpy

rclpy/src/rclpy/service.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ class Service : public Destroyable, public std::enable_shared_from_this<Service>
9595
const char *
9696
get_service_name();
9797

98+
/// Get the name of the logger associated with the node of the service.
99+
/**
100+
*
101+
* \return logger_name, or
102+
* \return None on failure
103+
*/
104+
const char *
105+
get_logger_name() const;
106+
98107
/// Get the QoS profile for this service.
99108
py::dict
100109
get_qos_profile();

rclpy/test/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ def _service(request, response):
264264
executor.shutdown()
265265
executor_thread.join()
266266

267+
def test_logger_name_is_equal_to_node_name(self) -> None:
268+
with self.node.create_client(GetParameters, 'get/parameters') as cli:
269+
self.assertEqual(cli.logger_name, 'TestClient')
270+
267271

268272
if __name__ == '__main__':
269273
unittest.main()

0 commit comments

Comments
 (0)