1919comp.lang.python, and influenced by Apache's log4j system.
2020
2121Should work under Python versions >= 1.5.2, except that source line
22- information is not available unless 'inspect ' is.
22+ information is not available unless 'sys._getframe() ' is.
2323
2424Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
2525
3333 import threading
3434except ImportError :
3535 thread = None
36- try :
37- import inspect
38- except ImportError :
39- inspect = None
4036
4137__author__ = "Vinay Sajip <[email protected] >" 4238__status__ = "alpha"
5652 _srcfile = __file__
5753_srcfile = os .path .normcase (_srcfile )
5854
55+ # _srcfile is only used in conjunction with sys._getframe().
56+ # To provide compatibility with older versions of Python, set _srcfile
57+ # to None if _getframe() is not available; this value will prevent
58+ # findCaller() from being called.
59+ if not hasattr (sys , "_getframe" ):
60+ _srcfile = None
61+
5962#
6063#_startTime is used as the base when calculating the relative time of events
6164#
@@ -927,19 +930,14 @@ def findCaller(self):
927930 Find the stack frame of the caller so that we can note the source
928931 file name and line number.
929932 """
930- rv = (None , None )
931- frame = inspect .currentframe ().f_back
932- while frame :
933- sfn = inspect .getsourcefile (frame )
934- if sfn :
935- sfn = os .path .normcase (sfn )
936- if sfn != _srcfile :
937- #print frame.f_code.co_code
938- lineno = inspect .getlineno (frame )
939- rv = (sfn , lineno )
940- break
941- frame = frame .f_back
942- return rv
933+ f = sys ._getframe (1 )
934+ while 1 :
935+ co = f .f_code
936+ filename = os .path .normcase (co .co_filename )
937+ if filename == _srcfile :
938+ f = f .f_back
939+ continue
940+ return filename , f .f_lineno
943941
944942 def makeRecord (self , name , level , fn , lno , msg , args , exc_info ):
945943 """
@@ -953,12 +951,8 @@ def _log(self, level, msg, args, exc_info=None):
953951 Low-level logging routine which creates a LogRecord and then calls
954952 all the handlers of this logger to handle the record.
955953 """
956- if inspect and _srcfile :
957- _acquireLock ()
958- try :
959- fn , lno = self .findCaller ()
960- finally :
961- _releaseLock ()
954+ if _srcfile :
955+ fn , lno = self .findCaller ()
962956 else :
963957 fn , lno = "<unknown file>" , 0
964958 if exc_info :
0 commit comments