Welcome, guest | Sign In | My Account | Store | Cart

Can't use a stepping debugger to diagnose and fix your programs? Use these functions to log state and execution flow. Sample use provided.

Python, 65 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import types, string, sys
from traceback import *

traceOutput = sys.stdout
watchOutput = sys.stdout
rawOutput = sys.stdout

""" Should print out something like:
File "trace.py", line 57, in __testTrace
  secretOfUniverse <int> = 42
"""
def watch ( variableName ):
    if __debug__:
        stack = extract_stack ( )[-2:][0]
        actualCall = stack[3]
        if ( actualCall is None ):
            actualCall = "watch ( [unknown] )"
        left = string.find ( actualCall, '(' )
        right = string.rfind ( actualCall, ')' )
        paramDict = { }
        paramDict["varName"]    = string.strip ( actualCall[left+1:right] )  # everything between '(' and ')'
        paramDict["varType"]    = str ( type ( variableName ) )[7:-2]
        paramDict["value"]      = repr ( variableName )
        paramDict["methodName"] = stack[2]
        paramDict["lineNumber"] = stack[1]
        paramDict["fileName"]   = stack[0]
        outStr = 'File "%(fileName)s", line %(lineNumber)d, in %(methodName)s\n  %(varName)s <%(varType)s> = %(value)s\n\n'
        watchOutput.write ( outStr % paramDict )


""" Should print out something like:
File "trace.py", line 64, in ?
  This line was executed!
"""
def trace ( text ):
    if __debug__:
        stack = extract_stack ( )[-2:][0]
        paramDict = { }
        paramDict["methodName"] = stack[2]
        paramDict["lineNumber"] = stack[1]
        paramDict["fileName"]   = stack[0]
        paramDict["text"]       = text
        outStr = 'File "%(fileName)s", line %(lineNumber)d, in %(methodName)s\n  %(text)s\n\n'
        traceOutput.write ( outStr % paramDict )


""" Should print out something like:
   Just some raw text
"""
def raw ( text ):
    if __debug__:
        rawOutput.write ( text )


def __testTrace ( ):
    secretOfUniverse = 42
    watch ( secretOfUniverse )	

if __name__ == "__main__":
    a = "something else"
    watch ( a )
    __testTrace ( )

    trace ( "This line was executed!" )
    raw ( "Just some raw text.." )

A few functions that allow you to output the value of an expression, a variable or a function call with scope information, as well as trace statements and general comments. Useful for debugging without a stepping debugger. Override 'traceOutput', 'watchOutput' or 'rawOutput' to redirect the output and even turn it off completely when releasing programs. (ie: when __debug__ is false)

This code intended to replace (or compete against) a submission based on a previous version of this with a clearer and more programmer-friendly set of functions. By default, the output looks very much like the traceback info printed out by Python 1.5.2, however you can roll out your own output by simply modifying 'outStr'.

I can see this as useful for debugging CGI programs, for example, and instead of sprinkling a bunch of "print" statements that must be cleaned up (and didn't use to be redirectable)..