From 50ca0fb1bf6db2bac6b87c2471f52edf50075019 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 18 Jun 2024 20:57:31 +0200 Subject: [PATCH] added Debugger::tryLog() [WIP] --- src/Tracy/Debugger/Debugger.php | 16 ++++++++++++---- src/Tracy/Debugger/ProductionStrategy.php | 14 ++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Tracy/Debugger/Debugger.php b/src/Tracy/Debugger/Debugger.php index 933757696..981bab997 100644 --- a/src/Tracy/Debugger/Debugger.php +++ b/src/Tracy/Debugger/Debugger.php @@ -326,10 +326,7 @@ public static function exceptionHandler(\Throwable $exception): void $handler($exception); } } catch (\Throwable $e) { - try { - self::log($e, self::EXCEPTION); - } catch (\Throwable) { - } + self::tryLog($e, self::EXCEPTION); } } @@ -564,6 +561,17 @@ public static function log(mixed $message, string $level = ILogger::INFO): mixed } + /** @internal */ + public static function tryLog(mixed $message, string $level = ILogger::INFO): ?\Throwable + { + try { + self::log($message, $level); + } catch (\Throwable $e) { + } + return $e ?? null; + } + + /** @internal */ public static function addSourceMapper(callable $mapper): void { diff --git a/src/Tracy/Debugger/ProductionStrategy.php b/src/Tracy/Debugger/ProductionStrategy.php index 52ac3a467..fb8f6d398 100644 --- a/src/Tracy/Debugger/ProductionStrategy.php +++ b/src/Tracy/Debugger/ProductionStrategy.php @@ -27,10 +27,7 @@ public function initialize(): void public function handleException(\Throwable $exception, bool $firstTime): void { - try { - Debugger::log($exception, Debugger::EXCEPTION); - } catch (\Throwable $e) { - } + $e = Debugger::tryLog($exception, Debugger::EXCEPTION); if (!$firstTime) { // nothing @@ -40,11 +37,11 @@ public function handleException(\Throwable $exception, bool $firstTime): void header('Content-Type: text/html; charset=UTF-8'); } - (fn($logged) => require Debugger::$errorTemplate ?: __DIR__ . '/assets/error.500.phtml')(empty($e)); + (fn($logged) => require Debugger::$errorTemplate ?: __DIR__ . '/assets/error.500.phtml')(!$e); } elseif (Helpers::isCli() && is_resource(STDERR)) { fwrite(STDERR, "ERROR: {$exception->getMessage()}\n" - . (isset($e) + . ($e ? 'Unable to log error. You may try enable debug mode to inspect the problem.' : 'Check log to see more info.') . "\n"); @@ -66,10 +63,7 @@ public function handleError( $err = 'PHP ' . Helpers::errorTypeToString($severity) . ': ' . Helpers::improveError($message) . " in $file:$line"; } - try { - Debugger::log($err, Debugger::ERROR); - } catch (\Throwable $e) { - } + Debugger::tryLog($err, Debugger::ERROR); }