Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rclpy/rclpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ def service_name(self) -> str:
with self.handle:
return self.__client.service_name

@property
def logger_name(self) -> str:
"""Get the name of the logger associated with the node of the client."""
with self.handle:
return self.__client.get_logger_name()

def destroy(self) -> None:
"""
Destroy a container for a ROS service client.
Expand Down
6 changes: 6 additions & 0 deletions rclpy/rclpy/impl/_rclpy_pybind11.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class Client(Destroyable, Generic[SrvRequestT, SrvResponseT]):
) -> None:
"""Configure whether introspection is enabled."""

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


class Context(Destroyable):

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

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


class TypeDescriptionService(Destroyable):

Expand Down
6 changes: 6 additions & 0 deletions rclpy/rclpy/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def topic_name(self) -> str:
def handle(self) -> '_rclpy.Publisher[MsgT]':
return self.__publisher

@property
def logger_name(self) -> str:
"""Get the name of the logger associated with the node of the publisher."""
with self.handle:
return self.__publisher.get_logger_name()

def destroy(self) -> None:
"""
Destroy a container for a ROS publisher.
Expand Down
6 changes: 6 additions & 0 deletions rclpy/rclpy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ def service_name(self) -> str:
with self.handle:
return self.__service.name

@property
def logger_name(self) -> str:
"""Get the name of the logger associated with the node of the service."""
with self.handle:
return self.__service.get_logger_name()

def destroy(self) -> None:
"""
Destroy a container for a ROS service server.
Expand Down
6 changes: 6 additions & 0 deletions rclpy/rclpy/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def callback(self, value: Union[Callable[[MsgT], None],
'Subscription.__init__(): callback should be either be callable with one argument'
'(to get only the message) or two (to get message and message info)')

@property
def logger_name(self) -> str:
"""Get the name of the logger associated with the node of the subscription."""
with self.handle:
return self.__subscription.get_logger_name()

def __enter__(self) -> 'Subscription[MsgT]':
return self

Expand Down
16 changes: 15 additions & 1 deletion rclpy/src/rclpy/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ Client::get_service_name()
return rcl_client_get_service_name(rcl_client_.get());
}

const char *
Client::get_logger_name() const
{
const char * node_logger_name = rcl_node_get_logger_name(node_.rcl_ptr());
if (!node_logger_name) {
throw RCLError("Node logger name not set");
}

return node_logger_name;
}

void
define_client(py::object module)
{
Expand All @@ -194,6 +205,9 @@ define_client(py::object module)
"Take a received response from an earlier request")
.def(
"configure_introspection", &Client::configure_introspection,
"Configure whether introspection is enabled");
"Configure whether introspection is enabled")
.def(
"get_logger_name", &Client::get_logger_name,
"Get the name of the logger associated with the node of the client.");
}
} // namespace rclpy
9 changes: 9 additions & 0 deletions rclpy/src/rclpy/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ class Client : public Destroyable, public std::enable_shared_from_this<Client>
const char *
get_service_name();

/// Get the name of the logger associated with the node of the client.
/**
*
* \return logger_name, or
* \return None on failure
*/
const char *
get_logger_name() const;

private:
Node node_;
std::shared_ptr<rcl_client_t> rcl_client_;
Expand Down
16 changes: 15 additions & 1 deletion rclpy/src/rclpy/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ Service::get_qos_profile()
return rclpy::convert_to_qos_dict(&options->qos);
}

const char *
Service::get_logger_name() const
{
const char * node_logger_name = rcl_node_get_logger_name(node_.rcl_ptr());
if (!node_logger_name) {
throw RCLError("Node logger name not set");
}

return node_logger_name;
}

void
Service::configure_introspection(
Clock & clock, py::object pyqos_service_event_pub,
Expand Down Expand Up @@ -197,6 +208,9 @@ define_service(py::object module)
"Take a request from a given service")
.def(
"configure_introspection", &Service::configure_introspection,
"Configure whether introspection is enabled");
"Configure whether introspection is enabled")
.def(
"get_logger_name", &Service::get_logger_name,
"Get the name of the logger associated with the node of the client.");
}
} // namespace rclpy
9 changes: 9 additions & 0 deletions rclpy/src/rclpy/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ class Service : public Destroyable, public std::enable_shared_from_this<Service>
const char *
get_service_name();

/// Get the name of the logger associated with the node of the service.
/**
*
* \return logger_name, or
* \return None on failure
*/
const char *
get_logger_name() const;

/// Get the QoS profile for this service.
py::dict
get_qos_profile();
Expand Down
4 changes: 4 additions & 0 deletions rclpy/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ def _service(request, response):
executor.shutdown()
executor_thread.join()

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


if __name__ == '__main__':
unittest.main()
7 changes: 7 additions & 0 deletions rclpy/test/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ def test_wait_for_all_acked(self) -> None:
pub.destroy()
sub.destroy()

def test_logger_name_is_equal_to_node_name(self) -> None:
pub = self.node.create_publisher(BasicTypes, TEST_TOPIC, 10)
try:
self.assertEqual(pub.logger_name, 'node')
finally:
pub.destroy()


def test_publisher_context_manager() -> None:
rclpy.init()
Expand Down
20 changes: 20 additions & 0 deletions rclpy/test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,33 @@
from test_msgs.srv import Empty


NODE_NAME = 'test_node'


@pytest.fixture(autouse=True)
def default_context():
rclpy.init()
yield
rclpy.shutdown()


@pytest.fixture
def test_node():
node = Node(NODE_NAME)
yield node
node.destroy_node()


def test_logger_name_is_equal_to_node_name(test_node):
srv = test_node.create_service(
srv_type=Empty,
srv_name='test_srv',
callback=lambda _: None
)

assert srv.logger_name == NODE_NAME


@pytest.mark.parametrize('service_name, namespace, expected', [
# No namespaces
('service', None, '/service'),
Expand Down
21 changes: 21 additions & 0 deletions rclpy/test/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@
from test_msgs.msg import Empty


NODE_NAME = 'test_node'


@pytest.fixture(scope='session', autouse=True)
def setup_ros() -> None:
rclpy.init()


@pytest.fixture
def test_node():
node = Node(NODE_NAME)
yield node
node.destroy_node()


@pytest.mark.parametrize('topic_name, namespace, expected', [
# No namespaces
('topic', None, '/topic'),
Expand Down Expand Up @@ -56,6 +66,17 @@ def test_get_subscription_topic_name(topic_name, namespace, expected):
node.destroy_node()


def test_logger_name_is_equal_to_node_name(test_node):
sub = test_node.create_subscription(
msg_type=Empty,
topic='topic',
callback=lambda _: None,
qos_profile=10,
)

assert sub.logger_name == NODE_NAME


@pytest.mark.parametrize('topic_name, namespace, cli_args, expected', [
('topic', None, ['--ros-args', '--remap', 'topic:=new_topic'], '/new_topic'),
('topic', 'ns', ['--ros-args', '--remap', 'topic:=new_topic'], '/ns/new_topic'),
Expand Down