Browse Source

add support for ring buffers for debugging, this lets you more easily

keep state on what has happened recently w/o having to spam the log
as much...  we default to 50 entries...

add comment to make sure you update the global line when adding new
functions otherwise when you try to overwrite them, it won't...

[git-p4: depot-paths = "//depot/": change = 844]
v0.3
John-Mark Gurney 18 years ago
parent
commit
a3db9c0f5e
1 changed files with 44 additions and 13 deletions
  1. +44
    -13
      debug.py

+ 44
- 13
debug.py View File

@@ -2,27 +2,44 @@


from twisted.internet import reactor from twisted.internet import reactor


__all__ = [ 'appendnamespace', 'insertnamespace', ]
# Make sure to update the global line when adding a new function
__all__ = [ 'appendnamespace', 'insertnamespace', 'insertringbuf' ]

DEFAULTRINGSIZE = 50


appendnamespace = lambda k, v: [] appendnamespace = lambda k, v: []
insertnamespace = lambda k, v: None insertnamespace = lambda k, v: None
insertringbuf = lambda k, l = DEFAULTRINGSIZE: None

# This code from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68429
class RingBuffer:
def __init__(self,size_max):
self.max = size_max
self.data = []
def append(self,x):
"""append an element at the end of the buffer"""
self.data.append(x)
if len(self.data) == self.max:
self.cur=0
self.__class__ = RingBufferFull
def get(self):
""" return a list of elements from the oldest to the newest"""
return self.data

class RingBufferFull:
def __init__(self,n):
raise "you should use RingBuffer"
def append(self,x):
self.data[self.cur]=x
self.cur=(self.cur+1) % self.max
def get(self):
return self.data[self.cur:]+self.data[:self.cur]


def doDebugging(opt): def doDebugging(opt):
if not opt: if not opt:
return return


from twisted.manhole import telnet

class Debug(telnet.Shell):
def welcomeMessage(self):
data = [ '', 'PyMedS Debugging Console', '', '' ]
return '\r\n'.join(data)
sf = telnet.ShellFactory()
sf.protocol = Debug
reactor.listenTCP(56283, sf)

global insertnamespace, appendnamespace
global insertnamespace, appendnamespace, insertringbuf


def insertnamespace(k, v): def insertnamespace(k, v):
assert isinstance(k, basestring) assert isinstance(k, basestring)
@@ -33,3 +50,17 @@ def doDebugging(opt):
sf.namespace[k].append(v) sf.namespace[k].append(v)
except KeyError: except KeyError:
sf.namespace[k] = [ v ] sf.namespace[k] = [ v ]

def insertringbuf(k, l = DEFAULTRINGSIZE):
insertnamespace(k, RingBuffer(l))

from twisted.manhole import telnet

class Debug(telnet.Shell):
def welcomeMessage(self):
data = [ '', 'PyMedS Debugging Console', '', '' ]
return '\r\n'.join(data)
sf = telnet.ShellFactory()
sf.protocol = Debug
reactor.listenTCP(56283, sf)

Loading…
Cancel
Save