]>
code.delx.au - bg-scripts/blob - lib/GregDebug.py
8 DEBUG_LEVEL_DEBUG
= DEBUG_INCREMENT
* -2
9 DEBUG_LEVEL_LOW
= DEBUG_INCREMENT
* -1
10 DEBUG_LEVEL_MEDIUM
= DEBUG_INCREMENT
* 0
11 DEBUG_LEVEL_HIGH
= DEBUG_INCREMENT
* 1
12 DEBUG_LEVEL
= DEBUG_LEVEL_MEDIUM
14 __stackTraceEnabled
= True
16 def stackTraceEnabled(value
):
17 global __stackTraceEnabled
18 __stackTraceEnabled
= value
20 def setDebugLevel(level
):
24 def isBoundMethod(stackFrame
):
25 """Checks to see if the method that is running in the specified stackFrame is
27 Returns a 2-tuple containing if it is a bound method, and the object that it is
28 bound to if it is bound."""
32 if stackFrame
.f_code
.co_argcount
< 1:
34 firstVarName
= stackFrame
.f_code
.co_varnames
[0]
35 firstVar
= stackFrame
.f_locals
[firstVarName
]
36 if not hasattr(firstVar
, stackFrame
.f_code
.co_name
):
38 if not hasattr(getattr(firstVar
, stackFrame
.f_code
.co_name
), 'func_code'):
40 if getattr(getattr(firstVar
, stackFrame
.f_code
.co_name
), 'func_code') == stackFrame
.f_code
:
41 return (True, firstVar
)
45 def createStackTrace(stackList
):
46 if not __stackTraceEnabled
:
49 for stackItem
in stackList
:
51 bm
= isBoundMethod(stackItem
[0]) # stackframe
53 stackItemRepr
= '%s.' % bm
[1].__class
__.__name
__
54 stackItemRepr
+= stackItem
[3] # Function Name
55 del bm
# Help remove circular dependencies (reduces memory useage)
56 strStackList
.append(stackItemRepr
)
58 return '=>'.join(strStackList
)
60 def debug(message
, level
=DEBUG_LEVEL_MEDIUM
, indent_level
= None):
61 if level
>= DEBUG_LEVEL
:
62 stack
= inspect
.stack()[1:-1] # Ignore this method
64 if indent_level
== None:
65 indent_level
= len(stack
)
66 for line
in message
.split('\n'):
67 print >>sys
.stderr
, '%s %s [%s]' %('>' * indent_level
, line
, createStackTrace(stack
))
69 def tracebackHook(etype
, evalue
, etb
):
70 print cgitb
.text( (etype
, evalue
, etb
), context
= 5)