root/trunk/dabo/dBug.py

Revision 4179, 2.2 kB (checked in by ed, 5 months ago)

Removed bare 'except:' from the code.

  • Property svn:eol-style set to native
Line 
1 # -*- coding: utf-8 -*-
2 import os
3 import sys
4 import time
5 import inspect
6 from cStringIO import StringIO
7 import dabo
8
9
10 def logPoint(msg="", levels=None):
11     if levels is None:
12         # Default to 6, which works in most cases
13         levels = 6
14     stack = inspect.stack()
15     # get rid of logPoint's part of the stack:
16     stack = stack[1:]
17     stack.reverse()
18     output = StringIO()
19     if msg:
20         output.write(str(msg) + "\n")
21    
22     stackSection = stack[-1*levels:]
23     for stackLine in stackSection:
24         frame, filename, line, funcname, lines, unknown = stackLine
25         if filename.endswith("/unittest.py"):
26             # unittest.py code is a boring part of the traceback
27             continue
28         if filename.startswith("./"):
29             filename = filename[2:]
30         output.write("%s:%s in %s:\n" % (filename, line, funcname))
31         if lines:
32             output.write(%s\n" % "".join(lines)[:-1])
33     s = output.getvalue()
34     # I actually logged the result, but you could also print it:
35     return s
36
37
38 def mainProgram():
39     """Returns the name of first program in the call stack"""
40     return inspect.stack()[-1][1]
41    
42
43 def loggit(fnc):
44     """Decorator function to create a log of all methods as they are called. To use
45     it, modify all your methods from:
46     
47         def someMethod(...):
48         
49     to:
50     
51         @loggit
52         def someMethod(...):
53         
54     Be sure to add:
55     
56     from dabo.dBug import loggit
57     
58     to the import statements for every file that uses loggit. You can set the name and
59     location of the log file by overriding the setting for dabo.loggitFile. By default, this
60     value will be 'functionCall.log'.
61     """
62     try:
63         loggit.fhwr
64     except AttributeError:
65         # ... open it
66         fname = dabo.loggitFile
67         loggit.fhwr = open(fname, "a")
68     def wrapped(*args, **kwargs):
69         loggit.fhwr.write("\n%s\n" % time.strftime("%Y-%m-%d %H:%M:%S"))
70         loggit.fhwr.write("%s\n" % fnc)
71         if args:
72             loggit.fhwr.write("\tARGS:")
73             for ag in args:
74                 try:
75                     loggit.fhwr.write(" %s" % ag)
76                 except StandardError, e:
77                     loggit.fhwr.write(" ERR: %s" % e)
78             loggit.fhwr.write("\n")
79         if kwargs:
80             loggit.fhwr.write("\tKWARGS:%s\n" % kwargs)
81         for stk in inspect.stack()[1:-7]:
82             loggit.fhwr.write("\t%s, %s, line %s\n" % (os.path.split(stk[1])[1], stk[3], stk[2]))
83         result = fnc(*args, **kwargs)
84         loggit.fhwr.flush()
85         return result
86     wrapped.__doc__ = fnc.__doc__
87     return wrapped
Note: See TracBrowser for help on using the browser.