diff --git a/clients/rospy/src/rospy/core.py b/clients/rospy/src/rospy/core.py index 57c07b8367..5e967df66c 100644 --- a/clients/rospy/src/rospy/core.py +++ b/clients/rospy/src/rospy/core.py @@ -72,6 +72,7 @@ from rospy.impl.validators import ParameterInvalid from rosgraph_msgs.msg import Log +from functools import partial _logger = logging.getLogger("rospy.core") @@ -109,7 +110,7 @@ def parse_rosrpc_uri(uri): @raise ParameterInvalid: if uri is not a valid ROSRPC URI """ if uri.startswith(ROSRPC): - dest_addr = uri[len(ROSRPC):] + dest_addr = uri[len(ROSRPC):] else: raise ParameterInvalid("Invalid protocol for ROS service URL: %s"%uri) try: @@ -122,7 +123,7 @@ def parse_rosrpc_uri(uri): return dest_addr, dest_port ######################################################### - + # rospy logger _rospy_logger = logging.getLogger("rospy.internal") @@ -142,20 +143,52 @@ def rospyerr(msg, *args): def rospywarn(msg, *args): """Internal rospy client library warn logging""" _rospy_logger.warn(msg, *args) - -logdebug = logging.getLogger('rosout').debug -logwarn = logging.getLogger('rosout').warning + +def _base_logger(msg, *args, **kwargs): + + try: + name = kwargs.pop('logger_name') + except KeyError: + name = None + + try: + throttle = kwargs.pop('logger_throttle') + except KeyError: + throttle = None + + try: + level = kwargs.pop('logger_level') + except KeyError: + level = None + + rospy_logger = logging.getLogger('rosout') + if name: + rospy_logger = rospy_logger.getChild(name) + logfunc = getattr(rospy_logger, level) + + if throttle: + caller_id = _frame_record_to_caller_id(inspect.stack()[1]) + _logging_throttle(caller_id, logfunc, throttle, msg) + else: + logfunc(msg, *args, **kwargs) + + +loginfo = partial(_base_logger, logger_level='info') -loginfo = logging.getLogger('rosout').info logout = loginfo # alias deprecated name -logerr = logging.getLogger('rosout').error +logdebug = partial(_base_logger, logger_level='debug') + +logwarn = partial(_base_logger, logger_level='warn') + +logerr = partial(_base_logger, logger_level='error') + logerror = logerr # alias logerr -logfatal = logging.getLogger('rosout').critical - - +logfatal = partial(_base_logger, logger_level='critical') + + class LoggingThrottle(object): last_logging_time_table = {} @@ -192,28 +225,23 @@ def _frame_record_to_caller_id(frame_record): def logdebug_throttle(period, msg): - caller_id = _frame_record_to_caller_id(inspect.stack()[1]) - _logging_throttle(caller_id, logdebug, period, msg) + logdebug(msg, logger_name=None, logger_throttle=period) def loginfo_throttle(period, msg): - caller_id = _frame_record_to_caller_id(inspect.stack()[1]) - _logging_throttle(caller_id, loginfo, period, msg) + loginfo(msg, logger_name=None, logger_throttle=period) def logwarn_throttle(period, msg): - caller_id = _frame_record_to_caller_id(inspect.stack()[1]) - _logging_throttle(caller_id, logwarn, period, msg) + logwarn(msg, logger_name=None, logger_throttle=period) def logerr_throttle(period, msg): - caller_id = _frame_record_to_caller_id(inspect.stack()[1]) - _logging_throttle(caller_id, logerr, period, msg) + logerr(msg, logger_name=None, logger_throttle=period) def logfatal_throttle(period, msg): - caller_id = _frame_record_to_caller_id(inspect.stack()[1]) - _logging_throttle(caller_id, logfatal, period, msg) + logfatal(msg, logger_name=None, logger_throttle=period) ######################################################### @@ -302,10 +330,10 @@ def configure_logging(node_name, level=logging.INFO): class NullHandler(logging.Handler): def emit(self, record): pass - + # keep logging happy until we have the node name to configure with -logging.getLogger('rospy').addHandler(NullHandler()) - +logging.getLogger('rospy').addHandler(NullHandler()) + ######################################################### # Init/Shutdown/Exit API and Handlers @@ -364,7 +392,7 @@ def is_shutdown_requested(): received and continues until client shutdown handlers have been called. After client shutdown handlers have been serviced, the is_shutdown state becomes true. - + @return: True if shutdown has been requested (but possibly not yet initiated) @rtype: bool """ @@ -413,7 +441,7 @@ def add_client_shutdown_hook(h): Add client method to invoke when system shuts down. Unlike L{add_shutdown_hook} and L{add_preshutdown_hooks}, these methods will be called before any rospy internal shutdown code. - + @param h: function with zero args @type h: fn() """ @@ -424,7 +452,7 @@ def add_preshutdown_hook(h): Add method to invoke when system shuts down. Unlike L{add_shutdown_hook}, these methods will be called before any other shutdown hooks. - + @param h: function that takes in a single string argument (shutdown reason) @type h: fn(str) """ @@ -517,17 +545,17 @@ def register_signals(): """ _signalChain[signal.SIGTERM] = signal.signal(signal.SIGTERM, _ros_signal) _signalChain[signal.SIGINT] = signal.signal(signal.SIGINT, _ros_signal) - + # Validators ###################################### def is_topic(param_name): """ Validator that checks that parameter is a valid ROS topic name - """ + """ def validator(param_value, caller_id): v = valid_name_validator_resolved(param_name, param_value, caller_id) if param_value == '/': - raise ParameterInvalid("ERROR: parameter [%s] cannot be the global namespace"%param_name) + raise ParameterInvalid("ERROR: parameter [%s] cannot be the global namespace"%param_name) return v return validator @@ -542,4 +570,3 @@ def xmlrpcapi(uri): if not uriValidate[0] or not uriValidate[1]: return None return xmlrpcclient.ServerProxy(uri) -