|
|
|
|
|
|
| |
|
<?
/*******************************************************************************
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);
if($this->debug>=1)
{
$this->logfile=$logfile_path;
set_error_handler(array($this,"ErrorHandler"));
}
else
{
restore_error_handler();
}
}
/*==============================================================================
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);
}
}
?> | | |
|
| [PHP5] PHP Debugger and Helper Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML | | | Example of function to send out email if error occurs Categories : PHP, Email, Debugging, Errors and Logging | | | DB Connection Function with error handling and email failure notices Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | A time measuring and performance benchmarking class Categories : PHP, PHP Classes, Testing, Debugging, Date Time | | | Building a basic error handler with custom error types Categories : PHP, PHP Classes, Errors and Logging | | | Filter - A simple class that lets you use multiple functions to create custom filters. Categories : PHP, PHP Classes, Strings | | | DbObject - A PHP wrapper for working with various databases Categories : Databases, PHP, PHP Classes | | | The Ajax Tree view class fetches data from a db for the requested parent category id. The data is then stored in an array and converted into JSON (Javascript Object Notation) format. This format is then used by JavaScript for populating tree view. Categories : PHP, PHP Classes, Java Script, AJAX, Databases | | | Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs | | | Browser Detecor Class Categories : PHP Classes, PHP, HTML | | | Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP, Filesystem, PHP Classes | | | [PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more. Categories : PHP, PHP Classes, Databases, MS SQL Server, MySQL | | | PHP Zip Utility Categories : PHP, PHP Classes, Compression | | | Customizable Calendar Class Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar | |
|
|