cheets: Salvage tradefed global log for debugging.

This log file records the logs from DeviceManager and CommandScheduler,
which should be useful for debugging issues like the ones listed below
in BUG= items.

BUG=b:34778582
BUG=b:34691656
TEST=test_that cheets_CTS.6.0_r14.x86.android.usb

Change-Id: Id6361375d42c0083c4d10b50da3653cac6f28466
Reviewed-on: https://chromium-review.googlesource.com/439812
Commit-Ready: Kazuhiro Inaba <kinaba@chromium.org>
Tested-by: Kazuhiro Inaba <kinaba@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
(cherry picked from commit eb6b6335b2b7b86abde4e1349d1648af63cc3ad8)
Reviewed-on: https://chromium-review.googlesource.com/442051
Reviewed-by: Ilja H. Friedel <ihf@chromium.org>
Tested-by: Ilja H. Friedel <ihf@chromium.org>
diff --git a/server/cros/tradefed_test.py b/server/cros/tradefed_test.py
index fde0229..5bef29d 100644
--- a/server/cros/tradefed_test.py
+++ b/server/cros/tradefed_test.py
@@ -540,6 +540,23 @@
                 kwargs.get('extra_paths', []) + self._install_paths)
         return utils.run(*args, **kwargs)
 
+    def _collect_tradefed_global_log(self, result, destination):
+        """Collects the tradefed global log.
+
+        @param result: The result object from utils.run.
+        @param destination: Autotest result directory (destination of logs).
+        """
+        match = re.search(r'Saved log to /tmp/(tradefed_global_log_.*\.txt)',
+                          result.stdout)
+        if not match:
+            logging.error('no tradefed_global_log file is found')
+            return
+
+        name = match.group(1)
+        dest = os.path.join(destination, 'logs', 'tmp')
+        self._safe_makedirs(dest)
+        shutil.copy(os.path.join('/tmp', name), os.path.join(dest, name))
+
     def _parse_tradefed_datetime(self, result, summary=None):
         """Get the tradefed provided result ID consisting of a datetime stamp.
 
diff --git a/server/site_tests/cheets_CTS/cheets_CTS.py b/server/site_tests/cheets_CTS/cheets_CTS.py
index 3aa2ece..106e760 100644
--- a/server/site_tests/cheets_CTS/cheets_CTS.py
+++ b/server/site_tests/cheets_CTS/cheets_CTS.py
@@ -221,14 +221,17 @@
                 stdout_tee=utils.TEE_TO_LOGS,
                 stderr_tee=utils.TEE_TO_LOGS)
             logging.info('END: ./cts-tradefed %s\n', ' '.join(command))
+        result_destination = os.path.join(self.resultsdir, 'android-cts')
+        # Gather the global log first. Datetime parsing below can abort the test
+        # if tradefed startup had failed. Even then the global log is useful.
+        self._collect_tradefed_global_log(output, result_destination)
         if not datetime_id:
             # Parse stdout to obtain datetime of the session. This is needed to
             # locate result xml files and logs.
             datetime_id = self._parse_tradefed_datetime(output, self.summary)
         # Collect tradefed logs for autotest.
         tradefed = os.path.join(self._android_cts, 'android-cts', 'repository')
-        autotest = os.path.join(self.resultsdir, 'android-cts')
-        self._collect_logs(tradefed, datetime_id, autotest)
+        self._collect_logs(tradefed, datetime_id, result_destination)
         return self._parse_result(output, self.waivers_and_manual_tests)
 
     def _tradefed_continue(self, session_id, datetime_id=None):
diff --git a/server/site_tests/cheets_GTS/cheets_GTS.py b/server/site_tests/cheets_GTS/cheets_GTS.py
index 3d14954..13ffc67 100644
--- a/server/site_tests/cheets_GTS/cheets_GTS.py
+++ b/server/site_tests/cheets_GTS/cheets_GTS.py
@@ -70,12 +70,17 @@
                     # already during the test run.
                     stdout_tee=utils.TEE_TO_LOGS,
                     stderr_tee=utils.TEE_TO_LOGS)
+        result_destination = os.path.join(self.resultsdir, 'android-gts')
+
+        # Gather the global log first. Datetime parsing below can abort the test
+        # if tradefed startup had failed. Even then the global log is useful.
+        self._collect_tradefed_global_log(output, result_destination)
+
         # Parse stdout to obtain datetime IDs of directories into which tradefed
         # wrote result xml files and logs.
         datetime_id = self._parse_tradefed_datetime(output)
         repository = os.path.join(self._android_gts, 'android-gts')
-        autotest = os.path.join(self.resultsdir, 'android-gts')
-        self._collect_logs(repository, datetime_id, autotest)
+        self._collect_logs(repository, datetime_id, result_destination)
 
         # Result parsing must come after all other essential operations as test
         # warnings, errors and failures can be raised here.