From a83e09e9a2e23af798388146ad0b115be5b24f9e Mon Sep 17 00:00:00 2001 From: Greg Gallagher Date: Thu, 19 Jan 2017 12:50:35 -0500 Subject: [PATCH] fix white space changes and add throttle digest to bypass throttle if message contents have changed --- clients/rospy/src/rospy/core.py | 62 +++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/clients/rospy/src/rospy/core.py b/clients/rospy/src/rospy/core.py index d4ea0ede4c..e51e937cca 100644 --- a/clients/rospy/src/rospy/core.py +++ b/clients/rospy/src/rospy/core.py @@ -42,6 +42,7 @@ except ImportError: import pickle import inspect +import hashlib import logging import os import signal @@ -73,6 +74,7 @@ from rosgraph_msgs.msg import Log from functools import partial +from collections import namedtuple _logger = logging.getLogger("rospy.core") @@ -144,20 +146,21 @@ def rospywarn(msg, *args): """Internal rospy client library warn logging""" _rospy_logger.warn(msg, *args) + def _base_logger(msg, *args, **kwargs): try: - name = kwargs.pop('_logger_name') + name = kwargs.pop('logger_name') except KeyError: name = None try: - throttle = int(kwargs.pop('_logger_throttle')) + throttle = kwargs.pop('logger_throttle') except KeyError: throttle = None try: - level = kwargs.pop('_logger_level') + level = kwargs.pop('logger_level') except KeyError: level = None @@ -168,47 +171,55 @@ def _base_logger(msg, *args, **kwargs): if throttle: caller_id = _frame_record_to_caller_id(inspect.stack()[1]) - _logging_throttle(caller_id, logfunc, throttle, msg) + _logging_throttle(caller_id, logfunc, throttle, msg, *args) else: - logfunc(msg, *args, **kwargs) + logfunc(msg, *args) -loginfo = partial(_base_logger, _logger_level='info') +loginfo = partial(_base_logger, logger_level='info') logout = loginfo # alias deprecated name -logdebug = partial(_base_logger, _logger_level='debug') +logdebug = partial(_base_logger, logger_level='debug') -logwarn = partial(_base_logger, _logger_level='warn') +logwarn = partial(_base_logger, logger_level='warn') -logerr = partial(_base_logger, _logger_level='error') +logerr = partial(_base_logger, logger_level='error') logerror = logerr # alias logerr -logfatal = partial(_base_logger, _logger_level='critical') - +logfatal = partial(_base_logger, logger_level='critical') class LoggingThrottle(object): - last_logging_time_table = {} + LogEntry = namedtuple("LogEntry", "time digest") + + last_log_entry = {} - def __call__(self, caller_id, logging_func, period, msg): - """Do logging specified message periodically. + def __call__(self, caller_id, logging_func, period, msg, *args): + """Do logging specified message periodically. Messages with different contents will bypass throttling - caller_id (str): Id to identify the caller - logging_func (function): Function to do logging. - - period (float): Period to do logging in second unit. + - period (float): Period to do logging in seconds. - msg (object): Message to do logging. """ now = rospy.Time.now() - last_logging_time = self.last_logging_time_table.get(caller_id) + last = self.last_log_entry.get(caller_id, self.LogEntry(time=None, digest=None)) + digest = _get_digest(msg, *args) - if (last_logging_time is None or - (now - last_logging_time) > rospy.Duration(period)): - logging_func(msg) - self.last_logging_time_table[caller_id] = now + if (last.time is None or (now - last.time) > rospy.Duration(period) or digest != last.digest): + logging_func(msg, *args) + self.last_log_entry[caller_id] = self.LogEntry(time=now, digest=digest) + + +def _get_digest(*args): + m = hashlib.md5() + for arg in args: + m.update(str(args)) + return m.hexdigest() _logging_throttle = LoggingThrottle() @@ -225,24 +236,23 @@ def _frame_record_to_caller_id(frame_record): def logdebug_throttle(period, msg): - logdebug(msg, _logger_name=None, _logger_throttle=period) + logdebug(msg, logger_name=None, logger_throttle=period) def loginfo_throttle(period, msg): - loginfo(msg, _logger_name=None, _logger_throttle=period) + loginfo(msg, logger_name=None, logger_throttle=period) def logwarn_throttle(period, msg): - logwarn(msg, _logger_name=None, _logger_throttle=period) + logwarn(msg, logger_name=None, logger_throttle=period) def logerr_throttle(period, msg): - logerr(msg, _logger_name=None, _logger_throttle=period) + logerr(msg, logger_name=None, logger_throttle=period) def logfatal_throttle(period, msg): - logfatal(msg, _logger_name=None, _logger_throttle=period) - + logfatal(msg, logger_name=None, logger_throttle=period) #########################################################