1
2
3
4
5 import inspect
6 import sys
7 import traceback
8
9 debug = True
10 compress = True
11
12 _name = sys.argv[0]
13 _ni = _name.rfind('/')
14 if _ni >=0 and _ni != len(_name)-1:
15 _name = _name[_ni+1:]
16
17 logfd = sys.stderr
18
19 _last = ()
20 _counter = 0
21
22
24 stack = inspect.stack()
25 uplocal = stack[1][0].f_locals
26 stack = None
27 if not uplocal.has_key('__note'):
28 uplocal['__note'] = {}
29 uplocal['__note'][name] = val
30 uplocal = None
31
32
34 stack = inspect.stack()
35 uplocal = stack[1][0].f_locals
36 stack = None
37 if not uplocal.has_key('__show'):
38 uplocal['__show'] = []
39 uplocal['__show'].append(name)
40 uplocal = None
41
42
43
45
46 stack = inspect.stack()
47 o = []
48 for level in range(len(stack)-1, n, -1):
49 uplocal = stack[level][0].f_locals
50 if '__show' in uplocal:
51 for k in uplocal['__show']:
52 if k in uplocal:
53 o.append( (k, uplocal[k]) )
54 del uplocal['__show']
55
56 if '__note' in uplocal:
57 o.extend( uplocal['__note'].items() )
58 del uplocal['__note']
59
60 del uplocal
61 del stack
62 o.sort()
63
64
65
66 i = 1
67 while i < len(o):
68 if o[i] == o[i-1]:
69 del o[i]
70 else:
71 i += 1
72
73 return o
74
75
84
85
100
101
103 """Output a fatal error message and terminate."""
104 e = 'ERR: %s: %s' % (_name, s)
105 exit(1, e)
106
107
109 """Output a non-fatal warning."""
110
111 _display('WARN', _name, s, 1)
112
113
115 """Output useful information."""
116 global _q
117 _display('INFO', _name, s, 1)
118
119
121 """Call this inside an except statement.
122 It will report the exception and any other information it has."""
123 type, value, tback = sys.exc_info()
124 if extext is None:
125 extext = "die.catch: exception caught.\n"
126 _dumpmem(1)
127 traceback.print_exception(type, value, tback)
128 sys.stderr.flush()
129 sys.stdout.flush()
130
131
133 """Call this inside an except statement. It will report
134 all information and then exit."""
135 type, value, tback = sys.exc_info()
136 if extext is None:
137 extext = "die.catch: exception caught.\n"
138 _dumpmem(1)
139 traceback.print_exception(type, value, tback)
140 sys.stderr.flush()
141 sys.stdout.flush()
142 if text is not None:
143 sys.stdout.write('%s\n' % text)
144 sys.stdout.flush()
145 logfd.write('%s\n' % text)
146 logfd.flush()
147 sys.exit(n)
148
149
151 """Output debugging information, if debug is nonzero."""
152 if debug:
153 _display('DBG', _name, s, 1)
154
155
156 -def exit(n, text=None):
165
166
168 nl = _notes(0)
169 for (k, v) in nl:
170 if k == key:
171 return v
172 raise KeyError, key
173
174
175
176 if __name__ == '__main__':
177 debug = 1
178 info("You should see a debug message next.")
179 dbg("This is the debug message.")
180 debug = 0
181 note("gleep", "oldest note")
182 note("foo", "bar")
183 note("foo", "fleep")
184 note("foo", "foo")
185 note("farf", "newest note")
186 info("You should not see a debug message next.")
187 dbg("This is the debug message you shouldn't see.")
188 warn("This is a warning.")
189 warn("This is a warning.")
190 warn("This is a warning.")
191 info("You should have seen three identical warnings just above.")
192 die("This is the end.")
193