1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 from __future__
import print_function
12 def CaptureLogging(f
):
13 '''Call the function |f|, capturing any logging output generated. |f| must
14 take no arguments. Returns a list of LogRecords that were emitted.
17 class Capture(object):
18 def filter(self
, record
):
22 logging
.getLogger('').addFilter(cf
)
24 logging
.getLogger('').removeFilter(cf
)
29 def EnableLogging(name
):
30 '''Returns the output of the log with |name| to stdout.
33 return _ReplaceLogging(name
, lambda message
, *args
: print(message
% args
))
36 def DisableLogging(name
):
37 '''Disables the log with |name| for the duration of the decorated function.
39 return _ReplaceLogging(name
, lambda _
, *args
: None)
42 def _ReplaceLogging(name
, replacement
):
44 def impl(*args
, **optargs
):
45 saved
= getattr(logging
, name
)
46 setattr(logging
, name
, replacement
)
48 return fn(*args
, **optargs
)
50 setattr(logging
, name
, saved
)
55 def ChromiumPath(*path
):
57 sys
.path
[0], '..', '..', '..', '..', '..', *path
)
60 def ReadFile(*path
, **read_args
):
61 with
open(ChromiumPath(*path
), **read_args
) as f
: