Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 88b6a03

Browse files
author
Matthew Carlson
committed
Merged PR 631: [feature] properly filter log records when they're modified by a logging.filter
When you modify a LogRecord inside a filter, whether it should be emitted or not based on the current level is determined by the handler before it applies the filter in the base class. This just reverses the order of the evaluation to make sure we evaluate the filter before the level. It also includes pieces getting a named logger rather than the implicit root logger.
1 parent 7074b85 commit 88b6a03

File tree

5 files changed

+121
-20
lines changed

5 files changed

+121
-20
lines changed

MuPythonLibrary/MuAnsiHandler.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,24 @@ def __init__(self, stream=None, strip=None, convert=None):
276276
self._default_back = self._back
277277
self._default_style = self._style
278278

279+
def handle(self, record):
280+
"""
281+
Conditionally emit the specified logging record.
282+
Emission depends on filters which may have been added to the handler.
283+
Wrap the actual emission of the record with acquisition/release of
284+
the I/O thread lock. Returns whether the filter passed the record for
285+
emission.
286+
"""
287+
288+
rv = self.filter(record)
289+
if rv and record.levelno >= self.level:
290+
self.acquire()
291+
try:
292+
self.emit(record)
293+
finally:
294+
self.release()
295+
return rv
296+
279297
def get_win32_calls(self):
280298
if self.convert:
281299
return {

MuPythonLibrary/MuFileHandler.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# @file MuAnsiHandler.py
2+
# Handle basic logging outputting to files
3+
##
4+
# Copyright (c) 2018, Microsoft Corporation
5+
#
6+
# All rights reserved.
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
# 1. Redistributions of source code must retain the above copyright notice,
10+
# this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright notice,
12+
# this list of conditions and the following disclaimer in the documentation
13+
# and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS
22+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCEOR OTHERWISE)
24+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
# POSSIBILITY OF SUCH DAMAGE.
26+
##
27+
import logging
28+
29+
30+
class FileHandler(logging.FileHandler):
31+
def __init__(self, filename, mode='w+'):
32+
logging.FileHandler.__init__(self, filename, mode=mode)
33+
34+
def handle(self, record):
35+
"""
36+
Conditionally emit the specified logging record.
37+
Emission depends on filters which may have been added to the handler.
38+
Wrap the actual emission of the record with acquisition/release of
39+
the I/O thread lock. Returns whether the filter passed the record for
40+
emission.
41+
"""
42+
43+
rv = self.filter(record)
44+
if rv and record.levelno >= self.level:
45+
self.acquire()
46+
try:
47+
self.emit(record)
48+
finally:
49+
self.release()
50+
return rv

MuPythonLibrary/MuMarkdownHandler.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ def emit(self, record):
6666

6767
# self.flush()
6868

69+
def handle(self, record):
70+
"""
71+
Conditionally emit the specified logging record.
72+
Emission depends on filters which may have been added to the handler.
73+
Wrap the actual emission of the record with acquisition/release of
74+
the I/O thread lock. Returns whether the filter passed the record for
75+
emission.
76+
"""
77+
78+
rv = self.filter(record)
79+
if rv and record.levelno >= self.level:
80+
self.acquire()
81+
try:
82+
self.emit(record)
83+
finally:
84+
self.release()
85+
return rv
86+
6987
@staticmethod
7088
def __convert_to_markdownlink(text):
7189
# Using info from here https://stackoverflow.com/a/38507669

MuPythonLibrary/MuStringHandler.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,23 @@ def __init__(self):
3838
logging.Handler.__init__(self)
3939
self.stream = StringIO()
4040

41-
# just returns the message indefinitely
42-
def format(self, record):
43-
return record.message
41+
def handle(self, record):
42+
"""
43+
Conditionally emit the specified logging record.
44+
Emission depends on filters which may have been added to the handler.
45+
Wrap the actual emission of the record with acquisition/release of
46+
the I/O thread lock. Returns whether the filter passed the record for
47+
emission.
48+
"""
49+
50+
rv = self.filter(record)
51+
if rv and record.levelno >= self.level:
52+
self.acquire()
53+
try:
54+
self.emit(record)
55+
finally:
56+
self.release()
57+
return rv
4458

4559
def readlines(self, hint=-1):
4660
return self.stream.readlines(hint)

MuPythonLibrary/Uefi/EdkII/PathUtilities.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ class Edk2Path(object):
4242
#
4343
def __init__(self, ws, packagepathlist):
4444
self.WorkspacePath = ws
45+
self.logger = logging.getLogger("Edk2Path")
4546
if(not os.path.isabs(ws)):
4647
self.WorkspacePath = os.path.abspath(os.path.join(os.getcwd(), ws))
4748

4849
if(not os.path.isdir(self.WorkspacePath)):
49-
logging.error("Workspace path invalid. {0}".format(ws))
50+
self.logger.error("Workspace path invalid. {0}".format(ws))
5051
raise Exception("Workspace path invalid. {0}".format(ws))
5152

5253
# Set PackagePath
@@ -66,7 +67,7 @@ def __init__(self, ws, packagepathlist):
6667
error = False
6768
for a in self.PackagePathList:
6869
if(not os.path.isdir(a)):
69-
logging.error("Invalid package path entry {0}".format(a))
70+
self.logger.error("Invalid package path entry {0}".format(a))
7071
error = True
7172

7273
# report error
@@ -84,8 +85,8 @@ def GetEdk2RelativePathFromAbsolutePath(self, abspath):
8485
# found our path...now lets correct for case
8586
relpath = abspath[len(a):]
8687
found = True
87-
logging.debug("Successfully converted AbsPath to Edk2Relative Path using PackagePath")
88-
logging.debug("AbsolutePath: %s found in PackagePath: %s" % (abspath, a))
88+
self.logger.debug("Successfully converted AbsPath to Edk2Relative Path using PackagePath")
89+
self.logger.debug("AbsolutePath: %s found in PackagePath: %s" % (abspath, a))
8990
break
9091

9192
if(not found):
@@ -95,16 +96,16 @@ def GetEdk2RelativePathFromAbsolutePath(self, abspath):
9596
# found our path...now lets correct for case
9697
relpath = abspath[len(self.WorkspacePath):]
9798
found = True
98-
logging.debug("Successfully converted AbsPath to Edk2Relative Path using WorkspacePath")
99-
logging.debug("AbsolutePath: %s found in Workspace: %s" % (abspath, self.WorkspacePath))
99+
self.logger.debug("Successfully converted AbsPath to Edk2Relative Path using WorkspacePath")
100+
self.logger.debug("AbsolutePath: %s found in Workspace: %s" % (abspath, self.WorkspacePath))
100101

101102
if(found):
102103
relpath = relpath.replace(os.sep, "/")
103104
return relpath.lstrip("/")
104105

105106
# didn't find the path for conversion.
106-
logging.error("Failed to convert AbsPath to Edk2Relative Path")
107-
logging.error("AbsolutePath: %s" % abspath)
107+
self.logger.error("Failed to convert AbsPath to Edk2Relative Path")
108+
self.logger.error("AbsolutePath: %s" % abspath)
108109
return None
109110

110111
def GetAbsolutePathOnThisSytemFromEdk2RelativePath(self, relpath):
@@ -117,8 +118,8 @@ def GetAbsolutePathOnThisSytemFromEdk2RelativePath(self, relpath):
117118
abspath = os.path.join(a, relpath)
118119
if(os.path.exists(abspath)):
119120
return abspath
120-
logging.error("Failed to convert Edk2Relative Path to an Absolute Path on this system.")
121-
logging.error("Relative Path: %s" % relpath)
121+
self.logger.error("Failed to convert Edk2Relative Path to an Absolute Path on this system.")
122+
self.logger.error("Relative Path: %s" % relpath)
122123

123124
return None
124125

@@ -130,7 +131,7 @@ def GetAbsolutePathOnThisSytemFromEdk2RelativePath(self, relpath):
130131
#
131132
# @ret Name of Package that the module is in.
132133
def GetContainingPackage(self, InputPath):
133-
logging.debug("GetContainingPackage: %s" % InputPath)
134+
self.logger.debug("GetContainingPackage: %s" % InputPath)
134135

135136
dirpathprevious = os.path.dirname(InputPath)
136137
dirpath = os.path.dirname(InputPath)
@@ -141,15 +142,15 @@ def GetContainingPackage(self, InputPath):
141142
#
142143
if(dirpath in self.PackagePathList):
143144
a = os.path.basename(dirpathprevious)
144-
logging.debug("Reached Package Path. Using previous directory: %s" % a)
145+
self.logger.debug("Reached Package Path. Using previous directory: %s" % a)
145146
return a
146147
#
147148
# if at the root of the workspace return the previous dir.
148149
# this catches cases where a package has no DEC
149150
#
150151
if(dirpath == self.WorkspacePath):
151152
a = os.path.basename(dirpathprevious)
152-
logging.debug("Reached Workspace Path. Using previous directory: %s" % a)
153+
self.logger.debug("Reached Workspace Path. Using previous directory: %s" % a)
153154
return a
154155
#
155156
# Check for a DEC file in this folder
@@ -158,13 +159,13 @@ def GetContainingPackage(self, InputPath):
158159
for f in os.listdir(dirpath):
159160
if fnmatch.fnmatch(f, '*.dec'):
160161
a = os.path.basename(dirpath)
161-
logging.debug("Found DEC file at %s. Pkg is: %s", dirpath, a)
162+
self.logger.debug("Found DEC file at %s. Pkg is: %s", dirpath, a)
162163
return a
163164

164165
dirpathprevious = dirpath
165166
dirpath = os.path.dirname(dirpath)
166167

167-
logging.error("Failed to find containing package for %s" % InputPath)
168-
logging.info("PackagePath is: %s" % os.pathsep.join(self.PackagePathList))
169-
logging.info("Workspace path is : %s" % self.WorkspacePath)
168+
self.logger.error("Failed to find containing package for %s" % InputPath)
169+
self.logger.info("PackagePath is: %s" % os.pathsep.join(self.PackagePathList))
170+
self.logger.info("Workspace path is : %s" % self.WorkspacePath)
170171
return None

0 commit comments

Comments
 (0)