clean up NRWT logging/metering, be less verbose
[WebKit-https.git] / Tools / Scripts / webkitpy / layout_tests / controllers / worker.py
index 22c0a5f..b0588ab 100644 (file)
@@ -38,7 +38,7 @@ from webkitpy.layout_tests.controllers import manager_worker_broker
 from webkitpy.layout_tests.controllers import single_test_runner
 from webkitpy.layout_tests.models import test_expectations
 from webkitpy.layout_tests.models import test_results
-from webkitpy.layout_tests.views import printing
+from webkitpy.layout_tests.views import metered_stream
 
 
 _log = logging.getLogger(__name__)
@@ -65,7 +65,7 @@ class Worker(manager_worker_broker.AbstractWorker):
         self._driver = None
         self._tests_run_file = None
         self._tests_run_filename = None
-        self._printer = None
+        self._meter = None
 
     def __del__(self):
         self.cleanup()
@@ -82,41 +82,59 @@ class Worker(manager_worker_broker.AbstractWorker):
         tests_run_filename = self._filesystem.join(self._results_directory, "tests_run%d.txt" % self._worker_number)
         self._tests_run_file = self._filesystem.open_text_file_for_writing(tests_run_filename)
 
+    def _set_up_logging(self):
+        # The unix multiprocessing implementation clones the MeteredStream log handler
+        # into the child process, so we need to remove it before we can
+        # add a new one to get the correct pid logged.
+        root_logger = logging.getLogger()
+        handler_to_remove = None
+        for h in root_logger.handlers:
+            # log handlers don't have names until python 2.7.
+            if getattr(h, 'name', '') == metered_stream.LOG_HANDLER_NAME:
+                handler_to_remove = h
+                break
+        if handler_to_remove:
+            root_logger.removeHandler(handler_to_remove)
+
+        # FIXME: This won't work if the calling process is logging
+        # somewhere other than sys.stderr, but I'm not sure
+        # if this will be an issue in practice. Also, it would be
+        # nice if we trapped all of the messages for a given test
+        # and sent them back in finished_test() rather than logging
+        # them to stderr.
+        if not root_logger.handlers:
+            options = self._options
+            root_logger.setLevel(logging.DEBUG if options.verbose else logging.INFO)
+            self._meter = metered_stream.MeteredStream(sys.stderr, options.verbose, logger=root_logger)
+
+    def _set_up_host_and_port(self):
+        options = self._options
+        if options.platform and 'test' in options.platform:
+            # It is lame to import mocks into real code, but this allows us to use the test port in multi-process tests as well.
+            from webkitpy.common.host_mock import MockHost
+            host = MockHost()
+        else:
+            host = Host()
+        self._port = host.port_factory.get(options.platform, options)
+
     def set_inline_arguments(self, port):
         self._port = port
 
     def run(self):
         if not self._port:
-            # We are running in a child process and need to create a new Host.
-            if self._options.platform and 'test' in self._options.platform:
-                # It is lame to import mocks into real code, but this allows us to use the test port in multi-process tests as well.
-                from webkitpy.common.host_mock import MockHost
-                host = MockHost()
-            else:
-                host = Host()
-
-            options = self._options
-            self._port = host.port_factory.get(options.platform, options)
-
-            # The unix multiprocessing implementation clones the
-            # log handler configuration into the child processes,
-            # but the win implementation doesn't.
-            configure_logging = (sys.platform == 'win32')
-
-            # FIXME: This won't work if the calling process is logging
-            # somewhere other than sys.stderr and sys.stdout, but I'm not sure
-            # if this will be an issue in practice.
-            self._printer = printing.Printer(self._port, options, sys.stderr, sys.stdout, configure_logging)
+            # We are running in a child process and need to initialize things.
+            self._set_up_logging()
+            self._set_up_host_and_port()
 
         self.safe_init()
-
         try:
             _log.debug("%s starting" % self._name)
             super(Worker, self).run()
         finally:
+            self.kill_driver()
             self._worker_connection.post_message('done')
-            self.cleanup()
             _log.debug("%s exiting" % self._name)
+            self.cleanup()
 
     def handle_test_list(self, src, list_name, test_list):
         start_time = time.time()
@@ -153,9 +171,9 @@ class Worker(manager_worker_broker.AbstractWorker):
         if self._tests_run_file:
             self._tests_run_file.close()
             self._tests_run_file = None
-        if self._printer:
-            self._printer.cleanup()
-            self._printer = None
+        if self._meter:
+            self._meter.cleanup()
+            self._meter = None
 
     def timeout(self, test_input):
         """Compute the appropriate timeout value for a test."""