1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-21 22:32:41 +01:00

PhutilErrorHandler: support multiple error listeners

Summary: Ref T15554. The plan is to add a new listener that will only listen to DEPRECATED events, and do something useful with them.

Test Plan: Test script in P26 shows registering 2 handlers and getting both invoked.

Reviewers: O1 Blessed Committers, Matthew

Reviewed By: O1 Blessed Committers, Matthew

Subscribers: Sten, speck, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Maniphest Tasks: T15554

Differential Revision: https://we.phorge.it/D25388
This commit is contained in:
Aviv Eyal 2023-11-24 18:58:36 +02:00
parent 5bc53cfe53
commit 25611ba24a
3 changed files with 17 additions and 8 deletions

View file

@ -6,7 +6,7 @@
* *
* This class takes over the PHP error and exception handlers when you call * This class takes over the PHP error and exception handlers when you call
* ##PhutilErrorHandler::initialize()## and forwards all debugging information * ##PhutilErrorHandler::initialize()## and forwards all debugging information
* to a listener you install with ##PhutilErrorHandler::setErrorListener()##. * to a listener you install with ##PhutilErrorHandler::addErrorListener()##.
* *
* To use PhutilErrorHandler, which will enhance the messages printed to the * To use PhutilErrorHandler, which will enhance the messages printed to the
* PHP error log, just initialize it: * PHP error log, just initialize it:
@ -16,7 +16,7 @@
* To additionally install a custom listener which can print error information * To additionally install a custom listener which can print error information
* to some other file or console, register a listener: * to some other file or console, register a listener:
* *
* PhutilErrorHandler::setErrorListener($some_callback); * PhutilErrorHandler::addErrorListener($some_callback);
* *
* For information on writing an error listener, see * For information on writing an error listener, see
* @{function:phutil_error_listener_example}. Providing a listener is optional, * @{function:phutil_error_listener_example}. Providing a listener is optional,
@ -31,7 +31,7 @@
*/ */
final class PhutilErrorHandler extends Phobject { final class PhutilErrorHandler extends Phobject {
private static $errorListener = null; private static $errorListeners = array();
private static $initialized = false; private static $initialized = false;
private static $traps = array(); private static $traps = array();
@ -68,8 +68,15 @@ final class PhutilErrorHandler extends Phobject {
* @return void * @return void
* @task config * @task config
*/ */
public static function addErrorListener($listener) {
self::$errorListeners[] = $listener;
}
/**
* Deprecated - use `addErrorListener`.
*/
public static function setErrorListener($listener) { public static function setErrorListener($listener) {
self::$errorListener = $listener; self::addErrorListener($listener);
} }
@ -438,7 +445,7 @@ final class PhutilErrorHandler extends Phobject {
break; break;
} }
if (self::$errorListener) { if (self::$errorListeners) {
static $handling_error; static $handling_error;
if ($handling_error) { if ($handling_error) {
error_log( error_log(
@ -447,7 +454,9 @@ final class PhutilErrorHandler extends Phobject {
return; return;
} }
$handling_error = true; $handling_error = true;
call_user_func(self::$errorListener, $event, $value, $metadata); foreach (self::$errorListeners as $error_listener) {
call_user_func($error_listener, $event, $value, $metadata);
}
$handling_error = false; $handling_error = false;
} }
} }

View file

@ -41,7 +41,7 @@ function phlog($value/* , ... */) {
/** /**
* Example @{class:PhutilErrorHandler} error listener callback. When you call * Example @{class:PhutilErrorHandler} error listener callback. When you call
* `PhutilErrorHandler::setErrorListener()`, you must pass a callback function * `PhutilErrorHandler::addErrorListener()`, you must pass a callback function
* with the same signature as this one. * with the same signature as this one.
* *
* NOTE: @{class:PhutilErrorHandler} handles writing messages to the error * NOTE: @{class:PhutilErrorHandler} handles writing messages to the error

View file

@ -83,7 +83,7 @@ final class PhutilErrorLog
} }
public function onError($event, $value, array $metadata) { public function onError($event, $value, array $metadata) {
// If we've set "error_log" to a real file, so messages won't be output to // If we've set "error_log" to a real file, messages won't be output to
// stderr by default. Copy them to stderr. // stderr by default. Copy them to stderr.
if ($this->logPath === null) { if ($this->logPath === null) {