Location: PHPKode > projects > Mozajik > mozajik-release/system/app/model/mozajikerror.model.php
<?
/**
 * MozajikError is a model which stores php errors and backtraces for later analysis.
 *
 * @package Model
 * @subpackage BuiltinModels
 **/


define('MAX_ERRORS_TO_LOG', 3);

class MozajikError extends zajModel {
	static $number_of_errors = 0;
	
	/**
	 * Model definition
	 */
	static function __model(){
		// define custom database fields
			$f->errortext = zajDb::text();
			$f->errorlevel = zajDb::select(array('notice', 'warning', 'error'));

			$f->func = zajDb::text();
			$f->file = zajDb::textarea();
			$f->line = zajDb::integer();
			$f->class = zajDb::text();

			$f->backtrace = zajDb::textarea();

		// do not modify the line below!
			$f = parent::__model(__CLASS__, $f); return $f;
	}

	/**
	 * Construction and required methods
	 */
	public function __construct($id = ""){ parent::__construct($id, __CLASS__); return true; }
	public static function __callStatic($name, $arguments){ array_unshift($arguments, __CLASS__); return call_user_func_array(array('parent', $name), $arguments); }

	/**
	 * Static methods
	 */
	public static function log($errortext, $errorlevel){
		// generate a backtrace
			$backtrace = debug_backtrace(false);
		// increment number of errors
			MozajikError::$number_of_errors++;
			if(MozajikError::$number_of_errors > MAX_ERRORS_TO_LOG) return false;
		// uhoh, error in myself!
			if($backtrace[1]['class'] == 'MozajikError') exit('Could not log error: '.$errortext);
			
		// create the object
			$errorobj = MozajikError::create();
		// set errors
			$errorobj->set('errorlevel', $errorlevel);
			$errorobj->set('errortext', $errortext);
			
			// set first level backtrace
			if(!empty($backtrace[2])){
				$errorobj->set('func', $backtrace[2]['function']);
				$errorobj->set('file', $backtrace[1]['file']);
				$errorobj->set('line', $backtrace[1]['line']);
				if(!empty($backtrace[2]['class'])) $errorobj->set('class', $backtrace[2]['class']);
			}
			$backtrace = array_slice($backtrace, 1);
			
			
			// process backtrace
				foreach($backtrace as $key=>$element){
					// if call user function
						if($element['function'] == 'call_user_func_array') $element['args'] = $element['args'][0];
					//remove objects from argument list
						if(is_array($element['args'])){
							foreach($element['args'] as $argkey=>$arg){
								if(is_object($arg)) $backtrace[$key]['args'][$argkey] = '[Object] '.get_class($arg);
							}
						}
				}
			// now serialize and set full backtrace
				$errorobj->set('backtrace', serialize($backtrace));
				//$errorobj->set('backtrace', print_r($backtrace, true));
		// save it to a row
			$errorobj->save();
		return true;
	}
}
Return current item: Mozajik