不久前我写了一个模块,它做了类似的事情。
我的笔记说它在python2和python3中都能工作。在from __future__ import print_function
from itertools import chain
import traceback
import sys
def stackdump(id='', msg='HERE'):
print('ENTERING STACK_DUMP' + (': '+id) if id else '')
raw_tb = traceback.extract_stack()
entries = traceback.format_list(raw_tb)
# Remove the last two entries for the call to extract_stack() and to
# the one before that, this function. Each entry consists of single
# string with consisting of two lines, the script file path then the
# line of source code making the call to this function.
del entries[-2:]
# Split the stack entries on line boundaries.
lines = list(chain.from_iterable(line.splitlines() for line in entries))
if msg: # Append it to last line with na