<?php
class ErrorLog {
static $errorArr = array();
public static function throwError ($err, $level = E_USER_WARNING, $suppress = false, $shiftBack = 1) {
// Shiftback means: look back into the backtrace calls, shifting that amount of calls, and then report the error.
// There is no checking whether this many levels actually exist in the call stack, make sure yourself
// Error log doesn't show newlines.
str_replace(
array(
"\n"
),
array(
" "
),
$err
);
$trace = self::getBacktrace();
$info = "";
if (!empty($trace)) {
while ($shiftBack > 0) {
array_shift($trace);
--$shiftBack;
}
// Error file/line information:
$info .= "in " . $trace[0]["file"] . ":" . $trace[0]["line"] . ".";
// Caller information:
if (count($trace) > 1) {
array_shift($trace);
$caller = $trace[0];
$info .= " Thrown by ";
if (isset($caller["function"])) {
if (isset($caller["class"])) {
$info .= $caller["class"] . "::";
}
$info .= $caller["function"] . "() ";
} else {
$info .= "global scope ";
}
$info .= "which was called in " . $caller["file"] . ":" . $caller["line"] . ".";
}
}
$queryStr = $_SERVER["QUERY_STRING"];
$info .= " (Called URL: " . $_SERVER["HOST_NAME"] . $_SERVER["PHP_SELF"] . (!empty($queryStr) ? ("?" . $queryStr) : "") . ")";
$out = "\"" . $err . "\" " . $info;
self::$errorArr[] = $err;
if (!$suppress) {
trigger_error($out, $level);
}
}
public static function throwDeprecated ($suppress = false) {
// Call this method from INSIDE a deprecated function,
self::throwError("Deprecated!", E_USER_NOTICE, $suppress, 2);
}
private static function getBacktrace () {
if (version_compare(PHP_VERSION, "5.2.5", "<")) {
return debug_backtrace();
} else {
return debug_backtrace(false); // A bit more efficient
}
}
}
function a () {
ErrorLog::throwDeprecated();
}
a();
preferences:
38.63 ms | 402 KiB | 5 Q