Box_Exception: log a backtrace when debug enabled

This commit is contained in:
Belle Aerni committed 2022-10-28 23:46:28 -07:00
1 parent b8b59261d0
commit 0725d02cae
1 file changed
+47 -1
+47 -1
View File
@@ -22,10 +22,56 @@ class Box_Exception extends Exception
*/
public function __construct($message, array $variables = NULL, $code = 0)
{
$config = include BB_PATH_ROOT.'/bb-config.php';
if(isset($config['debug']) && $config['debug'] === true){
error_log('An exception has been called. Stacktrace:');
error_log( $this->stackTrace() );
}
// Set the message
$message = __($message, $variables);
// Pass the message to the parent
parent::__construct($message, $code);
}
}
/**
* Big thank you to jhurliman and jambroseclarke on Stack Overflow for this backtrace formatter.
* We have slightly modified it for our purposes
* https://stackoverflow.com/a/32365961
*/
private function stackTrace($Length = 25) {
$stack = debug_backtrace($Length);
$output = '';
$stackLen = count($stack);
for ($i = 1; $i < $stackLen; $i++) {
$entry = $stack[$i];
$func = $entry['function'] . '(';
if(isset($entry['args'])){
$argsLen = count($entry['args']);
for ($j = 0; $j < $argsLen; $j++) {
$my_entry = $entry['args'][$j];
if (is_string($my_entry)) {
$func .= $my_entry;
}
if ($j < $argsLen - 1) $func .= ', ';
}
}
$func .= ')';
$entry_file = 'NO_FILE';
if (array_key_exists('file', $entry)) {
$entry_file = $entry['file'];
}
$entry_line = 'NO_LINE';
if (array_key_exists('line', $entry)) {
$entry_line = $entry['line'];
}
$output .= $entry_file . ':' . $entry_line . ' - ' . $func . PHP_EOL;
}
return $output;
}
}