本文實例講述了Python記錄詳細調用堆棧日志的方法。分享給大家供大家參考。具體實現方法如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import sys import os def detailtrace(info): retStr = "" curindex=0 f = sys._getframe() f = f.f_back # first frame is detailtrace, ignore it while hasattr(f, "f_code"): co = f.f_code retStr = "%s(%s:%s)->"%(os.path.basename(co.co_filename), co.co_name, f.f_lineno) + retStr f = f.f_back print retStr+info def foo(): detailtrace("hello world") def bar(): foo() def main(): bar() if __name__ == "__main__": main()輸出:
aaa1.py(
希望本文所述對大家的Python程序設計有所幫助。