Bug 172876 - webkitpy: Add contextmanager to disable logging for a block
Summary: webkitpy: Add contextmanager to disable logging for a block
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Tools / Tests (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Jonathan Bedard
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2017-06-02 15:23 PDT by Jonathan Bedard
Modified: 2017-06-03 09:11 PDT (History)
6 users (show)

See Also:


Attachments
Patch (2.92 KB, patch)
2017-06-02 15:51 PDT, Jonathan Bedard
no flags Details | Formatted Diff | Diff
Patch (3.00 KB, patch)
2017-06-02 16:53 PDT, Jonathan Bedard
no flags Details | Formatted Diff | Diff
Patch for landing (3.25 KB, patch)
2017-06-03 08:34 PDT, Jonathan Bedard
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Bedard 2017-06-02 15:23:18 PDT
We need a contextmanager which disables logging in a block and renables logging on exit.  Currently, OutputCapture only provides functions to disable and then re-enable logging.
Comment 1 Radar WebKit Bug Importer 2017-06-02 15:33:07 PDT
<rdar://problem/32546391>
Comment 2 Jonathan Bedard 2017-06-02 15:51:07 PDT
Created attachment 311877 [details]
Patch
Comment 3 Daniel Bates 2017-06-02 16:35:07 PDT
Comment on attachment 311877 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=311877&action=review

> Tools/Scripts/webkitpy/common/system/outputcapture.py:36
> +from contextlib import contextmanager
>  from StringIO import StringIO

These imports should be sorted based on the UNIX sort command.

> Tools/Scripts/webkitpy/common/system/outputcapture.py:112
> +class Suppressor(OutputCapture):

Maybe a better name for this would be OutputCaptureScope?

> Tools/Scripts/webkitpy/common/system/outputcapture.py:116
> +        self.last_log = None

I suggest that we use the same terminology as used in OutputCapture and call this captured_output.

> Tools/Scripts/webkitpy/common/system/outputcapture.py:123
> +    @contextmanager
> +    def suppress_logging(self):
> +        self.capture_output()
> +        yield
> +        self.last_log = self.restore_output()
> +

Instead of using @contextmanager can we have this class implement __enter__() and __exit__() itself so that we can make use of this convenience class using the following syntax:

scope = OutputCaptureScope()
with scope:
    ...
scope.captured_output
Comment 4 Daniel Bates 2017-06-02 16:37:25 PDT
Comment on attachment 311877 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=311877&action=review

>> Tools/Scripts/webkitpy/common/system/outputcapture.py:112
>> +class Suppressor(OutputCapture):
> 
> Maybe a better name for this would be OutputCaptureScope?

We may want to have this class take the OutputCapture instance instead of extending it to make it easy to use this class correctly and hard to use it incorrectly or un-idiomatic.
Comment 5 Jonathan Bedard 2017-06-02 16:53:33 PDT
Created attachment 311886 [details]
Patch
Comment 6 Daniel Bates 2017-06-02 17:09:02 PDT
Comment on attachment 311886 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=311886&action=review

> Tools/Scripts/webkitpy/common/system/outputcapture.py:114
> +        self.captured_output = None

This should return (None, None, None) to match up with how restore_output() behaves.

> Tools/Scripts/webkitpy/common/system/outputcapture_unittest.py:61
> +    def test_output_capture_scope(self):

Please add a test where OutputScope takes an OutputCapture as an argument.
Comment 7 Daniel Bates 2017-06-02 19:16:30 PDT
Comment on attachment 311886 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=311886&action=review

> Tools/Scripts/webkitpy/common/system/outputcapture.py:120
> +        self.captured_output = self._output_capture.restore_output()

exc_val => exc_value
exc_tb => traceback

(To match https://docs.python.org/2/reference/datamodel.html#context-managers)
Comment 8 Jonathan Bedard 2017-06-03 08:34:19 PDT
Created attachment 311927 [details]
Patch for landing
Comment 9 WebKit Commit Bot 2017-06-03 09:11:34 PDT
Comment on attachment 311927 [details]
Patch for landing

Clearing flags on attachment: 311927

Committed r217758: <http://trac.webkit.org/changeset/217758>
Comment 10 WebKit Commit Bot 2017-06-03 09:11:35 PDT
All reviewed patches have been landed.  Closing bug.