Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 829dc51

Browse files
committed
Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
1 parent 84df97f commit 829dc51

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

Lib/logging/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
__author__ = "Vinay Sajip <[email protected]>"
3838
__status__ = "beta"
3939
__version__ = "0.4.9.6"
40-
__date__ = "20 October 2004"
40+
__date__ = "03 February 2005"
4141

4242
#---------------------------------------------------------------------------
4343
# Miscellaneous module data
4444
#---------------------------------------------------------------------------
4545

4646
#
47-
#_srcfile is used when walking the stack to check when we've got the first
47+
# _srcfile is used when walking the stack to check when we've got the first
4848
# caller stack frame.
4949
#
5050
if string.lower(__file__[-4:]) in ['.pyc', '.pyo']:
@@ -53,12 +53,23 @@
5353
_srcfile = __file__
5454
_srcfile = os.path.normcase(_srcfile)
5555

56+
# next bit filched from 1.5.2's inspect.py
57+
def currentframe():
58+
"""Return the frame object for the caller's stack frame."""
59+
try:
60+
raise 'catch me'
61+
except:
62+
return sys.exc_traceback.tb_frame.f_back
63+
64+
if hasattr(sys, '_getframe'): currentframe = sys._getframe
65+
# done filching
66+
5667
# _srcfile is only used in conjunction with sys._getframe().
5768
# To provide compatibility with older versions of Python, set _srcfile
5869
# to None if _getframe() is not available; this value will prevent
5970
# findCaller() from being called.
60-
if not hasattr(sys, "_getframe"):
61-
_srcfile = None
71+
#if not hasattr(sys, "_getframe"):
72+
# _srcfile = None
6273

6374
#
6475
#_startTime is used as the base when calculating the relative time of events
@@ -1005,16 +1016,16 @@ def log(self, level, msg, *args, **kwargs):
10051016
def findCaller(self):
10061017
"""
10071018
Find the stack frame of the caller so that we can note the source
1008-
file name and line number.
1019+
file name, line number and function name.
10091020
"""
1010-
f = sys._getframe(1)
1021+
f = currentframe().f_back
10111022
while 1:
10121023
co = f.f_code
10131024
filename = os.path.normcase(co.co_filename)
10141025
if filename == _srcfile:
10151026
f = f.f_back
10161027
continue
1017-
return filename, f.f_lineno
1028+
return filename, f.f_lineno, co.co_name
10181029

10191030
def makeRecord(self, name, level, fn, lno, msg, args, exc_info):
10201031
"""
@@ -1029,9 +1040,9 @@ def _log(self, level, msg, args, exc_info=None):
10291040
all the handlers of this logger to handle the record.
10301041
"""
10311042
if _srcfile:
1032-
fn, lno = self.findCaller()
1043+
fn, lno, func = self.findCaller()
10331044
else:
1034-
fn, lno = "<unknown file>", 0
1045+
fn, lno, func = "(unknown file)", 0, "(unknown function)"
10351046
if exc_info:
10361047
if type(exc_info) != types.TupleType:
10371048
exc_info = sys.exc_info()

0 commit comments

Comments
 (0)