Bug 1261018 - Make log.queue_debug output debug log when receiving an exception. r=ted
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 01 Apr 2016 10:53:16 +0900
changeset 291549 fc2ac654c59b188d8eef45f9ba99e2918ad32c34
parent 291548 667c295f3d3d187145ed9f1898516db73f7287e2
child 291550 0e5ae87fb396ec92af0de53e09433a9bf451729f
push id74615
push usermh@glandium.org
push dateMon, 04 Apr 2016 22:19:06 +0000
treeherdermozilla-inbound@ae2f8bdfec61 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted
bugs1261018
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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)