Пример #1
0
def test_enable_rosout(name, enable_global_rosout_logs, enable_node_rosout,
                       expected_data):
    if enable_global_rosout_logs:
        args = ['--ros-args', '--enable-rosout-logs']
    else:
        args = ['--ros-args', '--disable-rosout-logs']

    context = rclpy.context.Context()
    rclpy.init(context=context, args=args)
    executor = SingleThreadedExecutor(context=context)

    # create node
    node = rclpy.create_node(node_name='my_node_' + name,
                             namespace='/my_ns',
                             enable_rosout=enable_node_rosout,
                             context=context)
    executor.add_node(node)

    global rosout_subscription_msg
    rosout_subscription_msg = None
    # create subscriber of 'rosout' topic
    node.create_subscription(Log, '/rosout', rosout_subscription_callback, 1)

    max_difference_time = 5
    begin_time = time.time()
    message_data = 'SOMETHING'
    while rosout_subscription_msg is None and int(
            time.time() - begin_time) <= max_difference_time:
        node.get_logger().info(message_data)
        executor.spin_once(timeout_sec=1)

    if expected_data:
        assert (rosout_subscription_msg is not None)
        assert (type(rosout_subscription_msg) == Log)
        assert (LoggingSeverity(
            rosout_subscription_msg.level) == LoggingSeverity.INFO)
        assert (len(rosout_subscription_msg.msg) != 0)
        assert (rosout_subscription_msg.msg == message_data)
    else:
        assert (rosout_subscription_msg is None)

    node.destroy_node()
    rclpy.shutdown(context=context)
Пример #2
0
    def log(self, message, severity, **kwargs):
        r"""
        Log a message with the specified severity.

        The message will not be logged if:
          * the logger is not enabled for the message's severity (the message severity is less than
            the level of the logger), or
          * a logging filter causes the message to be skipped.

        .. note::
           Logging filters will only be evaluated if the logger is enabled for the message's
           severity.

        :param message str: message to log.
        :param severity: severity of the message.
        :type severity: :py:class:LoggingSeverity
        :keyword name str: name of the logger to use.
        :param \**kwargs: optional parameters for logging filters (see below).

        :Keyword Arguments:
            * *throttle_duration_sec* (``float``) --
              Duration of the throttle interval for the :py:class:Throttle: filter.
            * *throttle_time_source_type* (``str``) --
              Optional time source type for the :py:class:Throttle: filter (default of
              ``RCUTILS_STEADY_TIME``)
            * *skip_first* (``bool``) --
              If True, enable the :py:class:SkipFirst: filter.
            * *once* (``bool``) --
              If True, enable the :py:class:Once: filter.
        :returns: False if a filter caused the message to not be logged; True otherwise.
        :raises: TypeError on invalid filter parameter combinations.
        :raises: ValueError on invalid parameters values.
        :rtype: bool
        """
        # Gather context info and check filters only if the severity is appropriate.
        if not self.is_enabled_for(severity):
            return False

        from rclpy.logging import LoggingSeverity
        severity = LoggingSeverity(severity)

        name = kwargs.pop('name', self.name)

        # Infer the requested log filters from the keyword arguments
        detected_filters = get_filters_from_kwargs(**kwargs)

        # Get/prepare the context corresponding to the caller.
        caller_id = CallerId()
        if caller_id not in self.contexts:
            context = {'name': name, 'severity': severity}
            for detected_filter in detected_filters:
                if detected_filter in supported_filters:
                    supported_filters[detected_filter].initialize_context(
                        context, **kwargs)
            context['filters'] = detected_filters
            self.contexts[caller_id] = context
        else:
            context = self.contexts[caller_id]
            # Don't support any changes to the logger.
            if severity != context['severity']:
                raise ValueError(
                    'Logger severity cannot be changed between calls.')
            if name != context['name']:
                raise ValueError(
                    'Logger name cannot be changed between calls.')
            if detected_filters != context['filters']:
                raise ValueError(
                    'Requested logging filters cannot be changed between calls.'
                )
            for detected_filter in detected_filters:
                filter_params = supported_filters[detected_filter].params
                if any(context[p] != kwargs.get(p, filter_params[p])
                       for p in filter_params):
                    raise ValueError(
                        'Logging filter parameters cannot be changed between calls.'
                    )

        # Check if any filter determines the message shouldn't be processed.
        # Note(dhood): even if a message doesn't get logged, a filter might still update its state
        # as if it had been. This matches the behavior of the C logging macros provided by rcutils.
        for logging_filter in context['filters']:
            if not supported_filters[logging_filter].should_log(context):
                return False

        # Call the relevant function from the C extension.
        _rclpy.rclpy_logging_rcutils_log(severity, name, message,
                                         caller_id.function_name,
                                         caller_id.file_path,
                                         caller_id.line_number)
        return True
Пример #3
0
 def is_enabled_for(self, severity):
     from rclpy.logging import LoggingSeverity
     severity = LoggingSeverity(severity)
     return _rclpy.rclpy_logging_logger_is_enabled_for(self.name, severity)
Пример #4
0
 def get_effective_level(self):
     from rclpy.logging import LoggingSeverity
     level = LoggingSeverity(
         _rclpy.rclpy_logging_get_logger_effective_level(self.name))
     return level
Пример #5
0
 def set_level(self, level):
     from rclpy.logging import LoggingSeverity
     level = LoggingSeverity(level)
     return _rclpy.rclpy_logging_set_logger_level(self.name, level)