PHP Classes

File: debug_sample.php

Recommend this page to a friend!
  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: 21 years ago
Size: 3,770 bytes
 

Contents

Class file image Download
<?php

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
$debug = new DEBUG(5, DEBUG_TEXT|DEBUG_HTML|DEBUG_JS|DEBUG_FILE);

//----- 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"
$debug->SetLogFile("myerror.log");

//===== TESTING SCRIPT START ===================================================
echo "<STYLE>HR {text-align: left}</style>";
//----- STANDARD OPERATION
// I will see this message only if I set the debug level to 9
// This will ignore from level 6 (inclusive) onwards
$debug->SetLevel(5);
echo
"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);
}
//----- TURN OFF EVERYTHING
echo "<HR>The debug message is now off";
$debug->SetDebugOn(FALSE);
$debug->Output("level ON/OFF", 0);
echo
"<HR>The debug message is now on";
$debug->SetDebugOn(TRUE);
$debug->Output("level ON/OFF", 0);
//----- INTERCEPT STANDARD SYSTEM MESSAGE
// 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";
echo
"<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;
   
$debug->ErrorHandler($errno, $errstr, $errfile, $errline);
}
//--- I want to capture only these errors:
define("FATAL",E_USER_ERROR);
define("ERROR",E_USER_WARNING);
define("WARNING",E_USER_NOTICE);
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)",
       
WARNING);
//---- Program error
echo "Program eror in the next line<BR>";
syassafda;


?>