From d068aaeed7ce20e432d2acff58d32e920b9c34de Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 16 Jun 2021 10:11:44 +0900 Subject: [PATCH] Update Debug\Logger for some simpler code All strings are written to an array and not string append. Group debug write/debug echo better to make it more simple. Fixed bug with echo output in logging Also set debug to be deprecated for phan testing --- www/admin/class_test.debug.php | 50 ++++++++++++++---- www/lib/CoreLibs/Basic.php | 3 +- www/lib/CoreLibs/DB/Extended/ArrayIO.php | 2 +- www/lib/CoreLibs/DB/IO.php | 4 +- www/lib/CoreLibs/Debug/Logging.php | 64 +++++++++++------------- 5 files changed, 74 insertions(+), 49 deletions(-) diff --git a/www/admin/class_test.debug.php b/www/admin/class_test.debug.php index 9cc712bc..f07f5df8 100644 --- a/www/admin/class_test.debug.php +++ b/www/admin/class_test.debug.php @@ -3,11 +3,12 @@ * @phan-file-suppress PhanTypeSuspiciousStringExpression */ -$DEBUG_ALL_OVERRIDE = 0; // set to 1 to debug on live/remote server locations -$DEBUG_ALL = 1; -$PRINT_ALL = 0; +// all the settings are overruled by config +$DEBUG_ALL_OVERRIDE = true; // set to 1 to debug on live/remote server locations +$DEBUG_ALL = true; +$PRINT_ALL = false; $ECHO_ALL = true; -$DB_DEBUG = 1; +$DB_DEBUG = true; if ($DEBUG_ALL) { error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR); @@ -26,7 +27,7 @@ if (!defined('SET_SESSION_NAME')) { // define log file id $LOG_FILE_ID = 'classTest-debug'; ob_end_flush(); - +// override ECHO ALL FALSE $ECHO_ALL = true; use CoreLibs\Debug\Support as DebugSupport; @@ -53,7 +54,10 @@ print "V-S::PRINTAR: ".$debug_support_class::printAr(['Foo', 'Bar'])."
"; // debug print "C->DEBUG: ".$debug->debug('CLASS-TEST-DEBUG', 'Class Test Debug')."
"; +print "C->DEBUG(html): ".$debug->debug('CLASS-TEST-DEBUG', 'HTML TAG
BOLD')."
"; +print "C->DEBUG(html,strip): ".$debug->debug('CLASS-TEST-DEBUG', 'HTML TAG
BOLD', true)."
"; print "C->PRINTERRORMSG:
".$debug->printErrorMsg()."
"; +echo "CLASS DEBUG CALL
"; class TestL { @@ -62,15 +66,40 @@ class TestL { $this->log = new CoreLibs\Debug\Logging(); } - public function test() + public function test(string $ts = null) { - $this->log->debug('TESTL', 'Logging in class testL'); - print "IN TestL->test:
".$this->log->printErrorMsg()."
"; + print "* GETCALLERCLASS(INSIDE CLASS): ".\CoreLibs\Debug\Support::getCallerClass()."
"; + $this->log->debug('TESTL', 'Logging in class testL'.($ts !== null ? ': '.$ts : '')); + $this->log->debug('TESTL', 'Some other message'); + return true; + } +} +class TestR extends TestL +{ + public $foo; + public function __construct() + { + parent::__construct(); + } + public function subTest() + { + print "** GETCALLERCLASS(INSIDE EXTND CLASS): ".\CoreLibs\Debug\Support::getCallerClass()."
"; + $this->log->debug('TESTR', 'Logging in class testR (extends testL)'); + $this->test('TESTR INSIDE'); return true; } } $tl = new TestL(); -print "CLASS SUB: DEBUG: ".$tl->test()."
"; +print "CLASS: LOG ECHO: ".(string)$tl->log->getLogLevelAll('echo')."
"; +print "CLASS: DEBUG: ".$tl->test()."
"; +print "CLASS: PRINTERRORMSG:
".$tl->log->printErrorMsg()."
"; +$tr = new TestR(); +print "CLASS: LOG ECHO: ".(string)$tr->log->getLogLevelAll('echo')."
"; +print "CLASS EXTEND: DEBUG/tl: ".$tr->test('TESTR OUTSIDE')."
"; +print "CLASS EXTEND: DEBUG/tr: ".$tr->subTest()."
"; +print "CLASS EXTEND: PRINTERRORMSG:
".$tr->log->printErrorMsg()."
"; + +print "GETCALLERCLASS(NON CLASS): ".\CoreLibs\Debug\Support::getCallerClass()."
"; // fdebug print "S::FSETFILENAME: ".FileWriter::fsetFilename('class_test_debug_file.log')."
"; @@ -78,7 +107,8 @@ print "S::FDEBUG: ".FileWriter::fdebug('CLASS TEST DEBUG FILE: '.date('Y-m-d H:i // error message // future DEPRECATED -$basic->debug('BASIC CLASS', 'Debug test'); +// $basic->debug('BASIC CLASS', 'Debug test'); +$basic->log->debug('BASIC CLASS', 'Debug test'); print "BASIC:
".$basic->log->printErrorMsg(); print ""; diff --git a/www/lib/CoreLibs/Basic.php b/www/lib/CoreLibs/Basic.php index 49772bb7..4abbd86c 100644 --- a/www/lib/CoreLibs/Basic.php +++ b/www/lib/CoreLibs/Basic.php @@ -325,7 +325,7 @@ class Basic * all html tags will be stripped and
changed to \n * this is only used for debug output * @return void has no return - * @[TODO]deprecated Use $basic->log->debug() instead + * @deprecated Use $basic->log->debug() instead */ public function debug(string $level, string $string, bool $strip = false): void { @@ -446,7 +446,6 @@ class Basic * prints a html formatted (pre) array * @param array $array any array * @return string formatted array for output with
 tag added
-	 * DEPRCATE HARD LATER
 	 * @deprecated Use \CoreLibs\Debug\Support::printAr() instead
 	 */
 	public static function printAr(array $array): string
diff --git a/www/lib/CoreLibs/DB/Extended/ArrayIO.php b/www/lib/CoreLibs/DB/Extended/ArrayIO.php
index ecd6284d..c6e58604 100644
--- a/www/lib/CoreLibs/DB/Extended/ArrayIO.php
+++ b/www/lib/CoreLibs/DB/Extended/ArrayIO.php
@@ -407,7 +407,7 @@ class ArrayIO extends \CoreLibs\DB\IO
 				}
 				// integer is different
 				if (isset($this->table_array[$column]['int']) || isset($this->table_array[$column]['int_null'])) {
-					$this->debug('write_check', '['.$column.']['.$this->table_array[$column]['value'].']['.$this->table_array[$column]['type'].'] '.
+					$this->log->debug('write_check', '['.$column.']['.$this->table_array[$column]['value'].']['.$this->table_array[$column]['type'].'] '.
 						'VALUE SET: '.(string)isset($this->table_array[$column]['value']).
 						' | INT NULL: '.(string)isset($this->table_array[$column]['int_null']));
 					if (isset($this->table_array[$column]['value']) &&
diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php
index c684d834..78a38c1a 100644
--- a/www/lib/CoreLibs/DB/IO.php
+++ b/www/lib/CoreLibs/DB/IO.php
@@ -547,7 +547,7 @@ class IO extends \CoreLibs\Basic
 		if ($prefix) {
 			$prefix .= '- ';
 		}
-		$this->debug($debug_id, $prefix.$error_string, true);
+		$this->log->debug($debug_id, $prefix.$error_string, true);
 	}
 
 	/**
@@ -1624,7 +1624,7 @@ class IO extends \CoreLibs\Basic
 		}
 		$result = $this->db_functions->__dbExecute($stm_name, $data);
 		if (!$result) {
-			$this->debug('ExecuteData', 'ERROR in STM['.$stm_name.'|'.$this->prepare_cursor[$stm_name]['result'].']: '.\CoreLibs\Debug\Support::printAr($data));
+			$this->log->debug('ExecuteData', 'ERROR in STM['.$stm_name.'|'.$this->prepare_cursor[$stm_name]['result'].']: '.\CoreLibs\Debug\Support::printAr($data));
 			$this->error_id = 22;
 			$this->__dbError($this->prepare_cursor[$stm_name]['result']);
 			$this->__dbDebug('db', 'DB-Error '.$stm_name.': Execution failed', 'DB_ERROR');
diff --git a/www/lib/CoreLibs/Debug/Logging.php b/www/lib/CoreLibs/Debug/Logging.php
index 54e4c079..233db905 100644
--- a/www/lib/CoreLibs/Debug/Logging.php
+++ b/www/lib/CoreLibs/Debug/Logging.php
@@ -427,16 +427,37 @@ class Logging
 		if (!$this->doDebugTrigger('debug', $level)) {
 			return false;
 		}
-		if (!isset($this->error_msg[$level])) {
-			$this->error_msg[$level] = '';
-		}
 		// get the last class entry and wrie that
 		$class = \CoreLibs\Debug\Support::getCallerClass();
 		// get timestamp
 		$timestamp = \CoreLibs\Debug\Support::printTime();
-		// HTML string, create only if we have echo
+		// same string put for print (no html data inside)
+		// write to file if set
+		$this->writeErrorMsg(
+			$level,
+			'['.$timestamp.'] '
+			.'['.$this->host_name.'] '
+			.'['.\CoreLibs\Get\System::getPageName(2).'] '
+			.'['.$this->running_uid.'] '
+			.'{'.$class.'} '
+			.'<'.$level.'> - '
+			// if stripping all html, etc is requested, only for write error msg
+			.($strip ?
+				// find any 
and replace them with \n + // strip rest of html elements (base only) + preg_replace("/(<\/?)(\w+)([^>]*>)/", '[\\2]', str_replace('
', "\n", $string)) : + $string + ) + ."\n" + ); + // write to error level msg array if there is an echo request if ($this->doDebugTrigger('echo', $level)) { - $error_string = '
' + // init if not set + if (!isset($this->error_msg[$level])) { + $this->error_msg[$level] = []; + } + // HTML string + $this->error_msg[$level][] = '
' .'['.$timestamp.'] ' .'['.$level.'] ' .'['.$this->host_name.'] ' @@ -445,29 +466,6 @@ class Logging .'{'.$class.'} - '.\CoreLibs\Convert\Html::htmlent($string) ."
"; } - // if stripping all html, etc is requested - if ($strip) { - // find any
and replace them with \n - $string = str_replace('
', "\n", $string); - // strip rest of html elements - $string = preg_replace("/(<\/?)(\w+)([^>]*>)/", '', $string); - } - // same string put for print (no html data inside) - $error_string_print = - '['.$timestamp.'] ' - .'['.$this->host_name.'] ' - .'['.\CoreLibs\Get\System::getPageName(2).'] ' - .'['.$this->running_uid.'] ' - .'{'.$class.'} ' - .'<'.$level.'> - ' - .$string; - $error_string_print .= "\n"; - // write to file if set - $this->writeErrorMsg($level, $error_string_print); - // write to error level - if ($this->doDebugTrigger('echo', $level)) { - $this->error_msg[$level] .= $error_string; - } return true; } @@ -482,9 +480,7 @@ class Logging if (!is_array($error_msg)) { $error_msg = []; } - foreach ($error_msg as $level => $msg) { - $this->error_msg[$level] .= $msg; - } + array_push($this->error_msg, ...$error_msg); } /** @@ -503,11 +499,11 @@ class Logging foreach ($this->error_msg as $level => $temp_debug_output) { if ($this->doDebugTrigger('debug', $level)) { if ($this->doDebugTrigger('echo', $level)) { - $string_output = '
' + $string_output .= '
' .'['.$level.'] ' - .($string ? "**** ".\CoreLibs\Convert\Html::htmlent($string)." ****\n" : "") + .($string ? "**** ".\CoreLibs\Convert\Html::htmlent($string)." ****
\n" : "") .'
' - .$temp_debug_output; + .join('', $temp_debug_output); } // echo it out } // do printout } // for each level