[Tutor] questions about debugging program

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Aug 17 21:08:27 EDT 2003



On Sun, 17 Aug 2003, gerard henry wrote:

> I've some experience in other langages as C, C++, so i want to replace
> in actual code:
> print statement
> by something like:
> print "%s %d" __FILE__ __LINE__ statement
> as we used to do in C/C++, so i can go to the right line in the right
> file easier.

Hi Gerard,

Welcome aboard!  Yes, there is a way of emulating __LINE__:

    http://www.faqts.com/knowledge_base/view.phtml/aid/4456/fid/538

But that being said, you may want to take a close look into Python's
'logging' package:

    http://www.python.org/doc/lib/module-logging.html

This logging package was recently introduced into Python 2.3.  It is very
good because it supports several options for each log message, including a
'lineno' option.  We can do this by creating a Formatter object:

    formatter = logging.Formatter('%(module)s %(lineno)d: %(message)s')

and use the setFormatter() method of our logging handler object.  This
approach is more robust than doing the line-number hack from the FAQTS
link above.


'logging' was just recently added into the standard library, so you may
not have it in your installation.  If you need to stick with an older
version of Python, you can look into Vinay Sajip's 'logging' module:

    http://www.red-dove.com/python_logging.html

which I think has a similar API to the Standard Library's logger.  But if
you can, upgrade to Python 2.3: it has a lot of nice features.


If you have more questions, please feel free to ask on Tutor; we'll be
happy to help!




More information about the Tutor mailing list