log caller

Discussion in 'Python' started by Matthew Peter, Jun 27, 2007.

  1. Is it possible to print the function calls to a module? Like:

    test.py
    import mymod
    print mymod.x()


    mymod.py
    # each time a function is called we print out the called function and module
    print 'Func call: %s from %s' % (???, ???)

    def x():
    return 'hello'

    Where would I pick up the ??? variables? A brief example would be nice too :) Thanks
    in advance!



    ____________________________________________________________________________________
    The fish are biting.
    Get more visitors on your site using Yahoo! Search Marketing.
    http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php
    Matthew Peter, Jun 27, 2007
    #1
    1. Advertising

  2. Matthew Peter

    Guest

    On Jun 27, 2:42 pm, Matthew Peter <> wrote:
    > Is it possible to print the function calls to a module? Like:
    >
    > test.py
    > import mymod
    > print mymod.x()
    >
    > mymod.py
    > # each time a function is called we print out the called function and module
    > print 'Func call: %s from %s' % (???, ???)
    >
    > def x():
    > return 'hello'
    >
    > Where would I pick up the ??? variables? A brief example would be nice too :) Thanks
    > in advance!




    You can use a decorator to wrap the function. You can use
    sys._getframe or
    inspect.stack to get information about the caller. The examples from
    the decorator module are very useful:

    http://www.phyast.pitt.edu/~micheles/python/documentation.html

    If for some reason you can't edit mymod.py to add the decorators, you
    can still wrap the function:

    import mymod
    import inspect

    try:
    from functools import update_wrapper
    except ImportError:
    def decorator_trace(f):
    def newf():
    caller = inspect.stack()[1]
    print 'Caller is line %d of %s' % (caller[2], caller[1])
    print 'Calling: %s from %s' % (f.__name__, f.__module__)
    return f()
    newf.__name__ = f.__name__
    newf.__dict__.update(f.__dict__)
    newf.__doc__ = f.__doc__
    newf.__module__ = f.__module__
    return newf
    else:
    def decorator_trace(f):
    def newf():
    caller = inspect.stack()[1]
    print 'Caller is line %d of %s' % (caller[2], caller[1])
    print 'Calling: %s from %s' % (f.__name__, f.__module__)
    return f()
    return update_wrapper(newf, f)


    mymod.x = decorator_trace(mymod.x)
    greetings = mymod.x()
    print greetings


    but this approach has the shortcoming mentioned in the article.

    --
    Hope this helps,
    Steven
    , Jun 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik_the_boss
    Replies:
    0
    Views:
    2,630
    Henrik_the_boss
    Nov 5, 2003
  2. Amratash
    Replies:
    0
    Views:
    501
    Amratash
    Apr 13, 2004
  3. =?Utf-8?B?VG9tIFdpbmdlcnQ=?=

    My.Log.Writeexception not writing to Application Event Log.

    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=, Jan 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,354
    =?Utf-8?B?VG9tIFdpbmdlcnQ=?=
    Jan 20, 2006
  4. Mark
    Replies:
    2
    Views:
    386
  5. Gildor Oronar
    Replies:
    6
    Views:
    150
    Gildor Oronar
    Sep 6, 2013
Loading...

Share This Page