Changeset 3295

Show
Ignore:
Timestamp:
07/30/2007 06:31:45 AM (1 year ago)
Author:
ed
Message:

Added the loggit function to dBug. It works best as a decorator for any method/function call you want to track in order to determine how often it is called, and the program stack that is leading to that call. It helps to track inefficiencies, such as a form's refresh() being called multiple times from various other objects.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/dBug.py

    r3054 r3295  
    11# -*- coding: utf-8 -*- 
     2import os 
     3import sys 
     4import time 
    25import inspect 
    36from cStringIO import StringIO 
     7import dabo 
     8 
    49 
    510def logPoint(msg="", levels=None): 
     
    2934    # I actually logged the result, but you could also print it: 
    3035    return s 
     36 
     37 
     38def loggit(fnc): 
     39    """Decorator function to create a log of all methods as they are called. To use 
     40    it, modify all your methods from: 
     41     
     42        def someMethod(...): 
     43         
     44    to: 
     45     
     46        @loggit 
     47        def someMethod(...): 
     48         
     49    Be sure to add: 
     50     
     51    from dabo.dBug import loggit 
     52     
     53    to the import statements for every file that uses loggit. You can set the name and 
     54    location of the log file by overriding the setting for dabo.loggitFile. By default, this 
     55    value will be 'functionCall.log'. 
     56    """ 
     57    try: 
     58        if loggit.fhwr: 
     59            pass 
     60    except: 
     61        # ... open it 
     62        fname = dabo.loggitFile 
     63        loggit.fhwr = open(fname, "a") 
     64    def wrapped(*args, **kwargs): 
     65        loggit.fhwr.write("\n%s\n" % time.strftime("%Y-%m-%d %H:%M:%S")) 
     66        loggit.fhwr.write("%s\n" % fnc) 
     67        if args: 
     68            loggit.fhwr.write("\tARGS:") 
     69            for ag in args: 
     70                try: 
     71                    loggit.fhwr.write(" %s" % ag) 
     72                except: 
     73                    loggit.fhwr.write(" ERR") 
     74            loggit.fhwr.write("\n") 
     75        if kwargs: 
     76            loggit.fhwr.write("\tKWARGS:%s\n" % kwargs) 
     77        for stk in inspect.stack()[1:-7]: 
     78            loggit.fhwr.write("\t%s, %s, line %s\n" % (os.path.split(stk[1])[1], stk[3], stk[2])) 
     79        result = fnc(*args, **kwargs) 
     80        return result 
     81    wrapped.__doc__ = fnc.__doc__ 
     82    return wrapped 
  • trunk/dabo/settings.py

    r3054 r3295  
    105105defaultEncoding = "utf-8" 
    106106 
     107# Default log file for the dabo.dBug.loggit function 
     108loggitFile = "functionCall.log" 
    107109 
    108110### Settings - end