diff --git a/tools/rosgraph/src/rosgraph/roslogging.py b/tools/rosgraph/src/rosgraph/roslogging.py index d6b16cc141..9ecc121893 100644 --- a/tools/rosgraph/src/rosgraph/roslogging.py +++ b/tools/rosgraph/src/rosgraph/roslogging.py @@ -40,6 +40,7 @@ import logging import logging.config import inspect +import datetime import yaml @@ -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)) @@ -259,10 +269,23 @@ def emit(self, record): except ImportError: 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) diff --git a/tools/rosgraph/test/test_roslogging.py b/tools/rosgraph/test/test_roslogging.py index 3ce09eb7ba..67fbd75543 100644 --- a/tools/rosgraph/test/test_roslogging.py +++ b/tools/rosgraph/test/test_roslogging.py @@ -47,6 +47,7 @@ '${severity}', '${message}', '${walltime}', + '${walltime:%Y-%m-%d %H:%M:%S}', '${thread}', '${logger}', '${file}', @@ -54,6 +55,7 @@ '${function}', '${node}', '${time}', + '${time:%Y-%m-%d %H:%M:%S}', ]) rosgraph.roslogging.configure_logging('test_rosgraph', logging.INFO) loginfo = logging.getLogger('rosout').info @@ -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), @@ -119,6 +122,7 @@ def test_rosconsole__logging_format(): # depending if rospy.get_name() is available '(/unnamed|)', 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) diff --git a/tools/rosgraph/test/test_roslogging_user_logger.py b/tools/rosgraph/test/test_roslogging_user_logger.py index 4e71ef0cb6..ab2981b781 100644 --- a/tools/rosgraph/test/test_roslogging_user_logger.py +++ b/tools/rosgraph/test/test_roslogging_user_logger.py @@ -86,6 +86,7 @@ def test_roslogging_user_logger(): '${severity}', '${message}', '${walltime}', + '${walltime:%Y-%m-%d %H:%M:%S}', '${thread}', '${logger}', '${file}', @@ -93,6 +94,7 @@ def test_roslogging_user_logger(): '${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 @@ -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', '', @@ -136,6 +139,7 @@ def test_roslogging_user_logger(): # depending if rospy.get_name() is available '(/unnamed|)', 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)