errorhandler.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. function format_backtrace($trace) {
  3. $rv = "";
  4. $idx = 1;
  5. if (is_array($trace)) {
  6. foreach ($trace as $e) {
  7. if (isset($e["file"]) && isset($e["line"])) {
  8. $fmt_args = [];
  9. if (is_array($e["args"])) {
  10. foreach ($e["args"] as $a) {
  11. if (!is_object($a)) {
  12. array_push($fmt_args, $a);
  13. } else {
  14. array_push($fmt_args, "[" . get_class($a) . "]");
  15. }
  16. }
  17. }
  18. $filename = str_replace(dirname(__DIR__) . "/", "", $e["file"]);
  19. $rv .= sprintf("%d. %s(%s): %s(%s)\n",
  20. $idx, $filename, $e["line"], $e["function"], implode(", ", $fmt_args));
  21. $idx++;
  22. }
  23. }
  24. }
  25. return $rv;
  26. }
  27. function ttrss_error_handler($errno, $errstr, $file, $line, $context) {
  28. if (error_reporting() == 0 || !$errno) return false;
  29. $file = substr(str_replace(dirname(dirname(__FILE__)), "", $file), 1);
  30. $context = format_backtrace(debug_backtrace());
  31. $errstr = truncate_middle($errstr, 16384, " (...) ");
  32. if (class_exists("Logger"))
  33. return Logger::get()->log_error($errno, $errstr, $file, $line, $context);
  34. }
  35. function ttrss_fatal_handler() {
  36. global $last_query;
  37. $error = error_get_last();
  38. if ($error !== NULL) {
  39. $errno = $error["type"];
  40. $file = $error["file"];
  41. $line = $error["line"];
  42. $errstr = $error["message"];
  43. if (!$errno) return false;
  44. $context = format_backtrace(debug_backtrace());
  45. $file = substr(str_replace(dirname(dirname(__FILE__)), "", $file), 1);
  46. if ($last_query) $errstr .= " [Last query: $last_query]";
  47. if (class_exists("Logger"))
  48. return Logger::get()->log_error($errno, $errstr, $file, $line, $context);
  49. }
  50. return false;
  51. }
  52. register_shutdown_function('ttrss_fatal_handler');
  53. set_error_handler('ttrss_error_handler');