From 22a5be4ddd504616e2c7f58bda1ec6e2cb49f61a Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Fri, 13 Dec 2024 12:03:10 +0100 Subject: [PATCH] LSlog: fix computing exception context --- src/includes/class/class.LSlog.php | 47 ++++++++++++++++++------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/includes/class/class.LSlog.php b/src/includes/class/class.LSlog.php index eb2580c3..a00a6a6d 100644 --- a/src/includes/class/class.LSlog.php +++ b/src/includes/class/class.LSlog.php @@ -401,24 +401,35 @@ class LSlog { * @return void **/ public static function exception($exception, $prefix=null, $fatal=true, $logger=null) { - $message = - ($prefix?"$prefix :\n":"An exception occured :\n"). - implode( - "\n", - array_map( - function($line) { return " $line"; }, - array_slice( - array_reverse( - explode( - "\n", - $exception->getTraceAsString() - ), - 1 - ), - 1 - ) - ) - )."\n => ". $exception->getMessage(); + $message = sprintf("%s:\n", $prefix ? $prefix : "An exception occured"); + + // Add backtrace + // Note: reverse the backtrace lines to get the entrypoint as first and remove useless first + // "{main}" line. + $lines = array_slice( + array_reverse( + explode( + "\n", + $exception->getTraceAsString() + ), + 1 + ), + 1 + ); + foreach($lines as $idx => $line) + $message .= sprintf( + " #%d %s\n", + $idx + 1, + substr($line, 2) + ); + + // Add exception details + $message .= sprintf( + " #0 %s(%s) :\n => %s", + $exception->getFile(), + $exception->getLine(), + $exception->getMessage() + ); self :: logging(($fatal?'FATAL':'ERROR'), $message, $logger, true); }