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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
#
# $Id: trace.py,v 1.3 2001/11/07 00:00:07 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""A debugging trace module.
This debugging trace module makes it easier to follow nested calls
in output by varying the indention level for log messages. The
caller can simply trace 'into()' a new level when control passes
into a function call, 'write()' debug messages at appropriate spots
in the function, then call 'outof()' when returning from the
function.
The debug level is set via the environment variable 'TRACE_LEVEL'.
Level '0' or no value specified results in no output. Positive
integer values are used to control the verbosity of the output with
higher numbers resulting in more output messages.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: trace.py,v $',
'rcs_id' : '$Id: trace.py,v 1.3 2001/11/07 00:00:07 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'UNSPECIFIED',
'created' : 'Mon, 29-Oct-2001 09:29:22 EST',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.3 $',
'date' : '$Date: 2001/11/07 00:00:07 $',
}
try:
__version__ = __rcs_info__['version'].split(' ')[1]
except:
__version__ = '0.0'
#
# Import system modules
#
import os
import sys
#
# Import Local modules
#
from happydoclib.StreamFlushTest import StreamFlushTest
#
# Module
#
class DebugTracer:
NO_RETURN_VALUE_SPECIFIED='No return value specified.'
def __init__(self,
outputStream=sys.stdout,
indentBy=' ',
maxOutputLevel=1,
startLevel=0):
self.output = outputStream
self.level = startLevel
self.stack = ()
self.indent_by = indentBy
self.max_output_level = maxOutputLevel
return
def setVerbosity(self, level):
self.max_output_level = level
return
def getIndent(self):
return self.indent_by * self.level
def pushLevel(self, newStackTop):
self.level = self.level + 1
self.stack = ( newStackTop, self.stack )
return
def popLevel(self):
self.level = self.level - 1
if self.stack:
popped, self.stack = self.stack
else:
popped = ()
return popped
def checkOutputLevel(self, outputLevel):
return self.max_output_level >= outputLevel
###
def into(self, className, functionName, outputLevel=1, **params):
"""Enter a new debug trace level.
Parameters
'className' -- Name of the class.
'functionName' -- The name of the function/method.
'outputLevel=1' -- The debug level where this message should be printed.
'**params' -- Parameters sent to the function.
"""
if self.checkOutputLevel(outputLevel):
self.output.write(self.getIndent())
self.output.write('%s::%s (' % (className, functionName))
params = params.items()
params.sort()
for name, value in params:
self.output.write('\n%s\t%s=%s, ' % (self.getIndent(), name, value))
self.output.write(') {\n')
self.pushLevel((className, functionName))
return
def callerParent(self, outputLevel=1):
if self.checkOutputLevel(outputLevel):
self.output.write(self.getIndent())
if not self.stack:
self.output.write('ERROR: trace.callerParent called when no stack present\n')
if len(self.stack) < 2:
parent = 'None'
else:
try:
parent = '%s::%s' % self.stack[1][0]
except:
parent = str(self.stack[1])
#self.output.write('Called by: %s\n' % parent)
self.output.write('Called by: %s\n' % str(self.stack))
return
def write(self, message, indent=1, outputLevel=1, **vars):
if self.checkOutputLevel(outputLevel):
if indent:
self.output.write(self.getIndent())
self.output.write(message)
if vars.items():
vars = vars.items()
vars.sort()
for name, value in vars:
self.output.write('\n%s\t%s=%s' % (self.getIndent(), name, value))
self.output.write('\n')
else:
self.output.write('\n')
return
def writeVar(self, outputLevel=1, **variables):
if self.checkOutputLevel(outputLevel):
variables = variables.items()
variables.sort()
for name, value in variables:
self.write('%s=%s' % (name, value))
return
def outof(self, returnValue=None, outputLevel=1):
"""Exit the current debug trace level.
Parameters
'returnValue' -- Optional argument indicating
the value returned from the function.
"""
if self.checkOutputLevel(outputLevel):
self.popLevel()
self.output.write(self.getIndent())
self.output.write('} %s\n' % str(returnValue))
return
trace=DebugTracer(maxOutputLevel=int(os.environ.get('HAPPYDOC_TRACE', 0)))
into=trace.into
outof=trace.outof
write=trace.outof
####################################################################################
class TraceUnitTest(StreamFlushTest):
def testTrace(self):
from cStringIO import StringIO
buffer = StringIO()
trace = DebugTracer(outputStream=buffer)
trace.callerParent()
trace.into('__main__', 'topLevel', a='a', b='b')
trace.callerParent()
trace.write('hi there')
trace.into('secondary', 'secondLevel', c='C', d='D')
trace.write('inside second level')
trace.callerParent()
trace.outof()
trace.outof('string returned')
expected_value = """ERROR: trace.callerParent called when no stack present
Called by: ()
__main__::topLevel (
a=a,
b=b, ) {
Called by: (('__main__', 'topLevel'), ())
hi there
secondary::secondLevel (
c=C,
d=D, ) {
inside second level
Called by: (('secondary', 'secondLevel'), (('__main__', 'topLevel'), ()))
} None
} string returned
"""
actual_output = buffer.getvalue()
assert actual_output == expected_value, \
'Trace generated unexpected output [%s].' % actual_output
return
|