@@ -65,6 +65,11 @@ def get_thread_resource_usage() -> "Optional[resource._RUsage]":
65
65
return None
66
66
67
67
68
+ # a hook which can be set during testing to assert that we aren't abusing logcontexts.
69
+ def logcontext_error (msg : str ):
70
+ logger .warning (msg )
71
+
72
+
68
73
# get an id for the current thread.
69
74
#
70
75
# threading.get_ident doesn't actually return an OS-level tid, and annoyingly,
@@ -330,10 +335,9 @@ def __enter__(self) -> "LoggingContext":
330
335
"""Enters this logging context into thread local storage"""
331
336
old_context = set_current_context (self )
332
337
if self .previous_context != old_context :
333
- logger .warning (
334
- "Expected previous context %r, found %r" ,
335
- self .previous_context ,
336
- old_context ,
338
+ logcontext_error (
339
+ "Expected previous context %r, found %r"
340
+ % (self .previous_context , old_context ,)
337
341
)
338
342
return self
339
343
@@ -346,10 +350,10 @@ def __exit__(self, type, value, traceback) -> None:
346
350
current = set_current_context (self .previous_context )
347
351
if current is not self :
348
352
if current is SENTINEL_CONTEXT :
349
- logger . warning ("Expected logging context %s was lost" , self )
353
+ logcontext_error ("Expected logging context %s was lost" % ( self ,) )
350
354
else :
351
- logger . warning (
352
- "Expected logging context %s but found %s" , self , current
355
+ logcontext_error (
356
+ "Expected logging context %s but found %s" % ( self , current )
353
357
)
354
358
355
359
# the fact that we are here suggests that the caller thinks that everything
@@ -387,16 +391,16 @@ def start(self, rusage: "Optional[resource._RUsage]") -> None:
387
391
support getrusuage.
388
392
"""
389
393
if get_thread_id () != self .main_thread :
390
- logger . warning ("Started logcontext %s on different thread" , self )
394
+ logcontext_error ("Started logcontext %s on different thread" % ( self ,) )
391
395
return
392
396
393
397
if self .finished :
394
- logger . warning ("Re-starting finished log context %s" , self )
398
+ logcontext_error ("Re-starting finished log context %s" % ( self ,) )
395
399
396
400
# If we haven't already started record the thread resource usage so
397
401
# far
398
402
if self .usage_start :
399
- logger . warning ("Re-starting already-active log context %s" , self )
403
+ logcontext_error ("Re-starting already-active log context %s" % ( self ,) )
400
404
else :
401
405
self .usage_start = rusage
402
406
@@ -414,17 +418,17 @@ def stop(self, rusage: "Optional[resource._RUsage]") -> None:
414
418
415
419
try :
416
420
if get_thread_id () != self .main_thread :
417
- logger . warning ("Stopped logcontext %s on different thread" , self )
421
+ logcontext_error ("Stopped logcontext %s on different thread" % ( self ,) )
418
422
return
419
423
420
424
if not rusage :
421
425
return
422
426
423
427
# Record the cpu used since we started
424
428
if not self .usage_start :
425
- logger . warning (
426
- "Called stop on logcontext %s without recording a start rusage" ,
427
- self ,
429
+ logcontext_error (
430
+ "Called stop on logcontext %s without recording a start rusage"
431
+ % ( self ,)
428
432
)
429
433
return
430
434
@@ -584,14 +588,13 @@ def __exit__(self, type, value, traceback) -> None:
584
588
585
589
if context != self ._new_context :
586
590
if not context :
587
- logger . warning (
588
- "Expected logging context %s was lost" , self ._new_context
591
+ logcontext_error (
592
+ "Expected logging context %s was lost" % ( self ._new_context ,)
589
593
)
590
594
else :
591
- logger .warning (
592
- "Expected logging context %s but found %s" ,
593
- self ._new_context ,
594
- context ,
595
+ logcontext_error (
596
+ "Expected logging context %s but found %s"
597
+ % (self ._new_context , context ,)
595
598
)
596
599
597
600
0 commit comments