Skip to content

Commit 94597f9

Browse files
committed
Instrument logger so lines can be connected to testcases
Signed-off-by: Matthias Büchse <matthias.buechse@alasca.cloud>
1 parent a9ee058 commit 94597f9

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

Tests/iaas/openstack_test.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,35 @@ def make_container(cloud):
205205
return c
206206

207207

208+
class _Filter:
209+
_instance = None
210+
211+
def __init__(self):
212+
self.components = []
213+
214+
def __call__(self, record):
215+
if len(self.components) == 1 or len(self.components) == 2 and self.components[0].replace('-', '_') == self.components[1]:
216+
record.msg = f'{self.components[0]}: {record.msg}'
217+
elif self.components:
218+
record.msg = f'[{self.components[-1]}] {record.msg}'
219+
return True
220+
221+
@classmethod
222+
def install(cls, logger):
223+
if cls._instance:
224+
return
225+
cls._instance = cls()
226+
logger.handlers[0].filters.append(cls._instance)
227+
228+
@classmethod
229+
def push(cls, name):
230+
cls._instance.components.append(name)
231+
232+
@classmethod
233+
def pop(cls):
234+
del cls._instance.components[-1]
235+
236+
208237
class Container:
209238
"""
210239
This class does lazy evaluation and memoization. You register any potential value either
@@ -233,13 +262,16 @@ def __getattr__(self, key):
233262
val = self._values.get(key)
234263
if val is None:
235264
# I thought this was too verbose, but it massively helps classifying log messages
236-
logger.debug(f'... {key}')
265+
# logger.debug(f'... {key}')
266+
_Filter.push(key)
237267
try:
238268
ret = self._functions[key](self)
239269
except BaseException as e:
240270
val = (True, e)
241271
else:
242272
val = (False, ret)
273+
_Filter.pop()
274+
# logger.debug(f'... /{key}')
243275
self._values[key] = val
244276
error, ret = val
245277
if error:
@@ -290,6 +322,8 @@ def harness(name, *check_fns):
290322
- 'PASS' otherwise
291323
"""
292324
logger.debug(f'** {name}')
325+
_Filter.push(name)
326+
293327
messages = []
294328
try:
295329
results = [check_fn() for check_fn in check_fns]
@@ -301,6 +335,8 @@ def harness(name, *check_fns):
301335
for r in results:
302336
fails += _eval_result(r, messages)
303337
result = ['FAIL', 'PASS'][fails == 0]
338+
finally:
339+
_Filter.pop()
304340
# this is quite redundant
305341
# logger.debug(f'** computation end for {name}')
306342
for msg in messages:
@@ -325,6 +361,7 @@ def run_sanity_checks(container):
325361
def main(argv):
326362
# configure logging, disable verbose library logging
327363
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
364+
_Filter.install(logging.getLogger())
328365
openstack.enable_logging(debug=False)
329366
cloud = None
330367

0 commit comments

Comments
 (0)