Bug 1257516 - Initialize a logger for the ConfigureSandbox, and use it for the help. r=ted
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 25 Mar 2016 16:12:51 +0900
changeset 290972 12248651d3ad2a6bacd79106899ec6464fd68e37
parent 290971 f97bcb2eaf41b62bb7d222f8a627a45e17fbbf54
child 290973 c2d739e9a7bc8463e1c7c29ba624e17c36f1e613
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted
bugs1257516
milestone48.0a1
Bug 1257516 - Initialize a logger for the ConfigureSandbox, and use it for the help. r=ted
python/mozbuild/mozbuild/configure/__init__.py
--- a/python/mozbuild/mozbuild/configure/__init__.py
+++ b/python/mozbuild/mozbuild/configure/__init__.py
@@ -1,30 +1,35 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import inspect
+import logging
 import os
 import sys
 import types
 from collections import OrderedDict
 from functools import wraps
 from mozbuild.configure.options import (
     CommandLineHelper,
     ConflictingOptionError,
     InvalidOptionError,
     NegativeOptionValue,
     Option,
     OptionValue,
     PositiveOptionValue,
 )
 from mozbuild.configure.help import HelpFormatter
+from mozbuild.configure.util import (
+    ConfigureOutputHandler,
+    LineIO,
+)
 from mozbuild.util import (
     ReadOnlyDict,
     ReadOnlyNamespace,
 )
 import mozpack.path as mozpath
 
 
 class ConfigureError(Exception):
@@ -87,17 +92,17 @@ class ConfigureSandbox(dict):
     # Expose a limited set of functions from os.path
     OS = ReadOnlyNamespace(path=ReadOnlyNamespace(**{
         k: getattr(mozpath, k, getattr(os.path, k))
         for k in ('abspath', 'basename', 'dirname', 'exists', 'isabs', 'isdir',
                   'isfile', 'join', 'normpath', 'realpath', 'relpath')
     }))
 
     def __init__(self, config, environ=os.environ, argv=sys.argv,
-                 stdout=sys.stdout, stderr=sys.stderr):
+                 stdout=sys.stdout, stderr=sys.stderr, logger=None):
         dict.__setitem__(self, '__builtins__', self.BUILTINS)
 
         self._paths = []
         self._templates = set()
         # Store the real function and its dependencies, behind each
         # DummyFunction generated from @depends.
         self._depends = {}
         self._seen = set()
@@ -116,17 +121,29 @@ class ConfigureSandbox(dict):
         self._implied_options = {}
 
         # Store all results from _prepare_function
         self._prepared_functions = set()
 
         self._helper = CommandLineHelper(environ, argv)
 
         assert isinstance(config, dict)
-        self._config, self._stdout, self._stderr = config, stdout, stderr
+        self._config = config
+
+        if logger is None:
+            logger = logging.getLogger('moz.configure')
+            logger.setLevel(logging.INFO)
+            formatter = logging.Formatter('%(levelname)s: %(message)s')
+            handler = ConfigureOutputHandler(stdout, stderr)
+            handler.setLevel(logging.INFO)
+            handler.setFormatter(formatter)
+            logger.addHandler(handler)
+
+        else:
+            assert isinstance(logger, logging.Logger)
 
         self._help = None
         self._help_option = self.option_impl('--help',
                                              help='print this message')
         self._seen.add(self._help_option)
         # self._option_impl('--help') will have set this if --help was on the
         # command line.
         if self._option_values[self._help_option]:
@@ -177,17 +194,18 @@ class ConfigureSandbox(dict):
         for option in self._options.itervalues():
             if option not in self._seen:
                 raise ConfigureError(
                     'Option `%s` is not handled ; reference it with a @depends'
                     % option.option
                 )
 
         if self._help:
-            self._help.usage(self._stdout)
+            with LineIO(logging.getLogger('moz.configure').info) as out:
+                self._help.usage(out)
 
     def __getitem__(self, key):
         impl = '%s_impl' % key
         func = getattr(self, impl, None)
         if func:
             return func
 
         return super(ConfigureSandbox, self).__getitem__(key)