Best way to work in debug code?

Darrell news at dorb.com
Wed Jan 19 21:04:15 EST 2000


This debugging solution may just be over kill, so be kind. But working with
6+ developers all in one application, debugging can be tricky. We have
plugin developers also that want more trace info than a typical user. Like
turning on or off debugging for specific areas with out editing the whole
system.

I use debug levels in each module or subsystem.
They are set from the command line with something like
-d moduleName level
or
-d subSystemName level

The debug module that parses command line debug switches, adds an alias of
it's self in the sys.modules list. This debug module must be imported early
in the main so the debug alias gets setup before it's needed.

When a module is imported it does the following:
# Some quick last min changes so this is untested code.
#### module to debug
import sys
    # The use of __debugMain__  instead of the module name is just a level
of
    # indirection. Like a symbolic link.
sys.modules['__debugMain__'].setDebug(__name__)

    # debugLevel was set into this module by the setDebug call
if debugLevel >=1:
    # Do some debug stuff here

#### This is in __debugMain__ which was imported as something like debug.py

def setDebug(name):
    """
    Called from modules as they are imported to setup their debugLevel.
    name:: Name of the module to work with
    It's debugLevel is determined and written back to it.
    """
    global debugDic
    if debugDic.has_key(name):
            # Set the value of debug in the module.
        sys.modules[name].debugLevel= debugDic[name]
    else:
        sys.modules[name].debugLevel= 0

--
--Darrell






More information about the Python-list mailing list