5. How to compile your code in debug mode

One way to debug your code is to add print statements in your code. The output will show up in the window where the CherryPy server runs. However, this method may not always work (for instance, if the server is running as a daemon on a production machine and you don't have access to its standard output). Plus switching from debug mode to non-debug mode is not easy (you have to comment out all the print statements ...).

Another way is to save this output in a log file. You can redirect the standard output from the server, or you can write in the log file directly from your code. CherryPy provides one special function, one CGTL tag and one CHTL tag to do that.

The special function is called debug and the tag is called py-debug. They are used like this:

debug('i=%s'%i)
<py-debug="'i=%s'%i">
<div py-debug="'i=%s'%i"></div>
All these statements have the same effect: add a line in the log file.

If the name of the CherryPy server is RootServer.py, the name of the log file is RootServer.log.

Debugging can be easily turned on or off at compile time, using the -D option of the compiler. If -D is specified, then debugging is turned on:

python ../cherrypy.py -D Root.cpy
If -D is omitted, debugging is turned off.

CherryPy uses a special global variable called _debug that can be accessed from anywhere in your code. The variable is 1 if debugging is on, 0 otherwise.

See About this document... for information on suggesting changes.