Bug 1261018 - Make log.queue_debug output debug log when receiving an exception. r?ted draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 01 Apr 2016 10:53:16 +0900
changeset 346455 74d401683b5c4c99ac73e14a643480b15058b275
parent 346442 988ada582d926ec5cccd6e2eb8fd6f278b7f0ab3
child 346472 d1ea722351d0bc9c1d888c0805c2256f89ac7632
push id14381
push userbmo:mh+mozilla@glandium.org
push dateFri, 01 Apr 2016 03:48:11 +0000
reviewersted
bugs1261018
milestone48.0a1
Bug 1261018 - Make log.queue_debug output debug log when receiving an exception. r?ted
python/mozbuild/mozbuild/configure/util.py
python/mozbuild/mozbuild/test/configure/test_util.py
--- a/python/mozbuild/mozbuild/configure/util.py
+++ b/python/mozbuild/mozbuild/configure/util.py
@@ -116,29 +116,17 @@ class ConfigureOutputHandler(logging.Han
                     msg = '%s\n' % msg
             elif (record.levelno < logging.INFO and
                     self._keep_if_debug != self.PRINT):
                 if self._keep_if_debug == self.KEEP:
                     self._debug.append(record)
                 return
             else:
                 if record.levelno >= logging.ERROR and len(self._debug):
-                    self._keep_if_debug = self.PRINT
-                    if len(self._debug) == self._debug.maxlen:
-                        r = self._debug.popleft()
-                        self.emit(logging.LogRecord(
-                            r.name, r.levelno, r.pathname, r.lineno,
-                            '<truncated - see config.log for full output>',
-                            (), None))
-                    while True:
-                        try:
-                            self.emit(self._debug.popleft())
-                        except IndexError:
-                            break
-                    self._keep_if_debug = self.KEEP
+                    self._emit_queue()
 
                 if self._stdout_waiting == self.WAITING and self._same_output:
                     self._stdout_waiting = self.INTERRUPTED
                     self._stdout.write('\n')
                     self._stdout.flush()
                 stream = self._stderr
                 msg = '%s\n' % self.format(record)
             stream.write(msg)
@@ -146,20 +134,41 @@ class ConfigureOutputHandler(logging.Han
         except (KeyboardInterrupt, SystemExit):
             raise
         except:
             self.handleError(record)
 
     @contextmanager
     def queue_debug(self):
         self._keep_if_debug = self.KEEP
-        yield
+        try:
+            yield
+        except Exception:
+            self._emit_queue()
+            # The exception will be handled and very probably printed out by
+            # something upper in the stack.
+            raise
         self._keep_if_debug = self.THROW
         self._debug.clear()
 
+    def _emit_queue(self):
+        self._keep_if_debug = self.PRINT
+        if len(self._debug) == self._debug.maxlen:
+            r = self._debug.popleft()
+            self.emit(logging.LogRecord(
+                r.name, r.levelno, r.pathname, r.lineno,
+                '<truncated - see config.log for full output>',
+                (), None))
+        while True:
+            try:
+                self.emit(self._debug.popleft())
+            except IndexError:
+                break
+        self._keep_if_debug = self.KEEP
+
 
 class LineIO(object):
     '''File-like class that sends each line of the written data to a callback
     (without carriage returns).
     '''
     def __init__(self, callback):
         self._callback = callback
         self._buf = ''
--- a/python/mozbuild/mozbuild/test/configure/test_util.py
+++ b/python/mozbuild/mozbuild/test/configure/test_util.py
@@ -225,16 +225,37 @@ class TestConfigureOutputHandler(unittes
             'checking bar... no\n'
             'DEBUG:<truncated - see config.log for full output>\n'
             'DEBUG:do baz\n'
             'DEBUG:do qux\n'
             'DEBUG:do hoge\n'
             'ERROR:fail\n'
         )
 
+        out.seek(0)
+        out.truncate()
+
+        try:
+            with handler.queue_debug():
+                logger.info('checking bar... ')
+                logger.debug('do foo')
+                logger.debug('do bar')
+                logger.info('no')
+                e = Exception('fail')
+                raise e
+        except Exception as caught:
+            self.assertIs(caught, e)
+
+        self.assertEqual(
+            out.getvalue(),
+            'checking bar... no\n'
+            'DEBUG:do foo\n'
+            'DEBUG:do bar\n'
+        )
+
     def test_is_same_output(self):
         fd1 = sys.stderr.fileno()
         fd2 = os.dup(fd1)
         try:
             self.assertTrue(ConfigureOutputHandler._is_same_output(fd1, fd2))
         finally:
             os.close(fd2)