Skip to content

Commit f909278

Browse files
author
Chen Lihui
authored
aligh with rcl that a rosout publisher of a node might not exist (#1196)
* aligh with rcl that a rosout publisher of a node might not exist * check for RCL_RET_NOT_FOUND * print detailed message for exception reset error message for RCL_RET_NOT_FOUND as well * test for a node with rosout disabled Signed-off-by: Chen Lihui <[email protected]>
1 parent 6cd6fd1 commit f909278

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

rclpy/rclpy/impl/rcutils_logger.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,12 @@ def get_child(self, name):
230230

231231
if self.name:
232232
# Prepend the name of this logger
233-
_rclpy.rclpy_logging_rosout_add_sublogger(self.name, name)
234233
fullname = self.name + _rclpy.rclpy_logging_get_separator_string() + name
235234
else:
236235
fullname = name
237236

238237
logger = RcutilsLogger(name=fullname)
239-
if self.name:
238+
if self.name and _rclpy.rclpy_logging_rosout_add_sublogger(self.name, name):
240239
logger.logger_sublogger_namepair = (self.name, name)
241240
return logger
242241

rclpy/src/rclpy/_rclpy_logging.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,19 @@ rclpy_logging_get_logging_directory()
206206
}
207207

208208
/// Add a subordinate logger based on a logger
209-
void
209+
bool
210210
rclpy_logging_rosout_add_sublogger(const char * logger_name, const char * sublogger_name)
211211
{
212212
rclpy::LoggingGuard scoped_logging_guard;
213213
rcl_ret_t rcl_ret = rcl_logging_rosout_add_sublogger(logger_name, sublogger_name);
214-
if (RCL_RET_OK != rcl_ret) {
215-
rcutils_reset_error();
216-
throw std::runtime_error("Failed to call rcl_logging_rosout_add_sublogger");
214+
if (RCL_RET_OK == rcl_ret) {
215+
return true;
216+
} else if (RCL_RET_NOT_FOUND == rcl_ret) {
217+
rcl_reset_error();
218+
return false;
219+
} else {
220+
throw std::runtime_error(
221+
rclpy::append_rcl_error("Failed to call rcl_logging_rosout_add_sublogger"));
217222
}
218223
}
219224

@@ -225,7 +230,7 @@ rclpy_logging_rosout_remove_sublogger(const char * logger_name, const char * sub
225230
rcl_ret_t rcl_ret = rcl_logging_rosout_remove_sublogger(logger_name, sublogger_name);
226231

227232
if (RCL_RET_OK != rcl_ret) {
228-
rcutils_reset_error();
233+
rcl_reset_error();
229234
}
230235
}
231236

rclpy/test/test_rosout_subscription.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ def call_logger(logger):
114114
self.executor.spin_until_future_complete(self.fut, 3)
115115
self.assertTrue(self.fut.done())
116116

117-
def test_node_logger_not_exist(self):
118-
node = rclpy.create_node('test_extra_node', context=self.context)
119-
logger = node.get_logger()
120-
node = None
121-
with self.assertRaises(RuntimeError):
122-
logger.get_child('child')
117+
def test_logger_rosout_disabled_without_exception(self):
118+
node = rclpy.create_node('mynode', context=self.context, enable_rosout=False)
119+
try:
120+
logger = node.get_logger().get_child('child')
121+
logger.info('test')
122+
except Exception as e:
123+
self.fail(f'Not expected failure: {e}')
123124

124125

125126
if __name__ == '__main__':

0 commit comments

Comments
 (0)