Skip to content

Commit

Permalink
Add timestamp formatting for rosconsole (#1892)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzozowski authored Feb 25, 2020
1 parent 5834b2d commit 41cb050
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
27 changes: 25 additions & 2 deletions tools/rosgraph/src/rosgraph/roslogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import logging
import logging.config
import inspect
import datetime

import yaml

Expand Down Expand Up @@ -247,7 +248,16 @@ def emit(self, record):
'ROSCONSOLE_FORMAT', '[${severity}] [${time}]: ${message}')
msg = msg.replace('${severity}', level)
msg = msg.replace('${message}', str(record_message))
msg = msg.replace('${walltime}', '%f' % time.time())

# walltime tag
msg = msg.replace('${walltime}', '%f' % time.time()) # for performance reasons

while '${walltime:' in msg:
tag_end_index = msg.index('${walltime:') + len('${walltime:')
time_format = msg[tag_end_index: msg.index('}', tag_end_index)]
time_str = time.strftime(time_format)
msg = msg.replace('${walltime:' + time_format + '}', time_str)

msg = msg.replace('${thread}', str(record.thread))
msg = msg.replace('${logger}', str(record.name))
msg = msg.replace('${file}', str(record.pathname))
Expand All @@ -259,10 +269,23 @@ def emit(self, record):
except ImportError:
node_name = '<unknown_node_name>'
msg = msg.replace('${node}', node_name)

# time tag
time_str = '%f' % time.time()
if self._get_time is not None and not self._is_wallclock():
time_str += ', %f' % self._get_time()
msg = msg.replace('${time}', time_str)
msg = msg.replace('${time}', time_str) # for performance reasons

while '${time:' in msg:
tag_end_index = msg.index('${time:') + len('${time:')
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()

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

msg += '\n'
if record.levelno < logging.WARNING:
self._write(self._stdout, msg, color)
Expand Down
4 changes: 4 additions & 0 deletions tools/rosgraph/test/test_roslogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
'${severity}',
'${message}',
'${walltime}',
'${walltime:%Y-%m-%d %H:%M:%S}',
'${thread}',
'${logger}',
'${file}',
'${line}',
'${function}',
'${node}',
'${time}',
'${time:%Y-%m-%d %H:%M:%S}',
])
rosgraph.roslogging.configure_logging('test_rosgraph', logging.INFO)
loginfo = logging.getLogger('rosout').info
Expand Down Expand Up @@ -111,6 +113,7 @@ def test_rosconsole__logging_format():
'INFO',
'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}',
'[0-9]*',
'rosout',
re.escape(this_file),
Expand All @@ -119,6 +122,7 @@ def test_rosconsole__logging_format():
# depending if rospy.get_name() is available
'(/unnamed|<unknown_node_name>)',
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)

Expand Down
4 changes: 4 additions & 0 deletions tools/rosgraph/test/test_roslogging_user_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ def test_roslogging_user_logger():
'${severity}',
'${message}',
'${walltime}',
'${walltime:%Y-%m-%d %H:%M:%S}',
'${thread}',
'${logger}',
'${file}',
'${line}',
'${function}',
'${node}',
'${time}',
'${time:%Y-%m-%d %H:%M:%S}',
])
rosgraph.roslogging.configure_logging('test_rosgraph', logging.INFO)
loginfo = logging.getLogger('rosout.custom_logger_test').info
Expand Down Expand Up @@ -128,6 +130,7 @@ def test_roslogging_user_logger():
os.environ['ROS_IP'],
msg,
r'[0-9]*\.[0-9]*',
r'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}',
'[0-9]*',
'rosout.custom_logger_test',
'<filename>',
Expand All @@ -136,6 +139,7 @@ def test_roslogging_user_logger():
# depending if rospy.get_name() is available
'(/unnamed|<unknown_node_name>)',
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().strip(), log_expected)

Expand Down

0 comments on commit 41cb050

Please sign in to comment.