ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

Based on the information provided in the comments above, I propose to use the traceback module to retrieve a stack trace. For example, in the given minimal reproducible example, it can be achieved in the following manner:

Code:

ravi@dell:~/ros_ws/src/minimal_publisher/examples_rclpy_minimal_publisher$ cat publisher_member_function.py 
import rclpy
from rclpy.node import Node
from rclpy.executors import MultiThreadedExecutor

import traceback


class TimerNode(Node):
    def __init__(self):
        super().__init__("timer_test_node")
        self.timer = self.create_timer(
            0.1,
            self.timer_callback,
        )

    def timer_callback(self):
        print("Timer callback called")
        try:
            variable
        except Exception:
            self.get_logger().err(traceback.format_exc())


def main(*args, **kwargs):
    rclpy.init(*args, **kwargs)
    node = TimerNode(*args, **kwargs)

    executor = MultiThreadedExecutor(num_threads=4)
    executor.add_node(node)
    executor.spin()


if __name__ == "__main__":
    main()

Run:

$ ros2 run examples_rclpy_minimal_publisher publisher_member_function
Timer callback called
[ERROR] [1663553499.045761051] [timer_test_node]: Traceback (most recent call last):
  File "/home/ravi/ros_ws/install/examples_rclpy_minimal_publisher/lib/python3.8/site-packages/examples_rclpy_minimal_publisher/publisher_member_function.py", line 19, in timer_callback
    variable
NameError: name 'variable' is not defined