now also log fatal errors

This commit is contained in:
Ulf Gebhardt 2013-12-20 08:52:43 +01:00
parent e0559012ef
commit 4989aae171
8 changed files with 53 additions and 15 deletions

View File

@ -10,7 +10,7 @@ class error_handler_jsonoutput extends \SYSTEM\LOG\error_handler {
try{
echo \SYSTEM\LOG\JsonResult::error($E);
} catch (\Exception $E){} //Error -> Ignore
return true;
return die(); //die is required cuz else some fatals cant be catched properly
}
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace SYSTEM\LOG;
class ErrorException extends \ErrorException {
public $logged = false;
public function __construct($message = "", $code = 1, $severity = 0, $filename = "", $lineno = 0, $previous = NULL){
parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
\SYSTEM\LOG\log::__exception_handler($this,false);
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace SYSTEM\LOG;
class ShutdownException extends \ErrorException {
public $logged = false;
public function __construct($message = "", $code = 1, $severity = 0, $filename = "", $lineno = 0, $previous = NULL){
parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
\SYSTEM\LOG\log::__exception_handler($this,false);
}
}

View File

@ -1,6 +1,9 @@
<?php
namespace SYSTEM\LOG;
//ini_set('error_prepend_string', '{querytime: 0.0, status: false, result: { class: "FatalError",message: "');
//ini_set('error_append_string', '", code: 1, file: "unknown", line: 0, trace: }}');
class log {
const HANDLER_FUNC_MASK = 'MASK';
const HANDLER_FUNC_CALL = 'CALL';
@ -17,7 +20,11 @@ class log {
set_error_handler('\SYSTEM\LOG\log::__error_handler');
set_exception_handler('\SYSTEM\LOG\log::__exception_handler');
register_shutdown_function( "\SYSTEM\LOG\log::__shutdown_handler" );
register_shutdown_function( '\SYSTEM\LOG\log::__shutdown_handler' );
ini_set('error_prepend_string', '<phpfatalerror>');
ini_set('error_append_string', '</phpfatalerror>');
ob_start('\SYSTEM\LOG\log::__fatal_error_handler');
}
private static function call_handlers(\Exception $E, $thrown = true){
@ -27,17 +34,29 @@ class log {
return true;}
}
return false;
}
}
public static function __exception_handler(\Exception $E, $thrown = true){
return self::call_handlers($E, $thrown) && $thrown;}
public static function __error_handler($code, $message, $file, $line, $thrown = true){
return self::call_handlers(new \ErrorException($message, 1, $code, $file, $line) ,$thrown);}
return self::call_handlers(new \SYSTEM\LOG\ErrorException($message, 1, $code, $file, $line) ,$thrown);}
public static function __shutdown_handler($thrown = true) {
public static function __shutdown_handler($thrown = true) {
if( ($error = error_get_last()) !== NULL && !$error['type'] === E_DEPRECATED) { //http://www.dreamincode.net/forums/topic/284506-having-trouble-supressing-magic-quotes-gpc-fatal-errors/
return self::call_handlers(new \ErrorException($error["message"], 1, $error["type"],$error["file"],$error["line"]) ,$thrown);}
return self::call_handlers(new \SYSTEM\LOG\ShutdownException($error["message"], 1, $error["type"],$error["file"],$error["line"]) ,$thrown);}
}
public static function __fatal_error_handler($bufferContent, $thrown = true){
$errors = array();
if ( preg_match('|<phpfatalerror>.*</phpfatalerror>|s', $bufferContent, &$errors) ){
$error = strip_tags($errors[0]);
$error = substr($error,1,strlen($error)-2);
$file = substr($error,strpos($error,' in ')+5,strpos($error,' on ')-5-strpos($error,' in '));
$line = intval(substr($error,strpos($error,' line ')+6));
$error = substr($error,0,strpos($error,' in '));
return \SYSTEM\LOG\JsonResult::error(new \SYSTEM\LOG\ShutdownException($error,1,1,$file,$line));}
return $bufferContent;
}
}

View File

@ -1,2 +0,0 @@
<?php
\SYSTEM\LOG\log::registerHandler('\SYSTEM\LOG\error_handler_dbwriter');

View File

@ -1,2 +0,0 @@
<?php
\SYSTEM\LOG\log::registerHandler('\SYSTEM\LOG\error_handler_jsonoutput');

View File

@ -47,14 +47,14 @@ class autoload {
public static function registerFile($file, $namespace = ''){
if(!is_file($file)){
throw new \Exception('File not found on registerFile for Autoload: '.$file);}
throw new \SYSTEM\LOG\ERROR('File not found on registerFile for Autoload: '.$file);}
self::$files[] = array(self::getClassFromFile($file), $namespace, $file);
}
public static function registerFolder($folder, $namespace = ''){
if(!is_dir($folder)){
throw new \Exception('Folder not found on registerFolder for Autoload: '.$folder);}
throw new \SYSTEM\LOG\ERROR('Folder not found on registerFolder for Autoload: '.$folder);}
self::$folders[] = array($namespace, $folder);
}
@ -62,6 +62,9 @@ class autoload {
public static function autoload($class){
$classns = self::getClassNamespaceFromClass($class);
return self::autoload_($classns[0],$classns[1]);
if(!self::autoload_($classns[0],$classns[1]) || !class_exists($class)){
throw new \SYSTEM\LOG\ERROR("Class not found: ".$class);}
return true;
}
}

View File

@ -42,8 +42,8 @@ class system {
require_once \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_SYSTEMPATHREL).'/log/register_result_shortcut.php';}
public static function register_errorhandler_jsonoutput(){
require_once \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_SYSTEMPATHREL).'/log/register_errorhandler_jsonoutput.php';}
\SYSTEM\LOG\log::registerHandler('\SYSTEM\LOG\error_handler_jsonoutput');}
public static function register_errorhandler_dbwriter(){
require_once \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_SYSTEMPATHREL).'/log/register_errorhandler_dbwriter.php';}
\SYSTEM\LOG\log::registerHandler('\SYSTEM\LOG\error_handler_dbwriter');}
}