2013-04-16 15:41:31 +00:00
|
|
|
<?php
|
2021-11-11 20:46:42 +00:00
|
|
|
/**
|
|
|
|
* @param array<int, array<string, mixed>> $trace
|
|
|
|
*/
|
|
|
|
function format_backtrace($trace): string {
|
2015-12-04 12:58:20 +00:00
|
|
|
$rv = "";
|
|
|
|
$idx = 1;
|
|
|
|
|
|
|
|
if (is_array($trace)) {
|
|
|
|
foreach ($trace as $e) {
|
|
|
|
if (isset($e["file"]) && isset($e["line"])) {
|
|
|
|
$fmt_args = [];
|
|
|
|
|
2021-09-08 06:04:15 +00:00
|
|
|
if (is_array($e["args"] ?? false)) {
|
2015-12-04 12:58:20 +00:00
|
|
|
foreach ($e["args"] as $a) {
|
2021-02-05 20:41:32 +00:00
|
|
|
if (is_object($a)) {
|
2021-02-06 07:10:54 +00:00
|
|
|
array_push($fmt_args, "{" . get_class($a) . "}");
|
2021-02-05 20:41:32 +00:00
|
|
|
} else if (is_array($a)) {
|
2021-02-06 07:10:54 +00:00
|
|
|
array_push($fmt_args, "[" . truncate_string(json_encode($a), 256, "...")) . "]";
|
2021-02-11 07:29:42 +00:00
|
|
|
} else if (is_resource($a)) {
|
|
|
|
array_push($fmt_args, truncate_string(get_resource_type($a), 256, "..."));
|
2021-11-20 13:15:10 +00:00
|
|
|
} else if (is_string($a)) {
|
2021-02-06 07:10:54 +00:00
|
|
|
array_push($fmt_args, truncate_string($a, 256, "..."));
|
2015-12-04 12:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = str_replace(dirname(__DIR__) . "/", "", $e["file"]);
|
|
|
|
|
|
|
|
$rv .= sprintf("%d. %s(%s): %s(%s)\n",
|
2021-02-05 20:41:32 +00:00
|
|
|
$idx,
|
|
|
|
$filename,
|
|
|
|
$e["line"],
|
|
|
|
$e["function"],
|
|
|
|
implode(", ", $fmt_args));
|
2015-12-04 12:58:20 +00:00
|
|
|
|
|
|
|
$idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rv;
|
|
|
|
}
|
|
|
|
|
2021-11-11 20:46:42 +00:00
|
|
|
function ttrss_error_handler(int $errno, string $errstr, string $file, int $line): bool {
|
2022-09-29 14:46:33 +00:00
|
|
|
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
|
2020-12-12 15:56:10 +00:00
|
|
|
if (error_reporting() == 0 || !$errno) return false;
|
|
|
|
} else {
|
|
|
|
if (!(error_reporting() & $errno)) return false;
|
|
|
|
}
|
|
|
|
|
2021-02-22 14:38:46 +00:00
|
|
|
$file = substr(str_replace(dirname(__DIR__), "", $file), 1);
|
2013-04-16 15:41:31 +00:00
|
|
|
|
2015-12-04 12:58:20 +00:00
|
|
|
$context = format_backtrace(debug_backtrace());
|
2016-02-17 13:42:13 +00:00
|
|
|
$errstr = truncate_middle($errstr, 16384, " (...) ");
|
2015-12-03 13:15:28 +00:00
|
|
|
|
2013-05-13 09:32:51 +00:00
|
|
|
if (class_exists("Logger"))
|
2021-02-25 14:10:03 +00:00
|
|
|
return Logger::log_error((int)$errno, $errstr, $file, (int)$line, $context);
|
2021-03-07 06:02:24 +00:00
|
|
|
else
|
|
|
|
return false;
|
2013-04-16 15:41:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 20:46:42 +00:00
|
|
|
function ttrss_fatal_handler(): bool {
|
2013-04-16 15:41:31 +00:00
|
|
|
$error = error_get_last();
|
|
|
|
|
|
|
|
if ($error !== NULL) {
|
2013-04-17 04:32:45 +00:00
|
|
|
$errno = $error["type"];
|
2013-04-16 15:41:31 +00:00
|
|
|
$file = $error["file"];
|
|
|
|
$line = $error["line"];
|
|
|
|
$errstr = $error["message"];
|
|
|
|
|
2013-04-17 13:00:24 +00:00
|
|
|
if (!$errno) return false;
|
|
|
|
|
2015-12-04 12:58:20 +00:00
|
|
|
$context = format_backtrace(debug_backtrace());
|
2013-04-16 15:41:31 +00:00
|
|
|
|
2021-02-22 14:38:46 +00:00
|
|
|
$file = substr(str_replace(dirname(__DIR__), "", $file), 1);
|
2013-04-16 15:41:31 +00:00
|
|
|
|
2013-05-13 09:32:51 +00:00
|
|
|
if (class_exists("Logger"))
|
2021-02-25 14:10:03 +00:00
|
|
|
return Logger::log_error((int)$errno, $errstr, $file, (int)$line, $context);
|
2013-04-16 15:41:31 +00:00
|
|
|
}
|
2013-04-17 12:05:52 +00:00
|
|
|
|
|
|
|
return false;
|
2013-04-16 15:41:31 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 20:22:34 +00:00
|
|
|
register_shutdown_function('ttrss_fatal_handler');
|
|
|
|
set_error_handler('ttrss_error_handler');
|
2017-04-26 17:24:18 +00:00
|
|
|
|