Skip to content

Commit

Permalink
fix format message unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasw committed Dec 27, 2023
1 parent 7b93dab commit cd1263f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 6 additions & 4 deletions tools/rosgraph/src/rosgraph/roslogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def get_shortfile(pathname, maxlen=30):

def format_msg(record_message, thread, name, pathname,
lineno, funcName, levelname, levelno,
extra_time):
extra_time,
get_time=None, is_wallclock=False):
level, color = _logging_to_rospy_names[levelname]
msg = os.environ.get(
'ROSCONSOLE_FORMAT', '[${severity}] [${time}]: ${message}')
Expand Down Expand Up @@ -270,8 +271,8 @@ def format_msg(record_message, thread, name, pathname,
time_format = msg[tag_end_index: msg.index('}', tag_end_index)]
time_str = time.strftime(time_format)

if self._get_time is not None and not self._is_wallclock():
time_str += ', %f' % self._get_time()
if get_time is not None and not is_wallclock:
time_str += ', %f' % get_time()

msg = msg.replace('${time:' + time_format + '}', time_str)

Expand All @@ -297,7 +298,8 @@ def emit(self, record):
extra_time = self._get_time() if self._get_time is not None and not self._is_wallclock() else None
msg, color = format_msg(record_message, record.thread, record.name, record.pathname,
record.lineno, record.funcName, record.levelname, record.levelno,
extra_time)
extra_time,
self._get_time, self._is_wallclock())
msg += '\n'
if record.levelno < logging.WARNING:
self._write(self._stdout, msg, color)
Expand Down
8 changes: 5 additions & 3 deletions tools/rosgraph/test/test_roslogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def test_rosconsole__logging_format():
else:
raise ValueError

log_out = ' '.join([
'INFO',
expected_log_out = ' '.join([
'I',
'on ' + loc,
r'[0-9]*\.[0-9]*',
r'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}',
Expand All @@ -124,7 +124,9 @@ def test_rosconsole__logging_format():
r'[0-9]*\.[0-9]*',
r'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}',
])
assert_regexp_matches(lout.getvalue().splitlines()[i], log_out)
log_out = lout.getvalue().splitlines()[i]
# TODO(lucasw) if there is a mismatch the assert isn't very helpful in where it is
assert_regexp_matches(log_out, expected_log_out)

finally:

Expand Down
7 changes: 4 additions & 3 deletions tools/rosgraph/test/test_roslogging_user_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def findCaller(self, stack_info=False, stacklevel=1):
"""
if sys.version_info > (3, 2):
# Dummy last argument to match Python3 return type
return '<filename>', '<lineno>', '<func_name>', None
return '<filename>', 1234, '<func_name>', None
else:
return '<filename>', '<lineno>', '<func_name>'
return '<filename>', 1234, '<func_name>'

def _log(self, level, msg, args, exc_info=None, extra=None):
"""Write log with ROS_IP.
Expand Down Expand Up @@ -125,6 +125,7 @@ def test_roslogging_user_logger():
msg = 'Hello world.'
loginfo(msg)

# TODO(lucasw) 1234 magic number
log_expected = ' '.join([
'INFO',
os.environ['ROS_IP'],
Expand All @@ -134,7 +135,7 @@ def test_roslogging_user_logger():
'[0-9]*',
'rosout.custom_logger_test',
'<filename>',
'<lineno>',
1234,
'<func_name>',
# depending if rospy.get_name() is available
'(/unnamed|<unknown_node_name>)',
Expand Down

0 comments on commit cd1263f

Please sign in to comment.