autotest: deployment: capture more uart logs

Capture and dump more UART logs, e.g. cr50, servo_v4, servo_micro etc.

BUG=chromium:947142
TEST=Ran deploy script locally.

Change-Id: I86b7fc055bad2827c770d15b60a82aae733d0d0b
Reviewed-on: https://chromium-review.googlesource.com/1575080
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Congbin Guo <guocb@chromium.org>
Reviewed-by: Congbin Guo <guocb@chromium.org>
Reviewed-by: Nick Sanders <nsanders@chromium.org>
diff --git a/server/cros/servo/servo.py b/server/cros/servo/servo.py
index 0ecccc5..929aec8 100644
--- a/server/cros/servo/servo.py
+++ b/server/cros/servo/servo.py
@@ -133,32 +133,51 @@
 
 
 class _Uart(object):
-    """Class to capture CPU/EC UART streams."""
+    """Class to capture UART streams of CPU, EC, Cr50, etc."""
+    _UartToCapture = ('cpu', 'ec', 'cr50', 'servo_v4', 'servo_micro')
+
     def __init__(self, servo):
         self._servo = servo
         self._streams = []
-        self._logs_dir = None
+        self.logs_dir = None
 
-    def start_capture(self):
-        """Start capturing Uart streams."""
-        logging.debug('Start capturing CPU/EC UART.')
-        self._servo.set('cpu_uart_capture', 'on')
-        self._streams.append(('cpu_uart_stream', 'cpu_uart.log'))
+    def _start_stop_capture(self, uart, start):
+        """Helper function to start/stop capturing on specified UART.
+
+        @param uart:  The UART name to start/stop capturing.
+        @param start:  True to start capturing, otherwise stop.
+
+        @returns True if the operation completes successfully. False if the UART
+                 capturing is not supported.
+        @raises error.TestFail when operation is supported but failed.
+        """
         try:
-            self._servo.set('ec_uart_capture', 'on')
-            self._streams.append(('ec_uart_stream', 'ec_uart.log'))
+            logging.debug('%s capturing %s UART.',
+                          'Start' if start else 'Stop', uart)
+            self._servo.set('%s_uart_capture' % uart,
+                            'on' if start else 'off')
+            return True
         except error.TestFail as err:
             if 'No control named' in str(err):
-                logging.debug('The servod is too old that ec_uart_capture not '
-                              'supported.')
+                logging.debug('The servod is too old that %s_uart_capture '
+                              'not supported.', uart)
+                return False
+            raise err
+
+    def start_capture(self):
+        """Start capturing UART streams."""
+        for uart in self._UartToCapture:
+            if self._start_stop_capture(uart, True):
+                self._streams.append(('%s_uart_stream' % uart, '%s_uart.log' %
+                                      uart))
 
     def dump(self):
         """Dump UART streams to log files accordingly."""
-        if not self._logs_dir:
+        if not self.logs_dir:
             return
 
         for stream, logfile in self._streams:
-            logfile_fullname = os.path.join(self._logs_dir, logfile)
+            logfile_fullname = os.path.join(self.logs_dir, logfile)
             try:
                 content = self._servo.get(stream)
             except Exception as err:
@@ -173,30 +192,13 @@
 
     def stop_capture(self):
         """Stop capturing UART streams."""
-        logging.debug('Stop capturing CPU/EC UART.')
-        for uart in ('cpu_uart_capture', 'ec_uart_capture'):
+        for uart in self._UartToCapture:
             try:
-                self._servo.set(uart, 'off')
-            except error.TestFail as err:
-                if 'No control named' in str(err):
-                    logging.debug('The servod is too old that %s not '
-                                  'supported.', uart)
+                self._start_stop_capture(uart, False)
             except Exception as err:
                 logging.warn('Failed to stop UART logging for %s: %s', uart,
                              err)
 
-    @property
-    def logs_dir(self):
-        """Return the directory to save UART logs."""
-        return self._logs_dir
-
-    @logs_dir.setter
-    def logs_dir(self, a_dir):
-        """Set directory to save UART logs.
-
-        @param a_dir  String of logs directory name."""
-        self._logs_dir = a_dir
-
 
 class Servo(object):