<?php
function getDebugBacktrace($NL = "\n") {
$dbgTrace = array_reverse(debug_backtrace());
$dbgMsg = "";
foreach($dbgTrace as $dbgInfo) {
$dbgArg="";
if(isset($dbgInfo['args'])) {
foreach ($dbgInfo['args'] as $a) {
if (!empty($dbgArg)) {
$dbgArg .= ', ';
}
$dbgArg .= printVal($a);
}
}
$dbgMsg .= (isset($dbgInfo['file'])?("File ".$dbgInfo['file'].": "):"").
(isset($dbgInfo['line'])?("Line ".$dbgInfo['line'].": "):"")."Called: {$dbgInfo['function']}(".($dbgArg?("".$dbgArg):"").");$NL";
}
//$dbgMsg .= $NL;
return $dbgMsg;
}
function errorHandler($errno, $errmsg, $filename, $linenum, $vars = null) {
if(!(error_reporting() & $errno))
return false;
//variables
global $prefix;
global $ip, $host;
$date=date("Y-m-d H:i:s");
switch ($errno) {
case E_ERROR:
$errtype = 'Error';
break;
case E_WARNING:
$errtype = 'Warning';
break;
case E_NOTICE:
$errtype = 'Notice';
break;
case E_USER_ERROR:
$errtype = 'User Error';
break;
case E_USER_WARNING:
$errtype = 'User Warning';
break;
case E_USER_NOTICE:
$errtype = 'User Notice';
break;
case E_STRICT:
// $errtype = 'Runtime Notice';
break;
case E_RECOVERABLE_ERROR:
$errtype = 'Catchable Fatal Error';
break;
default:
$errtype='Unknown Error';
break;
}
if(isset($errtype)) {
$err = "Date: ".date("Y-m-d H:i:s").(($ip != "unknown")?" IP: $ip":"").(!empty($host)?" Host: $host":"")."\n$errtype: $errmsg\nFile: ".$filename."\nLine: ".$linenum."\nError Number: $errno\n";
if (isset($_SERVER['REQUEST_URI'])) $err .= "Request Uri:".$_SERVER['REQUEST_URI']."\n";
$err .= "Debug Backtrace:\n".getDebugBacktrace()."\n\n";
if (@is_dir("$prefix/logs")) error_log($err, 3, "$prefix/logs/errorlog.txt");
}
return false;
}
function exception_handler(Throwable $exception) {
global $prefix;
global $ip, $host;
$type="Exception";
$msg=$exception->getMessage();
$code=$exception->getCode();
$fname=$exception->getFile();
$line=$exception->getLine();
if(isset($type)) {
$err="Date: ".date("Y-m-d H:i:s").(($ip != "unknown")?" IP: $ip":"").(!empty($host)?" Host: $host":"")."\n$type: $msg\nFile: $fname\nLine: ".$line."\n".(isset($_SERVER['REQUEST_URI'])?("Request Uri:".$_SERVER['REQUEST_URI']."\n"):"")."Debug Backtrace:\n".getDebugBacktrace()."\n\n";
error_log($err, 3, "$prefix/logs/errorlog.txt");
}
throw $exception;
}
register_shutdown_function(function () {
$ignore=false;
global $prefix;
global $ip, $host;
$err = error_get_last();
if ($err !== null) {
$errno = $err['type'];
switch ($errno) {
case E_ERROR:
$errtype = 'Error';
break;
default:
$ignore = true;
break;
}
if (!$ignore) {
$errmsg = $err["message"];
$filename = $err["file"];
$linenum = $err["line"];
$msg="Date: ".date("Y-m-d H:i:s").(($ip != "unknown")?" IP: $ip":"").(!empty($host)?" Host: $host":"")."\n$errtype: $errmsg\nFile: ".$filename."\nLine: ".$linenum."\nError Number: $errno\n".(isset($_SERVER['REQUEST_URI'])?("Request Uri:".$_SERVER['REQUEST_URI']."\n"):"")."Debug Backtrace:\n".getDebugBacktrace()."\n\n";
if (@is_dir("$prefix/logs")) error_log($msg, 3, "$prefix/logs/errorlog.txt");
}
}
});
set_error_handler("errorHandler",E_ALL ^ E_DEPRECATED);
set_exception_handler("exception_handler");
?>