Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
webkitpy: Add contextmanager to disable logging for a block
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=172876

Reviewed by Daniel Bates.

Add a context manager which will use an OutputCapture object to capture logging
in a block and hold it in a variable.

* Scripts/webkitpy/common/system/outputcapture.py:
(OutputCaptureScope):
(OutputCaptureScope.__init__): Construct with OutputCapture object.
(OutputCaptureScope.__enter__): Begin capturing output.
(OutputCaptureScope.__exit__): Restore output and save captured output to a variable.
output and retain the resulting log.
* Scripts/webkitpy/common/system/outputcapture_unittest.py:
(OutputCaptureTest.test_output_capture_scope): Added.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@217758 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
jbedard@apple.com committed Jun 3, 2017
1 parent 1d8abaf commit 1f6b5b8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Tools/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
2017-06-03 Jonathan Bedard <jbedard@apple.com>

webkitpy: Add contextmanager to disable logging for a block
https://bugs.webkit.org/show_bug.cgi?id=172876

Reviewed by Daniel Bates.

Add a context manager which will use an OutputCapture object to capture logging
in a block and hold it in a variable.

* Scripts/webkitpy/common/system/outputcapture.py:
(OutputCaptureScope):
(OutputCaptureScope.__init__): Construct with OutputCapture object.
(OutputCaptureScope.__enter__): Begin capturing output.
(OutputCaptureScope.__exit__): Restore output and save captured output to a variable.
output and retain the resulting log.
* Scripts/webkitpy/common/system/outputcapture_unittest.py:
(OutputCaptureTest.test_output_capture_scope): Added.

2017-06-02 Zalan Bujtas <zalan@apple.com>

Cleanup FrameView::autoSizeIfEnabled.
Expand Down
13 changes: 13 additions & 0 deletions Tools/Scripts/webkitpy/common/system/outputcapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def assert_outputs(self, testcase, function, args=[], kwargs={}, expected_stdout
return return_value


class OutputCaptureScope(object):

def __init__(self, output_capture=OutputCapture()):
self._output_capture = output_capture
self.captured_output = (None, None, None)

def __enter__(self):
self._output_capture.capture_output()

def __exit__(self, exc_type, exc_value, traceback):
self.captured_output = self._output_capture.restore_output()


class OutputCaptureTestCaseBase(unittest.TestCase):
maxDiff = None

Expand Down
18 changes: 17 additions & 1 deletion Tools/Scripts/webkitpy/common/system/outputcapture_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import logging
import unittest

from webkitpy.common.system.outputcapture import OutputCapture
from webkitpy.common.system.outputcapture import OutputCapture, OutputCaptureScope


_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -57,3 +57,19 @@ def test_set_log_level(self):
self.output.set_log_level(logging.WARN)
self.log_all_levels()
self.assertLogged('ERROR\nCRITICAL\nWARN\nERROR\nCRITICAL\n')

def test_output_capture_scope(self):
scope = OutputCaptureScope()
with scope:
print 'STRING 1'
self.assertEqual(('STRING 1\n', '', ''), scope.captured_output)

with scope:
print 'STRING 2'
self.assertEqual(('STRING 2\n', '', ''), scope.captured_output)

def test_output_capture_scope_from_output_capture(self):
scope = OutputCaptureScope(self.output)
with scope:
self.log_all_levels()
self.assertEqual(('', '', 'INFO\nWARN\nERROR\nCRITICAL\n'), scope.captured_output)

0 comments on commit 1f6b5b8

Please sign in to comment.