From dbd0b77a20ed0265ca0dc8bc5d2a96276ee0ca7f Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud <marcimat@rezo.net> Date: Wed, 22 Mar 2023 16:17:31 +0100 Subject: [PATCH] feat: Loger les deprecated dans un fichier de log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Création d’une classe Spip\ErrorHandler pour gérer cela. Plus tard, il faudra lui passer un logger en entrée. --- ecrire/inc/utils.php | 1 + ecrire/inc_version.php | 8 ++++---- ecrire/src/ErrorHandler.php | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 ecrire/src/ErrorHandler.php diff --git a/ecrire/inc/utils.php b/ecrire/inc/utils.php index 778aff80d3..0256652cf0 100644 --- a/ecrire/inc/utils.php +++ b/ecrire/inc/utils.php @@ -394,6 +394,7 @@ function spip_log($message = null, $name = null) { } } + /** * Enregistrement des journaux * diff --git a/ecrire/inc_version.php b/ecrire/inc_version.php index 124b6ab810..68324faeb6 100644 --- a/ecrire/inc_version.php +++ b/ecrire/inc_version.php @@ -9,6 +9,8 @@ * Ce programme est un logiciel libre distribué sous licence GNU/GPL. * \***************************************************************************/ +use Spip\ErrorHandler; + /** * Initialisation de SPIP * @@ -28,9 +30,6 @@ if (defined('_ECRIRE_INC_VERSION')) { */ define('_ECRIRE_INC_VERSION', '1'); -# masquer les eventuelles erreurs sur les premiers define -error_reporting(E_ALL ^ E_NOTICE); - /** version PHP minimum exigee (cf. inc/utils) */ define('_PHP_MIN', '8.1.0'); define('_PHP_MAX', '8.2.99'); @@ -484,7 +483,8 @@ if (!defined('SPIP_ERREUR_REPORT')) { /** Masquer les warning */ define('SPIP_ERREUR_REPORT', E_ALL ^ E_NOTICE ^ E_DEPRECATED); } -error_reporting(SPIP_ERREUR_REPORT); + +ErrorHandler::setup(SPIP_ERREUR_REPORT); // Initialisations critiques non surchargeables par les plugins // INITIALISER LES REPERTOIRES NON PARTAGEABLES ET LES CONSTANTES diff --git a/ecrire/src/ErrorHandler.php b/ecrire/src/ErrorHandler.php new file mode 100644 index 0000000000..3e95006178 --- /dev/null +++ b/ecrire/src/ErrorHandler.php @@ -0,0 +1,40 @@ +<?php + +namespace Spip; + +/** + * Gestion des erreurs PHP + * @internal + */ +final class ErrorHandler { + + static bool $done = false; + + public static function setup(?int $error_level = null): void { + if (!self::$done) { + self::$done = true; + error_reporting($error_level); + set_error_handler(self::user_deprecated(...), E_USER_DEPRECATED); + } + } + + /** Loger les `trigger_deprecated()` */ + private static function user_deprecated(int $errno, string $errstr, string $errfile, int $errline): bool { + if (!(\E_USER_DEPRECATED & $errno)) { + return false; + } + + $backtrace = debug_backtrace(); + array_shift($backtrace); + do { + $t = array_shift($backtrace); + $fqdn = $t['class'] . $t['type'] . $t['function']; + } while (in_array($fqdn, ['trigger_error', 'trigger_deprecation'])); + + $errfile = $t['file']; + $errline = $t['line']; + + spip_log(sprintf('%s in %s on line %s', $errstr, $errfile, $errline), 'deprecated.' . _LOG_INFO); + return false; + } +} -- GitLab