<?
/*******************************************************************************
Program Name : Error.php
Purpose : This file contains the error class,which manages errors.
It handles erros in customized way
Creation Date : 04/22/2005 02:05PM
********************************************************************************/
class Error
{
var $logfile;
var $debug;
/*==============================================================================
Constructor
Arguments :- $debug_yesno. If 1 then errors will be logged,else displayed
==============================================================================*/
function Error($debug_yesno=0,$logfile_path)
{
$this->debug=$debug_yesno;
$this->printdebug=1;
//Report only user generated error messages
error_reporting(E_USER_ERROR|E_USER_WARNING|E_USER_NOTICE);
/*==============================================================================
Following function logs an error message
==============================================================================*/
function ErrorHandler($errno,$errstr,$errfile,$errline)
{
$errtime=date("H:i:s - d/m/y");
$error_message="An error occured on ".$errtime."\n";
$error_message .="Details are as follows\n";
$error_message .="Error Number : $errno\n";
$error_message .=$errstr."\nOccured in ".$errfile."\n";
$error_message .="On Line $errline \n\n";
$error_message .=str_repeat("-",50);
$error_message .="\n";
$fp=fopen($this->logfile,"r+");
fwrite($fp,$error_message);
fclose($fp);
}
/*==============================================================================
Following function prints debug messages on the screen
==============================================================================*/
function PrintDebug($message,$yesno=0)
{
if($yesno>0)
{
$message="<font face=\"verdana\" size=\"2\" color=\"red\">Debug : ".$message."</font><br>";
print $message;
}
}
}
?>
How to use this class in the script.
Initialize this class as $error=new Error(1,"/err.log"). Constructor argument
value "1" will set the custom error handler and redirect all errors to a log
file specified by the second argument "/err.log".In your script,whereever you
expect an error to happen, use PHP's builtin function - trigger_error("Your
error message") to trigger error.You won't see any other error or warning except
parse errors with this class.
In PHP scripts we often need to "echo" certain variables and then delete those
echo statements for debugging pupose. Instead of that we can use
$error->PrintDebug("debug message",$yesno). This will provide a neat error
message and also will eliminate the need of deleting those messages. To print
selectively, call function with second argument with value 1. Like following
$error->PrintDebug("This is the message",1);
There are a lot of enhacements to be done for this class.
<?
//If initialized with 1 errors will be logged. else they will be displayed on the screen
$error=new Error(1,"/err.log");
$conn=mysql_connect($host,$username,$password);
if(!$conn)
{
trigger_error("Couldn't Connect To MySQL Because Of Following Error :".MySQL_Error());
die();
}
else
{
$error->PrintDebug("MySQL Connected",1);
$dblink=mysql_select_db($dbname);
if(!$dbname)
{
trigger_error("Couldn't Select Database For Following Error".MySQL_Error());
die();
}
else
{
$error->PrintDebug("Database Selected",1);
}
}
?>