Turn the dbg logger into a Logger class

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@19762 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2003-03-24 19:23:49 +00:00
parent 0bbcffb2f5
commit 4cc1012b97

View File

@@ -61,6 +61,8 @@ useful debugging output in a program. Consider the following
code example: code example:
>>> d = {1:'a', 2:'dictionary', 3:'of', 4:'words'} >>> d = {1:'a', 2:'dictionary', 3:'of', 4:'words'}
>>> logger = dbg.Logger()
>>> dbg = logger.dbg
>>> dbg(enable=1) >>> dbg(enable=1)
>>> def foo(d): >>> def foo(d):
dbg('foo', indent=1) dbg('foo', indent=1)
@@ -91,75 +93,105 @@ foo
""" """
_indent = 0 # current number of indentations
_dbg = 0 # enable/disable flag class Logger:
_suspend = 0 # allows code to "suspend/resume" potential dbg output def __init__(self, name=None):
_wxLog = 0 # use wxLogMessage for debug output import sys
self.name = name
self._indent = 0 # current number of indentations
self._dbg = 0 # enable/disable flag
self._suspend = 0 # allows code to "suspend/resume" potential dbg output
self._wxLog = 0 # use wxLogMessage for debug output
self._outstream = sys.stdout # default output stream
self._outstream_stack = [] # for restoration of streams as necessary
import sys def IsEnabled():
_outstream = sys.stdout # default output stream return self._dbg
_outstream_stack = [] # for restoration of streams as necessary
del sys def IsSuspended():
return _suspend
def dbgEnabled(): def log(self, *args, **kwargs):
return _dbg """
This function provides a useful framework for generating
optional debugging output that can be displayed at an
arbitrary level of indentation.
"""
if not self._dbg and not 'enable' in kwargs.keys():
return
def dbgSuspended(): if self._dbg and len(args) and not self._suspend:
return _suspend # (emulate print functionality)
strs = [str(arg) for arg in args]
output = ' '.join(strs)
if self.name: output = self.name+': ' + output
output = ' ' * 3 * self._indent + output
def dbg(*args, **kwargs): if self._wxLog:
""" from wxPython.wx import wxLogMessage # (if not already imported)
This function provides a useful framework for generating wxLogMessage(output)
optional debugging output that can be displayed at an else:
arbitrary level of indentation. self._outstream.write(output + '\n')
""" self._outstream.flush()
global _indent, _dbg, _suspend, _wxLog, _outstream, _outstream_stack # else do nothing
if not _dbg and not 'enable' in kwargs.keys():
return
if _dbg and len(args) and not _suspend: # post process args:
# (emulate print functionality) for kwarg, value in kwargs.items():
strs = [str(arg) for arg in args] if kwarg == 'indent':
output = ' ' * 3 * _indent + ' '.join(strs) self.SetIndent(value)
elif kwarg == 'enable':
self.SetEnabled(value)
elif kwarg == 'suspend':
self.SetSuspend(value)
elif kwarg == 'wxlog':
self.SetWxLog(value)
elif kwarg == 'stream':
self.SetStream(value)
if _wxLog: # aliases for the log function
from wxPython.wx import wxLogMessage # (if not already imported) dbg = log # backwards compatible
wxLogMessage(output) msg = log #
else: __call__ = log # this one lets you 'call' the instance directly
_outstream.write(output + '\n')
_outstream.flush()
# else do nothing
# post process args:
for kwarg, value in kwargs.items():
if kwarg == 'indent' and value:
_indent = _indent + 1
elif kwarg == 'indent' and not value and _indent > 0:
_indent = _indent - 1
if kwarg == 'enable' and value: def SetEnabled(self, value):
old_dbg = _dbg if value:
_dbg = 1 old_dbg = self._dbg
self._dbg = 1
if not old_dbg: if not old_dbg:
dbg('dbg enabled') self.dbg('dbg enabled')
elif kwarg == 'enable' and not value: else:
if _dbg: if self._dbg:
dbg('dbg disabled') self.dbg('dbg disabled')
_dbg = 0 self._dbg = 0
if kwarg == 'suspend':
_suspend = value
if kwarg == 'wxlog': def SetSuspend(self, value):
_wxLog = value if value:
self._suspend += 1
elif self._suspend > 0:
self._suspend -= 1
if kwarg == 'stream' and value:
_outstream_stack.append( _outstream ) def SetIndent(self, value):
_outstream = value if value:
elif kwarg == 'stream' and value is None and len(_outstream_stack) > 0: self._indent += 1
_outstream = _outstream_stack.pop(-1) elif self._indent > 0:
self._indent -= 1
def SetWxLog(self, value):
self._wxLog = value
def SetStream(self, value):
if value:
self._outstream_stack.append( self._outstream )
self._outstream = value
elif value is None and len(self._outstream_stack) > 0:
self._outstream = self._outstream_stack.pop(-1)
#------------------------------------------------------------ #------------------------------------------------------------
@@ -167,7 +199,10 @@ def dbg(*args, **kwargs):
if __name__ == "__main__": if __name__ == "__main__":
from wxPython.wx import * from wxPython.wx import *
wxLog_SetActiveTarget( wxLogStderr() ) wxLog_SetActiveTarget( wxLogStderr() )
logger = Logger('module')
dbg = logger.dbg
dbg(enable=1) dbg(enable=1)
logger('test __call__ interface')
dbg('testing wxLog output to stderr:', wxlog=1, indent=1) dbg('testing wxLog output to stderr:', wxlog=1, indent=1)
dbg('1,2,3...') dbg('1,2,3...')
dbg('testing wxLogNull:') dbg('testing wxLogNull:')
@@ -177,9 +212,25 @@ if __name__ == "__main__":
dbg('(resuming to wxLogStdErr)', '7,8,9...', indent=0) dbg('(resuming to wxLogStdErr)', '7,8,9...', indent=0)
dbg('disabling wxLog output, switching to stderr:') dbg('disabling wxLog output, switching to stderr:')
dbg(wxlog=0, stream=sys.stderr) dbg(wxlog=0, stream=sys.stderr)
dbg(_outstream, 'switching back to stdout:') dbg(logger._outstream, 'switching back to stdout:')
dbg(stream=None) dbg(stream=None)
dbg(_outstream ) dbg(logger._outstream )
def foo(str):
dbg('foo:', indent=1)
dbg(str, indent=0)
foo('testing dbg inside function')
class bar(Logger):
def __init__(self, name):
Logger.__init__(self, name)
def enable(self, value):
self.dbg(enable=value)
def foo(self, str):
self.dbg('foo:', indent=1)
self.dbg(str, indent=0)
f = bar('class mixin')
f.foo("shouldn't print")
f.enable(1)
f.foo("should print")
dbg('test completed.', enable=0) dbg('test completed.', enable=0)
dbg('(double-checking ;-)') dbg('(double-checking ;-)')