Trace function with automatic source code location reporting

Jürgen Hermann jh at web.de
Wed Feb 7 16:32:55 EST 2001


Hi!

The following is the core of a tracing function, is there any less obscure
way to get the location of the caller?

def trace(*args):
    try:
        raise RuntimeError
    except RuntimeError:
        import sys, traceback, string
        caller = sys.exc_info()[2].tb_frame.f_back
        print "%s [%d]: %s" % (caller.f_code.co_filename, caller.f_lineno,
string.join(map(str, args)))
        del caller

trace("first call", 1)

trace("second call", 2.0)

def func():
    trace("third call", 1+2)

func()


Bye, Jürgen





More information about the Python-list mailing list