X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fmiddleware%2Fdebug%2Fclass_DebugMiddleware.php;h=dae80dbba3cada78a2e10ab1148c4ae579956599;hb=def3387cce94074b6d4c6b73d33b0672dfd04f83;hp=e15630f34d05081adc4d86cd3c4caa50a29620db;hpb=2218902056efcf9a2c66fe7c24995e066bd7cd11;p=core.git diff --git a/framework/main/middleware/debug/class_DebugMiddleware.php b/framework/main/middleware/debug/class_DebugMiddleware.php index e15630f3..dae80dbb 100644 --- a/framework/main/middleware/debug/class_DebugMiddleware.php +++ b/framework/main/middleware/debug/class_DebugMiddleware.php @@ -4,11 +4,15 @@ namespace Org\Mxchange\CoreFramework\Middleware\Debug; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory; +use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; use Org\Mxchange\CoreFramework\Generic\NullPointerException; use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware; use Org\Mxchange\CoreFramework\Registry\Registerable; use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer; +// Import SPL stuff +use \InvalidArgumentException; + /** * The middlware debug output system. A *real* or concrete output class shall * become registered with this middleware because the back-fall class will @@ -16,7 +20,7 @@ use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer; * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @deprecated See LoggerFactory for a more flexible approach @@ -45,12 +49,13 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { * * @return void */ - protected function __construct () { + private function __construct () { // Call parent constructor + //* NOISY-DEBUG: */ printf('[%s:%d]: CONSTRUCTED!' . PHP_EOL, __METHOD__, __LINE__); parent::__construct(__CLASS__); - // Set own instance - self::$selfInstance = $this; + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } /** @@ -63,42 +68,100 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { * @param $className Class where a output should be created and * configured for * @return $debugInstance An instance of this middleware class + * @throws InvalidArgumentException If a parameter has an invalid value */ public static final function createDebugMiddleware (string $outputClass, string $className) { - //* DEBUG-DIE: */ die(__METHOD__.': outputClass=' . $outputClass . ',className=' . $className); - - // Create an instance if this middleware - $debugInstance = new DebugMiddleware(); + // Check parameter + //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s,className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $outputClass, $className); + if (empty($outputClass)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "outputClass" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($className)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "className" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } - // Default is that $outputClass may be invalid - $isInitialized = false; + // Is a static instance there? + //* NOISY-DEBUG: */ printf('[%s:%d]: self::selfInstance[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype(self::$selfInstance)); + if (is_null(self::$selfInstance)) { + // Create an instance if this middleware + self::$selfInstance = new DebugMiddleware(); + } // Is there a valid output instance provided? - if ((!is_null($outputClass)) && (is_object($outputClass)) && ($outputClass instanceof OutputStreamer)) { - // Use the given output instance - $debugInstance->setOutputInstance($outputClass); - - // All fine - $isInitialized = true; - } elseif (class_exists($outputClass)) { + //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s' . PHP_EOL, __METHOD__, __LINE__, $outputClass); + if (class_exists($outputClass) && is_null(self::$selfInstance->getOutputInstance())) { // A name for a debug output class has been provided so we try to get it + //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing outputClass=%s ...' . PHP_EOL, __METHOD__, __LINE__, $outputClass); $outputInstance = ObjectFactory::createObjectByName($outputClass); // Set this as output class - $debugInstance->setOutputInstance($outputInstance); - - // All fine - $isInitialized = true; + //* NOISY-DEBUG: */ printf('[%s:%d]: outputInstance=%s' . PHP_EOL, __METHOD__, __LINE__, $outputInstance->__toString()); + self::$selfInstance->setOutputInstance($outputInstance); } - // Is the output class initialized? - if ($isInitialized === true) { + // Is the output class loadable and an output instance is set? + if (class_exists($outputClass) && !is_null(self::$selfInstance->getOutputInstance())) { // Then set class name - $debugInstance->getOutputInstance()->setLoggerClassName($className); - } // END - if + //* NOISY-DEBUG: */ printf('[%s:%d]: Setting className=%s as logger class ...' . PHP_EOL, __METHOD__, __LINE__, $className); + self::$selfInstance->getOutputInstance()->setLoggerClassName($className); + } // Return instance - return $debugInstance; + //* NOISY-DEBUG: */ printf('[%s:%d]: debugInstance=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, self::$selfInstance->__toString()); + return self::$selfInstance; + } + + /** + * This method shall send debug output which can be HTML code for the + * browser or debug lines for a log file, etc. to the registered debug + * output instance. + * + * @param $message Data we shall 'stream' out to the world + * @param $stripTags Whether HTML tags shall be stripped out + * @return void + * @throws NullPointerException If this->outputInstance is NULL + */ + private function output (string $message, bool $stripTags = false) { + // Get backtrace + //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); + $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT); + + // Is the deprecated debugOutput() or partialStub() invoked before? + if (isset($backtrace[4]) && $backtrace[3]['function'] == 'partialStub') { + // Prepend class::function:line from 2nd element + //* NOISY-DEBUG: */ printf('[%s:%d]: partialStub() was invoked ...' . PHP_EOL, __METHOD__, __LINE__); + $message = sprintf('[%s::%s:%d]: %s', + $backtrace[4]['class'], + $backtrace[4]['function'], + (isset($backtrace[4]['line']) ? $backtrace[4]['line'] : '0'), + $message + ); + } elseif (isset($backtrace[3]) && $backtrace[2]['function'] == 'debugOutput') { + // Prepend class::function:line from 2nd element + //* NOISY-DEBUG: */ printf('[%s:%d]: debugOutput() was invoked ...' . PHP_EOL, __METHOD__, __LINE__); + $message = sprintf('[%s::%s:%d]: DEPRECATED: %s', + $backtrace[3]['class'], + $backtrace[3]['function'], + (isset($backtrace[3]['line']) ? $backtrace[3]['line'] : '0'), + $message + ); + } else { + // Prepend class::function:line from 2nd element + //* NOISY-DEBUG: */ printf('[%s:%d]: Ordinary invocation ...' . PHP_EOL, __METHOD__, __LINE__); + $message = sprintf('[%s::%s:%d]: %s', + $backtrace[2]['class'], + $backtrace[2]['function'], + (isset($backtrace[2]['line']) ? $backtrace[2]['line'] : '0'), + $message + ); + } + + // Use the output instance + $this->getOutputInstance()->outputStream($message, $stripTags); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } /** @@ -111,26 +174,187 @@ class DebugMiddleware extends BaseMiddleware implements Registerable { } /** - * This method shall send debug output which can be HTML code for the - * browser or debug lines for a log file, etc. to the registered debug - * output instance. + * Outputs a trace message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. * - * @param $outStream Data we shall 'stream' out to the world - * @param $stripTags Whether HTML tags shall be stripped out + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter */ - public final function output (string $outStream, bool $stripTags = false) { - // Is the output stream set - if (empty($outStream)) { - // @TODO Initialization phase - return; + public function traceMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + // Check parameter + //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); + if (empty($message)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (is_null($this->getOutputInstance())) { // Should not be NULL throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } - // Use the output instance - $this->getOutputInstance()->outputStream($outStream, $stripTags); + // Use debug output handler + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); + $this->output(sprintf('TRACE: %s', $message), $stripTags); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); + } + + /** + * Outputs a debug message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + */ + public function debugMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + // Check parameter + //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); + if (empty($message)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (is_null($this->getOutputInstance())) { + // Should not be NULL + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } + + // Use debug output handler + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); + $this->output(sprintf('DEBUG: %s', $message), $stripTags); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); + } + + /** + * Outputs an informational message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + */ + public function infoMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + // Check parameter + //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); + if (empty($message)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (is_null($this->getOutputInstance())) { + // Should not be NULL + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } + + // Use debug output handler + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); + $this->output(sprintf('INFO: %s', $message), $stripTags); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); + } + + /** + * Outputs a warning message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + */ + public function warningMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + // Check parameter + //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); + if (empty($message)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (is_null($this->getOutputInstance())) { + // Should not be NULL + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } + + // Use debug output handler + //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags)); + $this->output(sprintf('WARNING: %s', $message), $stripTags); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); + } + + /** + * Output a partial stub message for the caller method + * + * @param $message An optional message to display + * @return void + */ + public function partialStub (string $message = '') { + // Init variable + //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message); + $stubMessage = 'Partial stub!'; + + // Is an extra message given? + if (!empty($message)) { + // Then add it as well + $stubMessage .= ' Message: ' . $message; + } + + // Output stub message + $this->output($stubMessage); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); + } + + /** + * Outputs a deprecated message whether to debug instance (should be set!) or + * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to + * ApplicationEntryPoint::app_exit(), this would cause an endless loop. + * + * @param $message Message we shall send out... + * @param $doPrint Whether print or die here (default: print) + * @paran $stripTags Whether to strip tags (default: false) + * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws NullPointerException If this->outputInstance is NULL + * @todo Remove $doPrint parameter + * @todo When all old method invocations are fixed, renamed this do deprecatedMessage + */ + public function debugOutput (string $message, bool $doPrint = true, bool $stripTags = false) { + // Check parameter + //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); + if (empty($message)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (is_null($this->getOutputInstance())) { + // Should not be NULL + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } + + // Invoke Inner method + $this->outputMessage(sprintf('DEPRECATED: %s', $message), $doPrint, $stripTags); + + // Trace message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } }