From b62994fb488b96cc912c37914c49ecefb1ca3b5d Mon Sep 17 00:00:00 2001 From: Ruben Rodriguez Buchillon Date: Fri, 4 Dec 2020 12:40:21 -0800 Subject: ec3po: console log printing in py2 and py3 The console log printing needs to work in py2 and py3. For this to work, the 'data' passed in needs to be treated equally in both cases. Since mostly what we care about is comparing it to other elements, we simply ensure that the data becomes a list of integers in both py2 and py3, and then we compare directly against the integer values of the characters we want to handle in a special way (\b, \t, etc). BUG=b:173654272 BRANCH=None // this shows all the correctly parsed log output from the MCUs (ec, v4, etc) in the logs TEST=sudo servod -b scarlet cat /var/log/servod_9999/latest.DEBUG Change-Id: Ibcbb95fd60619dc8234d7dd1f3d5d5c623542ce0 Signed-off-by: Ruben Rodriguez Buchillon Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2576140 Reviewed-by: Matthew Blecker --- util/ec3po/console.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'util') diff --git a/util/ec3po/console.py b/util/ec3po/console.py index 910e6b9114..5974aaa014 100755 --- a/util/ec3po/console.py +++ b/util/ec3po/console.py @@ -215,39 +215,49 @@ class Console(object): when it sees backspaces. Args: - data: binary string received from MCU + data: bytes - string received from MCU """ data = list(data) + # For compatibility with python2 and python3, standardize on the data + # being a list of integers. This requires one more transformation in py2 + if not isinstance(data[0], int): + data = [ord(c) for c in data] # This is a list of already filtered characters (or placeholders). line = self.output_line_log_buffer + # TODO(b/177480273): use raw strings here symbols = { - b'\n': u'\\n', - b'\r': u'\\r', - b'\t': u'\\t' + ord(b'\n'): u'\\n', + ord(b'\r'): u'\\r', + ord(b'\t'): u'\\t' } # self.logger.debug(u'%s + %r', u''.join(line), ''.join(data)) while data: + # Recall, data is a list of integers, namely the byte values sent by + # the MCU. byte = data.pop(0) - if byte == '\n': + # This means that |byte| is an int. + if byte == ord('\n'): line.append(symbols[byte]) if line: self.logger.debug(u'%s', ''.join(line)) line = [] - elif byte == b'\b': + elif byte == ord('\b'): # Backspace: trim the last character off the buffer if line: line.pop(-1) elif byte in symbols: line.append(symbols[byte]) - elif byte < b' ' or byte > b'~': + elif byte < ord(' ') or byte > ord('~'): # Turn any character that isn't printable ASCII into escaped hex. # ' ' is chr(20), and 0-19 are unprintable control characters. # '~' is chr(126), and 127 is DELETE. 128-255 are control and Latin-1. - line.append(u'\\x%02x' % ord(byte)) + line.append(u'\\x%02x' % byte) else: - line.append(u'%s' % byte) + # byte is printable. Thus it is safe to use chr() to get the printable + # character out of it again. + line.append(u'%s' % chr(byte)) self.output_line_log_buffer = line def PrintHistory(self): -- cgit v1.2.1