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
_suspend = 0 # allows code to "suspend/resume" potential dbg output
_wxLog = 0 # use wxLogMessage for debug output
class Logger:
def __init__(self, name=None):
import sys import sys
_outstream = sys.stdout # default output stream self.name = name
_outstream_stack = [] # for restoration of streams as necessary self._indent = 0 # current number of indentations
del sys 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
def dbgEnabled(): def IsEnabled():
return _dbg return self._dbg
def dbgSuspended(): def IsSuspended():
return _suspend return _suspend
def dbg(*args, **kwargs):
def log(self, *args, **kwargs):
""" """
This function provides a useful framework for generating This function provides a useful framework for generating
optional debugging output that can be displayed at an optional debugging output that can be displayed at an
arbitrary level of indentation. arbitrary level of indentation.
""" """
global _indent, _dbg, _suspend, _wxLog, _outstream, _outstream_stack if not self._dbg and not 'enable' in kwargs.keys():
if not _dbg and not 'enable' in kwargs.keys():
return return
if _dbg and len(args) and not _suspend: if self._dbg and len(args) and not self._suspend:
# (emulate print functionality) # (emulate print functionality)
strs = [str(arg) for arg in args] strs = [str(arg) for arg in args]
output = ' ' * 3 * _indent + ' '.join(strs) output = ' '.join(strs)
if self.name: output = self.name+': ' + output
output = ' ' * 3 * self._indent + output
if _wxLog: if self._wxLog:
from wxPython.wx import wxLogMessage # (if not already imported) from wxPython.wx import wxLogMessage # (if not already imported)
wxLogMessage(output) wxLogMessage(output)
else: else:
_outstream.write(output + '\n') self._outstream.write(output + '\n')
_outstream.flush() self._outstream.flush()
# else do nothing # else do nothing
# post process args: # post process args:
for kwarg, value in kwargs.items(): for kwarg, value in kwargs.items():
if kwarg == 'indent' and value: if kwarg == 'indent':
_indent = _indent + 1 self.SetIndent(value)
elif kwarg == 'indent' and not value and _indent > 0: elif kwarg == 'enable':
_indent = _indent - 1 self.SetEnabled(value)
elif kwarg == 'suspend':
self.SetSuspend(value)
elif kwarg == 'wxlog':
self.SetWxLog(value)
elif kwarg == 'stream':
self.SetStream(value)
if kwarg == 'enable' and value: # aliases for the log function
old_dbg = _dbg dbg = log # backwards compatible
_dbg = 1 msg = log #
__call__ = log # this one lets you 'call' the instance directly
def SetEnabled(self, value):
if value:
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 ;-)')