| | 36 | |
|---|
| | 37 | |
|---|
| | 38 | def 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 |
|---|