From fbb747ea74a372c0e4c82b445e9e0928094637e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Mon, 13 Feb 2023 11:42:41 +0100 Subject: [PATCH] Fix #74 Add a PHP error logger in XMLRPC --- xmlrpc/src/misc/logging.php | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/xmlrpc/src/misc/logging.php b/xmlrpc/src/misc/logging.php index 4518737..4d1d85e 100644 --- a/xmlrpc/src/misc/logging.php +++ b/xmlrpc/src/misc/logging.php @@ -91,3 +91,43 @@ class Logger $this->mylog("Debug", $message); } } + +function systemErrorHandler($errno, string $errstr, string $errfile = '', int $errline = 0, $trace = '') +{ + if (\is_array($trace)) $trace = ''; + + $error = $errstr . " in " . $errfile . ' (line ' . $errline . ")\n"; + $fullError = $error . 'Trace' . "\n" . $trace; + + Logger::getInstance()->debug($fullError); + + return false; +} + +function exceptionHandler($exception) +{ + systemErrorHandler( + E_ERROR, + get_class($exception) . ': '. $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $exception->getTraceAsString() + ); +} + +function fatalErrorShutdownHandler() +{ + $lastError = error_get_last(); + if ($lastError && $lastError['type'] === E_ERROR) { + systemErrorHandler( + E_ERROR, + $lastError['message'], + $lastError['file'], + $lastError['line'] + ); + } +} + +set_error_handler('systemErrorHandler', E_ALL); +set_exception_handler('exceptionHandler'); +register_shutdown_function('fatalErrorShutdownHandler'); \ No newline at end of file