From 47e30ff5364f54591d79ec8b387cccfbe5b7dc71 Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Sat, 29 Oct 2022 00:48:01 -0700 Subject: [PATCH] Create config options for stacktraces --- src/bb-config-sample.php | 9 +++++++++ src/bb-library/Box/Exception.php | 9 ++++++--- src/bb-library/Box/Tools.php | 2 ++ src/install/install.php | 2 ++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/bb-config-sample.php b/src/bb-config-sample.php index b474e995e..3c351b93f 100755 --- a/src/bb-config-sample.php +++ b/src/bb-config-sample.php @@ -29,6 +29,15 @@ return [ */ 'debug' => false, + /** + * Enable or disable stacktraces when an exception is thrown (also requires debug to be enabled). + */ + 'log_stacktrace' => true, + /** + * How long the stacktrace should be. + */ + 'stacktrace_length' => 25, + 'maintenance_mode' => [ /** * Enable or disable the system maintenance mode. diff --git a/src/bb-library/Box/Exception.php b/src/bb-library/Box/Exception.php index 11606bebd..300d81558 100644 --- a/src/bb-library/Box/Exception.php +++ b/src/bb-library/Box/Exception.php @@ -23,10 +23,13 @@ class Box_Exception extends Exception public function __construct($message, array $variables = NULL, $code = 0) { $config = include BB_PATH_ROOT.'/bb-config.php'; + $debug = (isset($config['debug'])) ? isset($config['debug']) : false; + $logStack = (isset($config['log_stacktrace'])) ? isset($config['log_stacktrace']) : true; + $stackLength = (isset($config['stacktrace_length'])) ? isset($config['stacktrace_length']) : 25; - if(isset($config['debug']) && $config['debug'] === true){ - error_log('An exception has been called. Stacktrace:'); - error_log( $this->stackTrace() ); + if($debug && $logStack){ + error_log('An exception has been thrown. Stacktrace:'); + error_log( $this->stackTrace($stackLength) ); } // Set the message diff --git a/src/bb-library/Box/Tools.php b/src/bb-library/Box/Tools.php index 9a778056e..d02074214 100644 --- a/src/bb-library/Box/Tools.php +++ b/src/bb-library/Box/Tools.php @@ -490,6 +490,8 @@ class Box_Tools $newConfig = [ 'debug' => (isset($currentConfig['debug'])) ? $currentConfig['debug'] : false, + 'log_stacktrace' => (isset($currentConfig['log_stacktrace'])) ? $currentConfig['log_stacktrace'] : true, + 'stacktrace_length' => (isset($currentConfig['stacktrace_length'])) ? $currentConfig['stacktrace_length'] : 25, 'maintenance_mode' => [ 'enabled' => (isset($currentConfig['maintenance_mode']['enabled'])) ? $currentConfig['maintenance_mode']['enabled'] : false, 'allowed_urls' => (isset($currentConfig['maintenance_mode']['allowed_urls'])) ? $currentConfig['maintenance_mode']['allowed_urls'] : [], diff --git a/src/install/install.php b/src/install/install.php index f0b662dea..561a4f626 100644 --- a/src/install/install.php +++ b/src/install/install.php @@ -411,6 +411,8 @@ final class Box_Installer // TODO: Why not just take the defaults from the bb.config.example.php file and modify accordingly? Also this method doesn't preserve the comments in the example config. $data = [ 'debug' => false, + 'log_stacktrace' => true, + 'stacktrace_length' => 25, 'maintenance_mode' => [ 'enabled' => false,