2Stand-in for `rospy` logging functionality in ROS2.
4Heavily modified copy from ROS1 `rospy.node`,
5at https://github.com/ros/ros_comm (`clients/rospy/src/rospy/core.py`),
6released under the BSD License.
8------------------------------------------------------------------------------
9This file is part of rosros - simple unified interface to ROS1 / ROS2.
10Released under the BSD License.
15------------------------------------------------------------------------------
17## @namespace rosros.rospify.log
19# Original file copyright notice:
21# Software License Agreement (BSD License)
23# Copyright (c) 2008, Willow Garage, Inc.
26# Redistribution and use in source and binary forms, with or without
27# modification, are permitted provided that the following conditions
30# * Redistributions of source code must retain the above copyright
59def logdebug(msg, *args, **kwargs):
62def loginfo(msg, *args, **kwargs):
65def logwarn(msg, *args, **kwargs):
68def logerr(msg, *args, **kwargs):
71def logfatal(msg, *args, **kwargs):
75def logdebug_throttle(period, msg, *args, **kwargs):
76 _base_logger(msg, args, kwargs, throttle=period, level=
"debug")
78def loginfo_throttle(period, msg, *args, **kwargs):
79 _base_logger(msg, args, kwargs, throttle=period, level=
"info")
81def logwarn_throttle(period, msg, *args, **kwargs):
82 _base_logger(msg, args, kwargs, throttle=period, level=
"warn")
84def logerr_throttle(period, msg, *args, **kwargs):
85 _base_logger(msg, args, kwargs, throttle=period, level=
"error")
87def logfatal_throttle(period, msg, *args, **kwargs):
88 _base_logger(msg, args, kwargs, throttle=period, level=
"critical")
91def logdebug_throttle_identical(period, msg, *args, **kwargs):
92 _base_logger(msg, args, kwargs, throttle=period, throttle_identical=
True,
95def loginfo_throttle_identical(period, msg, *args, **kwargs):
96 _base_logger(msg, args, kwargs, throttle=period, throttle_identical=
True,
99def logwarn_throttle_identical(period, msg, *args, **kwargs):
100 _base_logger(msg, args, kwargs, throttle=period, throttle_identical=
True,
103def logerr_throttle_identical(period, msg, *args, **kwargs):
104 _base_logger(msg, args, kwargs, throttle=period, throttle_identical=
True,
107def logfatal_throttle_identical(period, msg, *args, **kwargs):
108 _base_logger(msg, args, kwargs, throttle=period, throttle_identical=
True,
112def logdebug_once(msg, *args, **kwargs):
113 _base_logger(msg, args, kwargs, once=
True, level=
"debug")
115def loginfo_once(msg, *args, **kwargs):
116 _base_logger(msg, args, kwargs, once=
True, level=
"info")
118def logwarn_once(msg, *args, **kwargs):
119 _base_logger(msg, args, kwargs, once=
True, level=
"warn")
121def logerr_once(msg, *args, **kwargs):
122 _base_logger(msg, args, kwargs, once=
True, level=
"error")
124def logfatal_once(msg, *args, **kwargs):
125 _base_logger(msg, args, kwargs, once=
True, level=
"critical")
135 """Returns caller id at specified call stack depth."""
136 frame = inspect.currentframe().f_back
137 while depth
and frame
and frame.f_back:
138 frame, depth = frame.f_back, depth - 1
139 return pickle.dumps((inspect.getabsfile(frame), frame.f_lineno, frame.f_lasti))
143 throttle_identical=False, level=None, once=False):
145 Issues the logging call, if throttling
or once-argument allow
for it.
147 @param msg log message
148 @param args positional arguments
for log message
149 @param kwargs keyword arguments
for log message
150 @param throttle seconds to throttle log messages
from call site
for
151 @param throttle_identical whether to throttle identical consecutive log messages
152 @param level log level name
153 @param once whether to log only once
from callsite
158 elif throttle_identical:
160 if throttle
is not None:
161 throttle_elapsed = LogInhibitor.passes_throttle(caller_id, throttle)
162 do_log = LogInhibitor.passes_identical(caller_id, msg)
or throttle_elapsed
166 if do_log: getattr(ros2.get_logger(), level.lower())(msg, *args, **kwargs)
182 """Returns whether the caller ID has not been once()-d before."""
183 result = caller_id
not in cls.
ONCES
184 cls.
ONCES.add(caller_id)
189 """Returns whether time from last throttle() was more than specified seconds ago, if any."""
190 now, last = ros2.get_rostime(), cls.
TIMES.get(caller_id)
191 if last
is not None and last > now: cls.
TIMES.clear()
193 result = last
is None or now - last > ros2.make_duration(period)
194 cls.
TIMES[caller_id] = now
199 """Returns whether last message from caller was different, if any."""
200 result, msg_hash =
False, hashlib.md5(msg.encode()).hexdigest()
201 if msg_hash != cls.
HASHES.get(caller_id):
208 "logdebug",
"loginfo",
"logout",
"logwarn",
"logerr",
"logfatal",
209 "logdebug_throttle",
"loginfo_throttle",
"logwarn_throttle",
210 "logerr_throttle",
"logfatal_throttle",
211 "logdebug_throttle_identical",
"loginfo_throttle_identical",
212 "logwarn_throttle_identical",
"logerr_throttle_identical",
"logfatal_throttle_identical",
213 "logdebug_once",
"loginfo_once",
"logwarn_once",
"logerr_once",
"logfatal_once",
dict HASHES
Caller IDs and log message hashes for logxyz_throttle_identical()
dict TIMES
Caller IDs and last timestamps for logxyz_throttle() and logxyz_throttle_identical()
ONCES
Caller IDs registered for logxyz_once()
passes_identical(cls, caller_id, msg)
Returns whether last message from caller was different, if any.
passes_throttle(cls, caller_id, period)
Returns whether time from last throttle() was more than specified seconds ago, if any.
passes_once(cls, caller_id)
Returns whether the caller ID has not been once()-d before.
_make_caller_id(depth=2)
Returns caller id at specified call stack depth.
_base_logger(msg, args, kwargs, throttle=None, throttle_identical=False, level=None, once=False)
Issues the logging call, if throttling or once-argument allow for it.