3 namespace Org\Mxchange\CoreFramework\Middleware\Debug;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Logging\Logger;
10 use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer;
15 use \InvalidArgumentException;
18 * The middlware debug output system. A *real* or concrete output class shall
19 * become registered with this middleware because the back-fall class will
20 * become deprecated soon.
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
27 * @deprecated See LoggerFactory for a more flexible approach
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class DebugMiddleware extends BaseMiddleware implements Registerable, Logger {
44 * An instance of this class
46 private static $selfInstance = NULL;
49 * Protected constructor
53 private function __construct () {
54 // Call parent constructor
55 //* NOISY-DEBUG: */ printf('[%s:%d]: CONSTRUCTED!' . PHP_EOL, __METHOD__, __LINE__);
56 parent::__construct(__CLASS__);
59 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
63 * Create a new debug output system.
64 * If no output is given this class is currently being used for back-fall.
65 * This fall-back mechanism will become deprecated very soon.
67 * @param $outputClass The class name which we shall use for
68 * registering the *real* debug output
69 * @param $className Class where a output should be created and
71 * @return $debugInstance An instance of this middleware class
72 * @throws InvalidArgumentException If a parameter has an invalid value
74 public static final function createDebugMiddleware (string $outputClass, string $className) {
76 //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s,className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $outputClass, $className);
77 if (empty($outputClass)) {
79 throw new InvalidArgumentException('Parameter "outputClass" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
80 } elseif (empty($className)) {
82 throw new InvalidArgumentException('Parameter "className" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
85 // Is a static instance there?
86 //* NOISY-DEBUG: */ printf('[%s:%d]: self::selfInstance[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype(self::$selfInstance));
87 if (is_null(self::$selfInstance)) {
88 // Create an instance if this middleware
89 self::$selfInstance = new DebugMiddleware();
92 // Is there a valid output instance provided?
93 //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s' . PHP_EOL, __METHOD__, __LINE__, $outputClass);
94 if (class_exists($outputClass) && is_null(self::$selfInstance->getOutputInstance())) {
95 // A name for a debug output class has been provided so we try to get it
96 //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing outputClass=%s ...' . PHP_EOL, __METHOD__, __LINE__, $outputClass);
97 $outputInstance = ObjectFactory::createObjectByName($outputClass);
99 // Set this as output class
100 //* NOISY-DEBUG: */ printf('[%s:%d]: outputInstance=%s' . PHP_EOL, __METHOD__, __LINE__, $outputInstance->__toString());
101 self::$selfInstance->setOutputInstance($outputInstance);
104 // Is the output class loadable and an output instance is set?
105 if (class_exists($outputClass) && !is_null(self::$selfInstance->getOutputInstance())) {
106 // Then set class name
107 //* NOISY-DEBUG: */ printf('[%s:%d]: Setting className=%s as logger class ...' . PHP_EOL, __METHOD__, __LINE__, $className);
108 self::$selfInstance->getOutputInstance()->setLoggerClassName($className);
112 //* NOISY-DEBUG: */ printf('[%s:%d]: debugInstance=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, self::$selfInstance->__toString());
113 return self::$selfInstance;
117 * This method shall send debug output which can be HTML code for the
118 * browser or debug lines for a log file, etc. to the registered debug
121 * @param $message Data we shall 'stream' out to the world
122 * @param $stripTags Whether HTML tags shall be stripped out
124 * @throws NullPointerException If this->outputInstance is NULL
126 private function outputMessage (string $logLevel, string $message, bool $stripTags = false) {
128 //* NOISY-DEBUG: */ printf('[%s:%d]: logLevel=%s,message=%s,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $logLevel, $message, intval($stripTags));
129 $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT);
131 // Is the deprecated debugOutput() or partialStub() invoked before?
132 if (isset($backtrace[4]) && $backtrace[3]['function'] == 'partialStub') {
133 // Prepend class::function:line from 2nd element
134 //* NOISY-DEBUG: */ printf('[%s:%d]: partialStub() was invoked ...' . PHP_EOL, __METHOD__, __LINE__);
135 $message = sprintf('[%s::%s:%d]: [%s] %s',
136 $backtrace[4]['class'],
137 $backtrace[4]['function'],
138 (isset($backtrace[4]['line']) ? $backtrace[4]['line'] : '0'),
142 } elseif (isset($backtrace[3]) && $backtrace[2]['function'] == 'debugOutput') {
143 // Prepend class::function:line from 2nd element
144 //* NOISY-DEBUG: */ printf('[%s:%d]: debugOutput() was invoked ...' . PHP_EOL, __METHOD__, __LINE__);
145 $message = sprintf('[%s::%s:%d]: [%s] %s',
146 $backtrace[3]['class'],
147 $backtrace[3]['function'],
148 (isset($backtrace[3]['line']) ? $backtrace[3]['line'] : '0'),
153 // Prepend class::function:line from 2nd element
154 //* NOISY-DEBUG: */ printf('[%s:%d]: Ordinary invocation ...' . PHP_EOL, __METHOD__, __LINE__);
155 $message = sprintf('[%s::%s:%d]: [%s] %s',
156 $backtrace[2]['class'],
157 $backtrace[2]['function'],
158 (isset($backtrace[2]['line']) ? $backtrace[2]['line'] : '0'),
164 // Use the output instance
165 $this->getOutputInstance()->outputStream($message, $stripTags);
168 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
172 * Getter for an instance of this class
174 * @return $selfInstance An instance of this class
176 public static final function getSelfInstance() {
177 return self::$selfInstance;
181 * Outputs a trace message whether to debug instance (should be set!) or
182 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
183 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
185 * @param $message Message we shall send out...
186 * @param $doPrint Whether print or die here (default: print)
187 * @paran $stripTags Whether to strip tags (default: false)
189 * @throws InvalidArgumentException If a parameter has an invalid value
190 * @throws NullPointerException If this->outputInstance is NULL
191 * @todo Remove $doPrint parameter
193 public function traceMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
195 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
196 if (empty($message)) {
198 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
199 } elseif (is_null($this->getOutputInstance())) {
200 // Should not be NULL
201 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
204 // Use debug output handler
205 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_TRACE, $message, intval($stripTags));
206 $this->outputMessage(Logger::LOGGER_LEVEL_TRACE, $message, $stripTags);
209 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
213 * Outputs a debug message whether to debug instance (should be set!) or
214 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
215 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
217 * @param $message Message we shall send out...
218 * @param $doPrint Whether print or die here (default: print)
219 * @paran $stripTags Whether to strip tags (default: false)
221 * @throws InvalidArgumentException If a parameter has an invalid value
222 * @throws NullPointerException If this->outputInstance is NULL
223 * @todo Remove $doPrint parameter
225 public function debugMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
227 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
228 if (empty($message)) {
230 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
231 } elseif (is_null($this->getOutputInstance())) {
232 // Should not be NULL
233 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
236 // Use debug output handler
237 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
238 $this->outputMessage(Logger::LOGGER_LEVEL_DEBUG, $message, $stripTags);
241 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
245 * Outputs an informational message whether to debug instance (should be set!) or
246 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
247 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
249 * @param $message Message we shall send out...
250 * @param $doPrint Whether print or die here (default: print)
251 * @paran $stripTags Whether to strip tags (default: false)
253 * @throws InvalidArgumentException If a parameter has an invalid value
254 * @throws NullPointerException If this->outputInstance is NULL
255 * @todo Remove $doPrint parameter
257 public function infoMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
259 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
260 if (empty($message)) {
262 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
263 } elseif (is_null($this->getOutputInstance())) {
264 // Should not be NULL
265 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
268 // Use debug output handler
269 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_INFO, $message, intval($stripTags));
270 $this->outputMessage(Logger::LOGGER_LEVEL_INFO, $message, $stripTags);
273 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
277 * Outputs a warning message whether to debug instance (should be set!) or
278 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
279 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
281 * @param $message Message we shall send out...
282 * @param $doPrint Whether print or die here (default: print)
283 * @paran $stripTags Whether to strip tags (default: false)
285 * @throws InvalidArgumentException If a parameter has an invalid value
286 * @throws NullPointerException If this->outputInstance is NULL
287 * @todo Remove $doPrint parameter
289 public function warningMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
291 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
292 if (empty($message)) {
294 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
295 } elseif (is_null($this->getOutputInstance())) {
296 // Should not be NULL
297 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
300 // Use debug output handler
301 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_WARNING, $message, intval($stripTags));
302 $this->outputMessage(Logger::LOGGER_LEVEL_WARNING, $message, $stripTags);
305 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
309 * Output a partial stub message for the caller method
311 * @param $message An optional message to display
314 public function partialStub (string $message = '') {
316 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message);
317 $stubMessage = 'Partial stub!';
319 // Is an extra message given?
320 if (!empty($message)) {
321 // Then add it as well
322 $stubMessage .= ' Message: ' . $message;
325 // Output stub message
326 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_WARNING, $subMessage);
327 $this->outputMessage(Logger::LOGGER_LEVEL_WARNING, $stubMessage);
330 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
334 * Outputs a deprecated message whether to debug instance (should be set!) or
335 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
336 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
338 * @param $message Message we shall send out...
339 * @param $doPrint Whether print or die here (default: print)
340 * @paran $stripTags Whether to strip tags (default: false)
342 * @throws InvalidArgumentException If a parameter has an invalid value
343 * @throws NullPointerException If this->outputInstance is NULL
344 * @todo Remove $doPrint parameter
345 * @todo When all old method invocations are fixed, renamed this do deprecatedMessage
347 public function debugOutput (string $message, bool $doPrint = true, bool $stripTags = false) {
349 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
350 if (empty($message)) {
352 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
353 } elseif (is_null($this->getOutputInstance())) {
354 // Should not be NULL
355 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
358 // Invoke Inner method
359 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->outputMessage(%s,%s) ...' . PHP_EOL, __METHOD__, __LINE__, Logger::LOGGER_LEVEL_DEPRECATED, $subMessage);
360 $this->outputMessage(Logger::LOGGER_LEVEL_DEPRECATED, $message, $doPrint, $stripTags);
363 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);