5151
5252import attr
5353from typing_extensions import ParamSpec
54+ from traceback import format_stack
5455
5556from twisted .internet import defer , threads
5657from twisted .python .threadpool import ThreadPool
@@ -379,6 +380,14 @@ def set_current_context(
379380
380381 def __enter__ (self ) -> "LoggingContext" :
381382 """Enters this logging context into thread local storage"""
383+ logger .debug (
384+ "LoggingContext(%s) enter (previous_context=%s) (source: %s)" ,
385+ self ,
386+ self .previous_context ,
387+ format_stack ()[- 2 ]
388+ .replace ("/home/eric/Documents/github/element/synapse/" , "" )
389+ .replace ("\n " , "->" ),
390+ )
382391 old_context = set_current_context (self )
383392 if self .previous_context != old_context :
384393 logcontext_error (
@@ -401,13 +410,24 @@ def __exit__(
401410 Returns:
402411 None to avoid suppressing any exceptions that were thrown.
403412 """
413+ logger .debug (
414+ "LoggingContext(%s) exit: returning to previous_context=%s (source: %s)" ,
415+ self ,
416+ self .previous_context ,
417+ format_stack ()[- 2 ]
418+ .replace ("/home/eric/Documents/github/element/synapse/" , "" )
419+ .replace ("\n " , "->" ),
420+ )
404421 current = set_current_context (self .previous_context )
405422 if current is not self :
406423 if current is SENTINEL_CONTEXT :
407- logcontext_error ("Expected logging context %s was lost" % (self ,))
424+ logcontext_error (
425+ "LoggingContext: Expected logging context %s was lost" % (self ,)
426+ )
408427 else :
409428 logcontext_error (
410- "Expected logging context %s but found %s" % (self , current )
429+ "LoggingContext: Expected logging context %s but found %s"
430+ % (self , current )
411431 )
412432
413433 # the fact that we are here suggests that the caller thinks that everything
@@ -628,6 +648,14 @@ def __init__(
628648 self ._new_context = new_context
629649
630650 def __enter__ (self ) -> None :
651+ logger .debug (
652+ "PreserveLoggingContext(%s) enter (old_context=%s) (source: %s)" ,
653+ self ._new_context ,
654+ current_context (),
655+ format_stack ()[- 2 ]
656+ .replace ("/home/eric/Documents/github/element/synapse/" , "" )
657+ .replace ("\n " , "->" ),
658+ )
631659 self ._old_context = set_current_context (self ._new_context )
632660
633661 def __exit__ (
@@ -636,16 +664,25 @@ def __exit__(
636664 value : Optional [BaseException ],
637665 traceback : Optional [TracebackType ],
638666 ) -> None :
667+ logger .debug (
668+ "PreserveLoggingContext(%s) exit returning to old_context=%s (source: %s)" ,
669+ self ._new_context ,
670+ self ._old_context ,
671+ format_stack ()[- 2 ]
672+ .replace ("/home/eric/Documents/github/element/synapse/" , "" )
673+ .replace ("\n " , "->" ),
674+ )
639675 context = set_current_context (self ._old_context )
640676
641677 if context != self ._new_context :
642678 if not context :
643679 logcontext_error (
644- "Expected logging context %s was lost" % (self ._new_context ,)
680+ "PreserveLoggingContext: Expected logging context %s was lost"
681+ % (self ._new_context ,)
645682 )
646683 else :
647684 logcontext_error (
648- "Expected logging context %s but found %s"
685+ "PreserveLoggingContext: Expected logging context %s but found %s"
649686 % (
650687 self ._new_context ,
651688 context ,
@@ -677,6 +714,10 @@ def set_current_context(context: LoggingContextOrSentinel) -> LoggingContextOrSe
677714
678715 current = current_context ()
679716
717+ logger .debug (
718+ "set_current_context(%s) old_context=%s - %s" , context , current , format_stack ()
719+ )
720+
680721 if current is not context :
681722 rusage = get_thread_resource_usage ()
682723 current .stop (rusage )
0 commit comments