Reland "autotest: deployment: capture more uart logs"

Reland fe4d4e84b (Change-Id: I86b7fc055bad2827c770d15b60a82aae733d0d0b)
which blocked the testing using servo v3 which initiated cr50_uart_pty
as a None object.

We ignore all errors when enable the UARTs capturing because it make
more sense that the testing shouldn't be aborted just because can't
capture UARTs.

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

Change-Id: Icfd66dcd03970fc95e6e00e554d79526f97d7ca0
Reviewed-on: https://chromium-review.googlesource.com/1582780
Commit-Ready: Congbin Guo <guocb@chromium.org>
Tested-by: Congbin Guo <guocb@chromium.org>
Reviewed-by: Mary Ruthven <mruthven@chromium.org>
diff --git a/server/cros/servo/servo.py b/server/cros/servo/servo.py
index 0ecccc5..9b27921 100644
--- a/server/cros/servo/servo.py
+++ b/server/cros/servo/servo.py
@@ -133,32 +133,52 @@
 
 
 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', 'usbpd')
+
     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 or failed due to an error.
+        """
+        logging.debug('%s capturing %s UART.', 'Start' if start else 'Stop',
+                      uart)
         try:
-            self._servo.set('ec_uart_capture', 'on')
-            self._streams.append(('ec_uart_stream', 'ec_uart.log'))
+            self._servo.set('%s_uart_capture' % uart,
+                            'on' if start else 'off')
         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 doesn't support %s_uart_capture.",
+                              uart)
+            else:
+                logging.debug("Can't caputre UART for %s: %s", uart, err)
+            return False
+
+        return True
+
+    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 +193,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):