Error recorder class

Here’s a little tool I use to record and report errors.
Actually written it as an exercise for orient-objected coding when I was trying to discover the differences between PERL and PHP, but it was useful in several places. So I still drag it from here to there and use it where needed. :stuck_out_tongue:

The class records errors and reports them when you choose to. These are of course not runtime errors cos they break the scripts, these are errors you want to report back to the user ( or the developer ).

An example of the error template is given below the PHP code. ( NOTE: this is only an example )

Apologies if some comment is in dutch, it’s my native tongue.
[php]

<?php /* Global Errors recorder */ /* Ojoshiro, */ /* A tool-class for the recording and uniformly reporting of * errors. * * Syntax: * include_once( 'Error.php' ); * Error::setTemplate( 'Errorframe.php' ); * Error::record( "The system is active and ready to receive input" ); * Error::renderErrors(); * * The recorder also records a complete stacktrace back to level 0. * * The template */ class Error { protected static $errorstack = array(); protected static $templatepath = ''; /* Method for assigning the template path */ public static function setTemplate( $path ) { if ( file_exists( $path ) ) { self::$templatepath = $path; } else { die( "Cannot set template path to [$path], File doesn't exist in". __FILE__ ); } } /* Returns the template path */ public static function getTemplate() { if ( file_exists( self::$templatepath ) ) { return self::$templatepath; } else { die( "No template path set in ". __FILE__ ); } return false; } /* Records a message and puts it on top of the error stack * Also does a debug_backtrace and stores the entire result * of this function in the recorded error. */ // NOTE: Make sure the right number of elements is shifted off the stacktrace. public static function record( $message, $level = 0 ) { $stacktrace = debug_backtrace(); //array_shift( $stacktrace ); $error = array(); $error['message'] = $message; $error['stacktrace'] = $stacktrace; // So this is the regular debug_backtrace structure $error['level'] = $level; array_push( self::$errorstack, $error ); if ( $level == 2 ) { exit( "A fatal error occurred" ); } } /* Empties the error stack */ public static function flush() { self::$errorstack = array(); } /* Returns in boolean whether there are errors on the stack */ public static function hasErrors() { if ( self::getErrorCount() > 0 ) { return true; } return false; } /* Count the number of errors on the stack */ public static function getErrorCount() { return count( self::$errorstack ); } /* return the entire errors stack */ public static function getErrors() { if ( self::hasErrors() ) { $return = self::$errorstack; return $return; } return false; } /* return the most recent error */ public static function getLastError() { if ( self::hasErrors() ) { $return = end( self::$errorstack ); reset( self::$errorstack ); return $return; } return false; } /* return the error at the given index */ public static function getError( $index ) { if ( self::hasErrors() ) { if ( isset( self::$errorstack[$index] ) ) { return self::$errorstack[$index]; } } return false; } /* Render the error at the given index */ public static function renderError( $index ) { if ( self::hasErrors() ) { $error = self::getError( $index ); $message = $error['message']; $level = $error['level']; $stacktrace = $error['stacktrace']; include( self::getTemplate() ); } } /* Render the last error */ public static function renderLastError() { if ( self::hasErrors() ) { $error = self::getLastError(); $message = $error['message']; $level = $error['level']; $stacktrace = $error['stacktrace']; include( self::getTemplate() ); } } /* Render all errors on the stack */ public static function renderErrors() { if ( self::hasErrors() ) { for( $i=0; $i

[/php]

An error template that works:
[php]

<?php echo $message. "
\n"; ?> <?php array_shift( $stacktrace ); foreach ( $stacktrace as $step ) { $call = "Function"; if ( $step['type'] == '::' ) { $call = 'Class method'; } elseif ( $step['type'] == '->' ) { $call = 'Object method'; } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; } ?>
". $step['function']. "". $step['file']. " at line ". $step['line']. "class ". $step['class']. " object ". get_class( $step['object'] ). "". $call. ""; foreach ( $step['args'] as $value ) { echo "$value
"; } echo "
[/php]

In a very short version this is what you need to do to use the class, use the template and record and report an error.
The recorder records multiple errors and can report them one-by-one or all at once.
[php]
include_once( ‘Error.php’ ); // Include the file with the class in it
Error::setTemplate( ‘Errorframe.php’ ); // Set the errortemplate you want to use

Error::record( “The system is active and ready to receive input” ); // Record an error

Error::renderErrors(); // Report all errors recorded till now
Error::flush(); // Flush the errorstack
[/php]

I hope it helps someone. ;D
O.

PS.: Do point out flaws and features.

Sponsor our Newsletter | Privacy Policy | Terms of Service