Bug 1257516 - Initialize a logger for the ConfigureSandbox, and use it for the help. r?ted draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 25 Mar 2016 16:12:51 +0900
changeset 344663 28b0a6003af4ed16e4654b17afa8cd1656e6069a
parent 344662 7c6e1e4be3981a15687154ae342528f597b5eea4
child 344664 d2099a95184bf367a69e144e4dcb248c049fb27c
push id13899
push userbmo:mh+mozilla@glandium.org
push dateFri, 25 Mar 2016 08:32:05 +0000
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)