<commit_before># Copyright 2017 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
import time
import unittest

import rclpy
from rclpy.logging import LoggingSeverity


class TestLogging(unittest.TestCase):

    def test_root_logger_severity_threshold(self):
        original_severity = rclpy.logging.root_logger.get_severity_threshold()
        for severity in LoggingSeverity:
            rclpy.logging.root_logger.set_severity_threshold(severity)
            self.assertEqual(severity, rclpy.logging.root_logger.get_severity_threshold())
        rclpy.logging.root_logger.set_severity_threshold(original_severity)

    def test_logger_severity_threshold(self):
        # We should be able to set the threshold of a nonexistent logger / one that doesn't
        # correspond to a python object, e.g. an RMW internal logger.
        name = 'my_internal_logger_name'
        original_severity = rclpy.logging.get_logger_severity_threshold(name)
        for severity in LoggingSeverity:
            rclpy.logging.set_logger_severity_threshold(name, severity)
            self.assertEqual(severity, rclpy.logging.get_logger_severity_threshold(name))
        rclpy.logging.set_logger_severity_threshold(name, original_severity)

    def test_logger_object_severity_threshold(self):
        original_severity = rclpy.logging.root_logger.get_severity_threshold()
        for severity in LoggingSeverity:
            rclpy.logging.root_logger.set_severity_threshold(severity)
            self.assertEqual(severity, rclpy.logging.root_logger.get_severity_threshold())
        rclpy.logging.root_logger.set_severity_threshold(original_severity)

    def test_logger_effective_severity_threshold(self):
        name = 'my_nonexistent_logger_name'
        self.assertEqual(
            rclpy.logging.root_logger.get_severity_threshold(),
            rclpy.logging.get_logger_effective_severity_threshold(name))

        # Check that the effective threshold for a logger with manually unset severity is default
        rclpy.logging.set_logger_severity_threshold(name, LoggingSeverity.UNSET)
        self.assertEqual(
            rclpy.logging.root_logger.get_severity_threshold(),
            rclpy.logging.get_logger_effective_severity_threshold(name))
        # Check that the effective threshold for a logger with set severity
        rclpy.logging.set_logger_severity_threshold(name, LoggingSeverity.ERROR)
        self.assertEqual(
            LoggingSeverity.ERROR,
            rclpy.logging.get_logger_effective_severity_threshold(name))

    def test_log_threshold(self):
        rclpy.logging.root_logger.set_severity_threshold(LoggingSeverity.INFO)

        # Logging below threshold not expected to be logged
        self.assertFalse(rclpy.logging.logdebug('message_debug'))

        # Logging at or above threshold expected to be logged
        self.assertTrue(rclpy.logging.loginfo('message_info'))
        self.assertTrue(rclpy.logging.logwarn('message_warn'))
        self.assertTrue(rclpy.logging.logerr('message_err'))
        self.assertTrue(rclpy.logging.logfatal('message_fatal'))

    def test_log_once(self):
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                once=True,
            ))
        self.assertEqual(message_was_logged, [True] + [False] * 4)

        # If the argument is specified as false it shouldn't impact the logging.
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message2_' + inspect.stack()[0][3] + '_false_' + str(i),
                LoggingSeverity.INFO,
                once=False,
            ))
        self.assertEqual(message_was_logged, [True] * 5)

    def test_log_throttle(self):
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                throttle_duration_sec=1,
                throttle_time_source_type='RCUTILS_STEADY_TIME',
            ))
            time.sleep(0.3)
        self.assertEqual(message_was_logged, [True] + [False] * 3 + [True])

    def test_log_skip_first(self):
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                skip_first=True,
            ))
        self.assertEqual(message_was_logged, [False] + [True] * 4)

    def test_log_skip_first_throttle(self):
        # Because of the ordering of supported_filters, first the throttle condition will be
        # evaluated/updated, then the skip_first condition
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                skip_first=True,
                throttle_duration_sec=1,
                throttle_time_source_type='RCUTILS_STEADY_TIME',
            ))
            time.sleep(0.3)
        self.assertEqual(message_was_logged, [False] * 4 + [True])
<commit_msg>Relax the tolerance on throttle timing (#144)<commit_after># Copyright 2017 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
import time
import unittest

import rclpy
from rclpy.logging import LoggingSeverity


class TestLogging(unittest.TestCase):

    def test_root_logger_severity_threshold(self):
        original_severity = rclpy.logging.root_logger.get_severity_threshold()
        for severity in LoggingSeverity:
            rclpy.logging.root_logger.set_severity_threshold(severity)
            self.assertEqual(severity, rclpy.logging.root_logger.get_severity_threshold())
        rclpy.logging.root_logger.set_severity_threshold(original_severity)

    def test_logger_severity_threshold(self):
        # We should be able to set the threshold of a nonexistent logger / one that doesn't
        # correspond to a python object, e.g. an RMW internal logger.
        name = 'my_internal_logger_name'
        original_severity = rclpy.logging.get_logger_severity_threshold(name)
        for severity in LoggingSeverity:
            rclpy.logging.set_logger_severity_threshold(name, severity)
            self.assertEqual(severity, rclpy.logging.get_logger_severity_threshold(name))
        rclpy.logging.set_logger_severity_threshold(name, original_severity)

    def test_logger_object_severity_threshold(self):
        original_severity = rclpy.logging.root_logger.get_severity_threshold()
        for severity in LoggingSeverity:
            rclpy.logging.root_logger.set_severity_threshold(severity)
            self.assertEqual(severity, rclpy.logging.root_logger.get_severity_threshold())
        rclpy.logging.root_logger.set_severity_threshold(original_severity)

    def test_logger_effective_severity_threshold(self):
        name = 'my_nonexistent_logger_name'
        self.assertEqual(
            rclpy.logging.root_logger.get_severity_threshold(),
            rclpy.logging.get_logger_effective_severity_threshold(name))

        # Check that the effective threshold for a logger with manually unset severity is default
        rclpy.logging.set_logger_severity_threshold(name, LoggingSeverity.UNSET)
        self.assertEqual(
            rclpy.logging.root_logger.get_severity_threshold(),
            rclpy.logging.get_logger_effective_severity_threshold(name))
        # Check that the effective threshold for a logger with set severity
        rclpy.logging.set_logger_severity_threshold(name, LoggingSeverity.ERROR)
        self.assertEqual(
            LoggingSeverity.ERROR,
            rclpy.logging.get_logger_effective_severity_threshold(name))

    def test_log_threshold(self):
        rclpy.logging.root_logger.set_severity_threshold(LoggingSeverity.INFO)

        # Logging below threshold not expected to be logged
        self.assertFalse(rclpy.logging.logdebug('message_debug'))

        # Logging at or above threshold expected to be logged
        self.assertTrue(rclpy.logging.loginfo('message_info'))
        self.assertTrue(rclpy.logging.logwarn('message_warn'))
        self.assertTrue(rclpy.logging.logerr('message_err'))
        self.assertTrue(rclpy.logging.logfatal('message_fatal'))

    def test_log_once(self):
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                once=True,
            ))
        self.assertEqual(message_was_logged, [True] + [False] * 4)

        # If the argument is specified as false it shouldn't impact the logging.
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message2_' + inspect.stack()[0][3] + '_false_' + str(i),
                LoggingSeverity.INFO,
                once=False,
            ))
        self.assertEqual(message_was_logged, [True] * 5)

    def test_log_throttle(self):
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                throttle_duration_sec=1,
                throttle_time_source_type='RCUTILS_STEADY_TIME',
            ))
            time.sleep(0.4)
        self.assertEqual(message_was_logged, [True, False, False, True, False])

    def test_log_skip_first(self):
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                skip_first=True,
            ))
        self.assertEqual(message_was_logged, [False] + [True] * 4)

    def test_log_skip_first_throttle(self):
        # Because of the ordering of supported_filters, first the throttle condition will be
        # evaluated/updated, then the skip_first condition
        message_was_logged = []
        for i in range(5):
            message_was_logged.append(rclpy.logging.log(
                'message_' + inspect.stack()[0][3] + '_' + str(i),
                LoggingSeverity.INFO,
                skip_first=True,
                throttle_duration_sec=1,
                throttle_time_source_type='RCUTILS_STEADY_TIME',
            ))
            time.sleep(0.3)
        self.assertEqual(message_was_logged, [False] * 4 + [True])

{'fraction_non_alphanumeric': 0.08592045645242491, 'fraction_numerical': 0.006041282094311126, 'mean_word_length': 2.8312439729990357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6058619', 'n_tokens_mistral': 3646, 'n_tokens_neox': 3462, 'n_words': 707}