PHP Classes

File: debug_sample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Mark Quah  >  class_debug  >  debug_sample.php  >  Download  
File: debug_sample.php
Role: Example script
Content type: text/plain
Description: Sample test script + Documentation
Class: class_debug
Enhance PHP error message handler.
Author: By
Last change: added msg text format.
Date: 12 years ago
Size: 3,770 bytes


Class file image Download

include_once "./class_debug.php";

//===== INITIATE DEBUG CLASS AND SET OPTION ====================================
//----- INitialize class
// debug-level
// The first number is the debug detail you want to get, normally 0-9
// Then every debug message a level. If the message level
// is less than debug level, then it will not be displayed. Usefull if you
// want the debug code remained in the programe, then you can control
// the amount of debug message by changing level from 0 (all message) to
// other level. Note that the lower the lever, the more important the
// message, i.e. level 0 is the most important.
// output mode
// you can choose one or more error output method by cascading with the
// "|" sign. eg DEBUG_TEXT|DEBUG_FILE will print message out as text
// as well as send to a logfile

//----- FORMAT
// Added some formating to the base message
$debug->SetFormat("DEBUG_SAMPLE: %s");

//----- DEBUG_TEXT
// Text message can have a prefix and postfix to the message
$debug->SetText("<H5>ERR [ ", "] </H5><BR>");

//----- DEBUG_HTML
// error message will be using this style
// note that %s will be replaced by the error message
// In this case the message will be enclosed by <H5 class='error'>
// and </H5>. Note that the %s can only be used once.
// The difference between HTML and TEXT is that HTML convert all
// new-line to <BR> field.
echo "<STYLE>.error {border: groove; background: lightgreen}</STYLE>";
$debug->SetHTML("<H5 class='error'>%s</H5>");

//----- DEBUG_JS
// Generate a javascript prompt. if you can think of what to add to
// this feature please inform me.

//----- DEBUG_FILE
// This will print the error message to the log file, if this is
// not specify, by default the log name is "debug.log"

//===== TESTING SCRIPT START ===================================================
echo "<STYLE>HR {text-align: left}</style>";
// I will see this message only if I set the debug level to 9
// This will ignore from level 6 (inclusive) onwards
"Debug Level is set to 5<BR>";
for (
$i = 0; $i <= 9; $i ++)
{ echo
"<BR><HR WIDTH=50%>Output a debug message: level $i<BR>";
$debug->Output("level $i", $i);
echo "<HR>The debug message is now off";
$debug->Output("level ON/OFF", 0);
"<HR>The debug message is now on";
$debug->Output("level ON/OFF", 0);
// Note that all the ERROR is set at level 1
// All the WARNING is set at level 2
// and all the NOTICE is set at level 3.
// That mean debug level has to set at least at 3 to see all trapped message
echo "<HR>Intercept system error message";
"<BR>Please refer to PHP help on Error handling and Logging Functions<BR>";
//--- Set up a wrapper function
function MyErrorHandler($errno, $errstr, $errfile, $errline)
{ global
$debug->ErrorHandler($errno, $errstr, $errfile, $errline);
//--- I want to capture only these errors:
error_reporting (FATAL | ERROR | WARNING);
$old_error_handler = set_error_handler("myErrorHandler");
//--- Trigger an error
echo "Manually trigger a level<BR>";
trigger_error("Value at position $i is not a number, using 0 (zero)",
//---- Program error
echo "Program eror in the next line<BR>";