Recipes for trace statements inside python programs?

cassiope fpm at u.washington.edu
Fri Jun 26 11:18:29 EDT 2009


On Jun 25, 1:33 am, Francesco Bochicchio <bieff... at gmail.com> wrote:
> Hi all,
>
> as many - I think - python programmers, I find muself debugging my
> scripts by placing print statements in strategic places rather than
> using the python debugger, and commenting/uncommenting them according
> to myy deugging needs.  After a time, these prints staements start to
> evolving in some ad-hoc half-baked framework ... so I wonder if there
> is somewhere there is a full-baked trace statement support framework
> which I can use. I'm aware of the logging module, but for me it its
> more geared toward  application logging rather than toward trace for
> debugging purpose.
>
> Having googlet and found nothing (or too much but nothing relefìvant),
> I'm now asking The List.
>
> Here is what I have in mind:
>
> Each module, function, class and method should have an attribute, say
> trace_flag, which can be set to true or false value.
>
> there should be a function TRACE which does something like this:
>
> if __debug__ :
> def TRACE(*args):
>      if  trace_enabled(): print "TRACE(%s) : %s " % ( context(), "
> ".join( str(x) for x in args ) )
>
> where trace_enabled() should return the value of the innermost
> trace_flag (checking current function/method then current class (if
> any) then current module) and context() shoud return a string like
> "module.function" or "module.class.method" ).
>
> At this point I could in my test code enable  the trace in the
> function/class that gives me trouble and disable it after I fixed it,
> without having to touch the actual code under test.
>
> I guess it should not be too hard do using python introspection
> modules, but of couse I first would like to know if something like
> this already exists.
>
> I'm  aware that this imposes a performance penalty, but my scripts are
> not operformance-critical. And if I put an if __debug__ switch

What are you trying to achieve that could not be accomplished with
the logging module?

   http://docs.python.org/library/logging.html#module-logging

    -f



More information about the Python-list mailing list