From b628331a9b32ce71d35b1449457344c3cf648953 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 15 Jun 2021 18:06:21 +0900 Subject: [PATCH] Fix Login class logging per class setting, fix per class getter Add a new helper function to get current class that called the debug function. Fix bug were log_per was not set correctly Change all get_class calls in debug to the new helper function --- www/lib/CoreLibs/ACL/Login.php | 6 +++--- www/lib/CoreLibs/Debug/Logging.php | 26 +++++++++++++++++++------- www/lib/CoreLibs/Debug/Support.php | 13 +++++++++++++ www/lib/Error.Handling.php | 9 --------- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/www/lib/CoreLibs/ACL/Login.php b/www/lib/CoreLibs/ACL/Login.php index 89374521..79c291c7 100644 --- a/www/lib/CoreLibs/ACL/Login.php +++ b/www/lib/CoreLibs/ACL/Login.php @@ -120,11 +120,11 @@ class Login extends \CoreLibs\DB\IO */ public function __construct(array $db_config) { - // log login data for this class only - $this->log->setLogPer('class', true); - // create db connection and init base class parent::__construct($db_config); + // log login data for this class only + $this->log->setLogPer('class', true); + // set db special errors if ($this->db_init_error === true) { echo 'Could not connect to DB
'; // if I can't connect to the DB to auth exit hard. No access allowed diff --git a/www/lib/CoreLibs/Debug/Logging.php b/www/lib/CoreLibs/Debug/Logging.php index c47443f2..54e4c079 100644 --- a/www/lib/CoreLibs/Debug/Logging.php +++ b/www/lib/CoreLibs/Debug/Logging.php @@ -229,8 +229,8 @@ class Logging $rpl_string = !$this->log_per_level ? '' : '_'.$level; // if request to write to one file $fn = str_replace('##LEVEL##', $rpl_string, $fn); // create output filename - - $rpl_string = !$this->log_per_class ? '' : '_'.str_replace('\\', '-', get_class($this)); // set sub class settings + // set per class, but don't use get_class as we will only get self + $rpl_string = !$this->log_per_class ? '' : '_'.str_replace('\\', '-', \CoreLibs\Debug\Support::getCallerClass()); // set sub class settings $fn = str_replace('##CLASS##', $rpl_string, $fn); // create output filename $rpl_string = !$this->log_per_page ? '' : '_'.\CoreLibs\Get\System::getPageName(1); // if request to write to one file @@ -252,6 +252,19 @@ class Logging return true; } + /** + * Superseeded by \CoreLibs\Debug\Support::getCallerClass() calls + * + * @return string Class name where called + * @deprecated Use \CoreLibs\Debug\Support::getCallerClass() insteader + */ + private function getCallerClass(): string + { + // get the last class entry and wrie that + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) ?? [['class' => get_class($this)]]; + return end($backtrace)['class']; + } + // *** PUBLIC *** /** @@ -384,7 +397,7 @@ class Logging if (!in_array($type, ['level', 'class', 'page', 'run'])) { return; } - $this->{'log_per'.$type} = $set; + $this->{'log_per_'.$type} = $set; } /** @@ -397,7 +410,7 @@ class Logging if (!in_array($type, ['level', 'class', 'page', 'run'])) { return false; } - return $this->{'log_per'.$type}; + return $this->{'log_per_'.$type}; } /** @@ -418,8 +431,7 @@ class Logging $this->error_msg[$level] = ''; } // get the last class entry and wrie that - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) ?? [['class' => get_class($this)]]; - $class = end($backtrace)['class']; + $class = \CoreLibs\Debug\Support::getCallerClass(); // get timestamp $timestamp = \CoreLibs\Debug\Support::printTime(); // HTML string, create only if we have echo @@ -502,7 +514,7 @@ class Logging // create the output wrapper around, so we have a nice formated output per class if ($string_output) { $string_prefix = '
' - .'
{'.get_class($this).'}
'; + .'
{'.\CoreLibs\Debug\Support::getCallerClass().'}
'; $string_output = $string_prefix.$string_output .'
Script Run Time: '.$script_end.'
' .'
'; diff --git a/www/lib/CoreLibs/Debug/Support.php b/www/lib/CoreLibs/Debug/Support.php index 445e3a40..f37d7a34 100644 --- a/www/lib/CoreLibs/Debug/Support.php +++ b/www/lib/CoreLibs/Debug/Support.php @@ -55,6 +55,19 @@ class Support } return null; } + + /** + * Get the current class where this function is called + * Is mostly used in debug log statements to get the class where the debug was called + * gets top level class + * + * @return string Class name with namespace + */ + public static function getCallerClass(): string + { + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) ?? [['class' => get_called_class()]]; + return end($backtrace)['class']; + } } // __END__ diff --git a/www/lib/Error.Handling.php b/www/lib/Error.Handling.php index b2ac7f28..366d9dec 100644 --- a/www/lib/Error.Handling.php +++ b/www/lib/Error.Handling.php @@ -16,15 +16,6 @@ if (defined('BASE')) { DEFINE('CURRENT_WORKING_DIR', str_replace('lib', '', __DIR__)); } -// METHOD: MyErrorHandler -// PARAMS: $type: the error code from PHP -// $message: the error message from php -// $file: in which file the error happend. this is the source file (eg include) -// $line: in which line the error happened -// $context: array with all the variable -// RETURN: true, so cought errors do not get processed by the PHP error engine -// DESC: will catch any error except E_ERROR and try to write them to the log file in log/php_error-.llog -// if this fails, it will print the data to the window via echo /** * will catch any error except E_ERROR and try to write them to the log file * in log/php_error-.log