From 9fce7075d992a5fc03476169600823b207ae5ff2 Mon Sep 17 00:00:00 2001 From: Aseda Aboagye Date: Tue, 5 Jan 2016 16:47:42 -0800 Subject: [PATCH] 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 Reviewed-on: https://chromium-review.googlesource.com/320597 Commit-Ready: Aseda Aboagye Tested-by: Aseda Aboagye Reviewed-by: Wai-Hong Tam --- util/ec3po/console.py | 3 ++- util/ec3po/interpreter.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/util/ec3po/console.py b/util/ec3po/console.py index 86a4a9be8c..d7dec24877 100755 --- a/util/ec3po/console.py +++ b/util/ec3po/console.py @@ -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 diff --git a/util/ec3po/interpreter.py b/util/ec3po/interpreter.py index 2d40729aaf..d8ebc6f536 100644 --- a/util/ec3po/interpreter.py +++ b/util/ec3po/interpreter.py @@ -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