|  | 
| 19 | 19 | #include <rcl/error_handling.h> | 
| 20 | 20 | #include <rcl/expand_topic_name.h> | 
| 21 | 21 | #include <rcl/graph.h> | 
|  | 22 | +#include <rcl/logging.h> | 
| 22 | 23 | #include <rcl/node.h> | 
| 23 | 24 | #include <rcl/publisher.h> | 
| 24 | 25 | #include <rcl/rcl.h> | 
| @@ -618,6 +619,56 @@ rclpy_init(PyObject * Py_UNUSED(self), PyObject * args) | 
| 618 | 619 |   Py_RETURN_NONE; | 
| 619 | 620 | } | 
| 620 | 621 | 
 | 
|  | 622 | +/// Initialize rcl logging | 
|  | 623 | +/** | 
|  | 624 | + * Raises RuntimeError if rcl logging could not be initialized | 
|  | 625 | + */ | 
|  | 626 | +static PyObject * | 
|  | 627 | +rclpy_logging_configure(PyObject * Py_UNUSED(self), PyObject * args) | 
|  | 628 | +{ | 
|  | 629 | +  // Expect one argument, a context. | 
|  | 630 | +  PyObject * pycontext; | 
|  | 631 | +  if (!PyArg_ParseTuple(args, "O", &pycontext)) { | 
|  | 632 | +    // Exception raised | 
|  | 633 | +    return NULL; | 
|  | 634 | +  } | 
|  | 635 | +  rcl_context_t * context = (rcl_context_t *)PyCapsule_GetPointer(pycontext, "rcl_context_t"); | 
|  | 636 | +  if (!context) { | 
|  | 637 | +    return NULL; | 
|  | 638 | +  } | 
|  | 639 | +  rcl_allocator_t allocator = rcl_get_default_allocator(); | 
|  | 640 | +  rcl_ret_t ret = rcl_logging_configure( | 
|  | 641 | +    &context->global_arguments, | 
|  | 642 | +    &allocator); | 
|  | 643 | +  if (RCL_RET_OK != ret) { | 
|  | 644 | +    PyErr_Format( | 
|  | 645 | +      RCLError, | 
|  | 646 | +      "Failed to initialize logging: %s", rcl_get_error_string().str); | 
|  | 647 | +    return NULL; | 
|  | 648 | +  } | 
|  | 649 | +  Py_RETURN_NONE; | 
|  | 650 | +} | 
|  | 651 | + | 
|  | 652 | +/// Finalize rcl logging | 
|  | 653 | +/** | 
|  | 654 | + * Raises RuntimeError if rcl logging could not be finalized | 
|  | 655 | + */ | 
|  | 656 | +static PyObject * | 
|  | 657 | +rclpy_logging_fini(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args)) | 
|  | 658 | +{ | 
|  | 659 | +  rcl_ret_t ret = rcl_logging_fini(); | 
|  | 660 | +  if (RCL_RET_OK != ret) { | 
|  | 661 | +    int stack_level = 1; | 
|  | 662 | +    PyErr_WarnFormat( | 
|  | 663 | +      PyExc_RuntimeWarning, | 
|  | 664 | +      stack_level, | 
|  | 665 | +      "Failed to fini logging: %s", | 
|  | 666 | +      rcl_get_error_string().str); | 
|  | 667 | +    return NULL; | 
|  | 668 | +  } | 
|  | 669 | +  Py_RETURN_NONE; | 
|  | 670 | +} | 
|  | 671 | + | 
| 621 | 672 | /// PyCapsule destructor for node | 
| 622 | 673 | static void | 
| 623 | 674 | _rclpy_destroy_node(PyObject * pyentity) | 
| @@ -5016,6 +5067,14 @@ static PyMethodDef rclpy_methods[] = { | 
| 5016 | 5067 |     "rclpy_init", rclpy_init, METH_VARARGS, | 
| 5017 | 5068 |     "Initialize RCL." | 
| 5018 | 5069 |   }, | 
|  | 5070 | +  { | 
|  | 5071 | +    "rclpy_logging_configure", rclpy_logging_configure, METH_VARARGS, | 
|  | 5072 | +    "Initialize RCL logging." | 
|  | 5073 | +  }, | 
|  | 5074 | +  { | 
|  | 5075 | +    "rclpy_logging_fini", rclpy_logging_fini, METH_NOARGS, | 
|  | 5076 | +    "Finalize RCL logging." | 
|  | 5077 | +  }, | 
| 5019 | 5078 |   { | 
| 5020 | 5079 |     "rclpy_remove_ros_args", rclpy_remove_ros_args, METH_VARARGS, | 
| 5021 | 5080 |     "Remove ROS-specific arguments from argument vector." | 
|  | 
0 commit comments