util: ec3po: Add served PTY to logging.

Some devices have multiple ECs (main EC and PD MCU) and therefore there
are multiple consoles and interpreters running concurrently.  This
commit prepends each log message with the served PTY to identify which
of the console or interpreter instances the log message comes from.

BUG=None
BRANCH=None
TEST=Ran ec3po in servo for samus. Noticed each debug print with the
different PTYs for the main EC as well as the PD MCU.
TEST=util/ec3po/run_tests.sh

Change-Id: Icc69d2257172f4fedc217572ad0b6d15dac40387
Signed-off-by: Aseda Aboagye <aaboagye@google.com>
Reviewed-on: https://chromium-review.googlesource.com/320597
Commit-Ready: Aseda Aboagye <aaboagye@chromium.org>
Tested-by: Aseda Aboagye <aaboagye@chromium.org>
Reviewed-by: Wai-Hong Tam <waihong@chromium.org>
This commit is contained in:
Aseda Aboagye
2016-01-05 16:47:42 -08:00
committed by chrome-bot
parent 5f18982f67
commit 9fce7075d9
2 changed files with 12 additions and 2 deletions

View File

@@ -120,7 +120,8 @@ class Console(object):
read-only side of the debug pipe. This must be a unidirectional pipe
attached to the intepreter. EC debug messages use this pipe.
"""
self.logger = logging.getLogger('EC3PO.Console')
logger = logging.getLogger('EC3PO.Console')
self.logger = interpreter.LoggerAdapter(logger, {'pty': user_pty})
self.master_pty = master_pty
self.user_pty = user_pty
self.cmd_pipe = cmd_pipe

View File

@@ -29,6 +29,14 @@ EC_SYN = '\xec' # Byte indicating EC interrogation.
EC_ACK = '\xc0' # Byte representing correct EC response to interrogation.
class LoggerAdapter(logging.LoggerAdapter):
"""Class which provides a small adapter for the logger."""
def process(self, msg, kwargs):
"""Prepends the served PTY to the beginning of the log message."""
return '%s - %s' % (self.extra['pty'], msg), kwargs
class Interpreter(object):
"""Class which provides the interpretation layer between the EC and user.
@@ -78,7 +86,8 @@ class Interpreter(object):
log_level: An optional integer representing the numeric value of the log
level. By default, the log level will be logging.INFO (20).
"""
self.logger = logging.getLogger('EC3PO.Interpreter')
logger = logging.getLogger('EC3PO.Interpreter')
self.logger = LoggerAdapter(logger, {'pty': ec_uart_pty})
self.ec_uart_pty = open(ec_uart_pty, 'a+')
self.cmd_pipe = cmd_pipe
self.dbg_pipe = dbg_pipe