Debug Help
I was in need of this for a program I am working on, and thought I would post it here for others to use to help debug their scripts. I welcome any suggestions/improvements on this method.
This places a file called DEBUG.TXT in the same directory as the script you are using it on. This will be able to viewed by anyone by browsing to the file, however during creation I have the scripts in a password protected directory, so I don't have to worry about getting viewed.
At the top of the script, place the following code:
<?php
// Set the following variable to TRUE to display flow statements to track program flow
$debug = true;
if ($debug)
{
$debugFH = fopen('debug.txt','w');
fwrite($debugFH,\"DEBUG SESSION STARTED: \" . date(\"l dS of F Y h:i:s A\") . \"\n\");
fwrite($debugFH,\" REQUEST_METHOD: \" . $_SERVER['REQUEST_METHOD'] . \"\n\");
fwrite($debugFH,\" QUERY_STRING: \" . $_SERVER['QUERY_STRING'] . \"\n\");
fwrite($debugFH,\" HTTP_REFERER: \" . $_SERVER['HTTP_REFERER'] . \"\n\");
fwrite($debugFH,\" HTTP_USER_AGENT: \" . $_SERVER['HTTP_USER_AGENT'] . \"\n\");
fwrite($debugFH,\" REMOTE_ADDR: \" . $_SERVER['REMOTE_ADDR'] . \"\n\");
fwrite($debugFH,\"==============================================\n\");
}
function debugOut($lineNumber,$comment)
{
global $debugFH;
fwrite($debugFH,\"LINE[\" . str_pad($lineNumber, 5, \"0\", STR_PAD_LEFT) . \"] \" . $comment . \"\n\");
}
?>
Then, wherever you want to track the flow of your program, or have it send out a comment, just add this line:
<?php
if ($debug) debugOut(__LINE__,\"**COMMENT**\");
?>
The only thing you need to change in the above code in the **COMMENT** portion, and can be left blank. This will be printed after the line number in the debug.txt file.
Finally, at the end of the script, you will need the following:
<?php
if ($debug) fclose($debugFH);
?>
Here is a sample of the debug.txt file:
DEBUG SESSION STARTED: Wednesday 11th of February 2004 06:39:26 PM
REQUEST_METHOD: GET
QUERY_STRING: mod=reports.statement&cmd=print
HTTP_REFERER: <a href="http://192.168.1.80/jvs/?mod=sys.main&cmd=view" class="bb-url">http://192.168.1.80/jvs/?mod=sys.main&cmd=view</a>
HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
REMOTE_ADDR: 192.168.1.103
==============================================
LINE[00039]
LINE[00046]
LINE[00046] There was no record found
LINE[00063]
LINE[00067]
In the above sample, the call to the debugger on line 46 looked like this:
<?php
if ($debug) debugOut(__LINE__,\"There was no record found\");
?>
while the rest of them looked like this:
<?php
if ($debug) debugOut(__LINE__,\"\");
?>
In the debugOut function, you could modify it to add additional information such as Function Name, Class Name etc. See http://www.php.net/manual/en/language.constants.predefined.php for more constants you can use. Note however, these have to be passed to the debugOut function in order to properly show them. If I would have put the __LINE__ constant in the debugOut function, they would have all shown line 20 (or whatever line the fwrite() happened to be on).
Now as you are working on a program, you can turn on the debugging when you run into problems, and normally keep it off.
I hope this helps people in debugging large scripts, it really is useful for finding out the flow of the program.
-Greg
Greg K posted this at 22:11 — 13th February 2004.
He has: 2,145 posts
Joined: Nov 2003
Just an update the above code. Once I tried calling
<?php
if ($debug) debugOut(__LINE__,\"**COMMENT**\");
?>
To fix this, use the following instead:
<?php
if ($GLOBALS[\"debug\"]) debugOut(__LINE__,\"**COMMENT**\");
?>
-Greg
Mark Hensler posted this at 05:18 — 14th February 2004.
He has: 4,048 posts
Joined: Aug 2000
Should add __FILE__ as well. I don't know of any projects that I have which do not include other files.
Also note that the file is opened at the begining of the script, and left open untill the end. This should only be used on a developement site, as it may present problems in a production environment (possible file locking).
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.