Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bool return to _base_logger, log* functions #2335

Open
wants to merge 1 commit into
base: noetic-devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 52 additions & 25 deletions clients/rospy/src/rospy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ def _frame_to_caller_id(frame):

def _base_logger(msg, args, kwargs, throttle=None,
throttle_identical=False, level=None, once=False):
"""
Base logger function that handles logging of normal, throttled,
identical, or once type calls. Also passes user args, kwargs to logging
module to be formatted into msg.

@param msg: message string to be logged.
@type msg: str
@param args: (optional) args to format into msg
@type args: tuple
@param kwargs: (optional) kwargs to format into msg
@type kwargs: dict
@param throttle: (optional) Period to do logging in second unit.
@type throttle: float
@param throttle_identical: (optional) Whether to only throttle identical messages. (Default: False)
@type throttle_identical: bool
@param level: (optional) Python logging level ('info', 'debug', etc...). (Default: None)
@type level: str
@param once: (optional) Whether to only log once. (Default: False)
@type once: bool
@return: True if local node initialized and message passed throttle
@rtype: bool
"""

rospy_logger = logging.getLogger('rosout')
name = kwargs.pop('logger_name', None)
Expand All @@ -168,35 +190,40 @@ def _base_logger(msg, args, kwargs, throttle=None,
caller_id = _frame_to_caller_id(inspect.currentframe().f_back.f_back)
if _logging_once(caller_id):
logfunc(msg, *args, **kwargs)
return is_initialized()
elif throttle_identical:
caller_id = _frame_to_caller_id(inspect.currentframe().f_back.f_back)
throttle_elapsed = False
if throttle is not None:
throttle_elapsed = _logging_throttle(caller_id, throttle)
if _logging_identical(caller_id, msg) or throttle_elapsed:
logfunc(msg, *args, **kwargs)
return is_initialized()
elif throttle:
caller_id = _frame_to_caller_id(inspect.currentframe().f_back.f_back)
if _logging_throttle(caller_id, throttle):
logfunc(msg, *args, **kwargs)
return is_initialized()
else:
logfunc(msg, *args, **kwargs)
return is_initialized()
return False


def logdebug(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, level='debug')
return _base_logger(msg, args, kwargs, level='debug')

def loginfo(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, level='info')
return _base_logger(msg, args, kwargs, level='info')

def logwarn(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, level='warning')
return _base_logger(msg, args, kwargs, level='warning')

def logerr(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, level='error')
return _base_logger(msg, args, kwargs, level='error')

def logfatal(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, level='critical')
return _base_logger(msg, args, kwargs, level='critical')

logout = loginfo # alias deprecated name

Expand Down Expand Up @@ -234,19 +261,19 @@ def __call__(self, caller_id, period):


def logdebug_throttle(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, level='debug')
return _base_logger(msg, args, kwargs, throttle=period, level='debug')

def loginfo_throttle(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, level='info')
return _base_logger(msg, args, kwargs, throttle=period, level='info')

def logwarn_throttle(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, level='warn')
return _base_logger(msg, args, kwargs, throttle=period, level='warn')

def logerr_throttle(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, level='error')
return _base_logger(msg, args, kwargs, throttle=period, level='error')

def logfatal_throttle(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, level='critical')
return _base_logger(msg, args, kwargs, throttle=period, level='critical')


class LoggingIdentical(object):
Expand All @@ -271,24 +298,24 @@ def __call__(self, caller_id, msg):


def logdebug_throttle_identical(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, throttle_identical=True,
level='debug')
return _base_logger(msg, args, kwargs, throttle=period,
throttle_identical=True, level='debug')

def loginfo_throttle_identical(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, throttle_identical=True,
level='info')
return _base_logger(msg, args, kwargs, throttle=period,
throttle_identical=True, level='info')

def logwarn_throttle_identical(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, throttle_identical=True,
level='warn')
return _base_logger(msg, args, kwargs, throttle=period,
throttle_identical=True, level='warn')

def logerr_throttle_identical(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, throttle_identical=True,
level='error')
return _base_logger(msg, args, kwargs, throttle=period,
throttle_identical=True, level='error')

def logfatal_throttle_identical(period, msg, *args, **kwargs):
_base_logger(msg, args, kwargs, throttle=period, throttle_identical=True,
level='critical')
return _base_logger(msg, args, kwargs, throttle=period,
throttle_identical=True, level='critical')


class LoggingOnce(object):
Expand All @@ -305,19 +332,19 @@ def __call__(self, caller_id):


def logdebug_once(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, once=True, level='debug')
return _base_logger(msg, args, kwargs, once=True, level='debug')

def loginfo_once(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, once=True, level='info')
return _base_logger(msg, args, kwargs, once=True, level='info')

def logwarn_once(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, once=True, level='warn')
return _base_logger(msg, args, kwargs, once=True, level='warn')

def logerr_once(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, once=True, level='error')
return _base_logger(msg, args, kwargs, once=True, level='error')

def logfatal_once(msg, *args, **kwargs):
_base_logger(msg, args, kwargs, once=True, level='critical')
return _base_logger(msg, args, kwargs, once=True, level='critical')


#########################################################
Expand Down