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\Middleware\BaseMiddleware;
10 use Org\Mxchange\CoreFramework\Registry\Registerable;
11 use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer;
14 use \InvalidArgumentException;
17 * The middlware debug output system. A *real* or concrete output class shall
18 * become registered with this middleware because the back-fall class will
19 * become deprecated soon.
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
26 * @deprecated See LoggerFactory for a more flexible approach
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class DebugMiddleware extends BaseMiddleware implements Registerable {
43 * An instance of this class
45 private static $selfInstance = NULL;
48 * Protected constructor
52 private function __construct () {
53 // Call parent constructor
54 //* NOISY-DEBUG: */ printf('[%s:%d]: CONSTRUCTED!' . PHP_EOL, __METHOD__, __LINE__);
55 parent::__construct(__CLASS__);
58 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
62 * Create a new debug output system.
63 * If no output is given this class is currently being used for back-fall.
64 * This fall-back mechanism will become deprecated very soon.
66 * @param $outputClass The class name which we shall use for
67 * registering the *real* debug output
68 * @param $className Class where a output should be created and
70 * @return $debugInstance An instance of this middleware class
71 * @throws InvalidArgumentException If a parameter has an invalid value
73 public static final function createDebugMiddleware (string $outputClass, string $className) {
75 //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s,className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $outputClass, $className);
76 if (empty($outputClass)) {
78 throw new InvalidArgumentException('Parameter "outputClass" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
79 } elseif (empty($className)) {
81 throw new InvalidArgumentException('Parameter "className" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
84 // Is a static instance there?
85 //* NOISY-DEBUG: */ printf('[%s:%d]: self::selfInstance[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype(self::$selfInstance));
86 if (is_null(self::$selfInstance)) {
87 // Create an instance if this middleware
88 self::$selfInstance = new DebugMiddleware();
91 // Is there a valid output instance provided?
92 //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s' . PHP_EOL, __METHOD__, __LINE__, $outputClass);
93 if (class_exists($outputClass) && is_null(self::$selfInstance->getOutputInstance())) {
94 // A name for a debug output class has been provided so we try to get it
95 //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing outputClass=%s ...' . PHP_EOL, __METHOD__, __LINE__, $outputClass);
96 $outputInstance = ObjectFactory::createObjectByName($outputClass);
98 // Set this as output class
99 //* NOISY-DEBUG: */ printf('[%s:%d]: outputInstance=%s' . PHP_EOL, __METHOD__, __LINE__, $outputInstance->__toString());
100 self::$selfInstance->setOutputInstance($outputInstance);
103 // Is the output class loadable and an output instance is set?
104 if (class_exists($outputClass) && !is_null(self::$selfInstance->getOutputInstance())) {
105 // Then set class name
106 //* NOISY-DEBUG: */ printf('[%s:%d]: Setting className=%s as logger class ...' . PHP_EOL, __METHOD__, __LINE__, $className);
107 self::$selfInstance->getOutputInstance()->setLoggerClassName($className);
111 //* NOISY-DEBUG: */ printf('[%s:%d]: debugInstance=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, self::$selfInstance->__toString());
112 return self::$selfInstance;
116 * This method shall send debug output which can be HTML code for the
117 * browser or debug lines for a log file, etc. to the registered debug
120 * @param $message Data we shall 'stream' out to the world
121 * @param $stripTags Whether HTML tags shall be stripped out
123 * @throws NullPointerException If this->outputInstance is NULL
125 private function output (string $message, bool $stripTags = false) {
127 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
128 $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT);
130 // Is the deprecated debugOutput() or partialStub() invoked before?
131 if (isset($backtrace[4]) && $backtrace[3]['function'] == 'partialStub') {
132 // Prepend class::function:line from 2nd element
133 //* NOISY-DEBUG: */ printf('[%s:%d]: partialStub() was invoked ...' . PHP_EOL, __METHOD__, __LINE__);
134 $message = sprintf('[%s::%s:%d]: %s',
135 $backtrace[4]['class'],
136 $backtrace[4]['function'],
137 (isset($backtrace[4]['line']) ? $backtrace[4]['line'] : '0'),
140 } elseif (isset($backtrace[3]) && $backtrace[2]['function'] == 'debugOutput') {
141 // Prepend class::function:line from 2nd element
142 //* NOISY-DEBUG: */ printf('[%s:%d]: debugOutput() was invoked ...' . PHP_EOL, __METHOD__, __LINE__);
143 $message = sprintf('[%s::%s:%d]: DEPRECATED: %s',
144 $backtrace[3]['class'],
145 $backtrace[3]['function'],
146 (isset($backtrace[3]['line']) ? $backtrace[3]['line'] : '0'),
150 // Prepend class::function:line from 2nd element
151 //* NOISY-DEBUG: */ printf('[%s:%d]: Ordinary invocation ...' . PHP_EOL, __METHOD__, __LINE__);
152 $message = sprintf('[%s::%s:%d]: %s',
153 $backtrace[2]['class'],
154 $backtrace[2]['function'],
155 (isset($backtrace[2]['line']) ? $backtrace[2]['line'] : '0'),
160 // Use the output instance
161 $this->getOutputInstance()->outputStream($message, $stripTags);
164 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
168 * Getter for an instance of this class
170 * @return $selfInstance An instance of this class
172 public static final function getSelfInstance() {
173 return self::$selfInstance;
177 * Outputs a trace message whether to debug instance (should be set!) or
178 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
179 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
181 * @param $message Message we shall send out...
182 * @param $doPrint Whether print or die here (default: print)
183 * @paran $stripTags Whether to strip tags (default: false)
185 * @throws InvalidArgumentException If a parameter has an invalid value
186 * @throws NullPointerException If this->outputInstance is NULL
187 * @todo Remove $doPrint parameter
189 public function traceMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
191 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
192 if (empty($message)) {
194 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
195 } elseif (is_null($this->getOutputInstance())) {
196 // Should not be NULL
197 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
200 // Use debug output handler
201 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
202 $this->output(sprintf('TRACE: %s', $message), $stripTags);
205 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
209 * Outputs a debug message whether to debug instance (should be set!) or
210 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
211 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
213 * @param $message Message we shall send out...
214 * @param $doPrint Whether print or die here (default: print)
215 * @paran $stripTags Whether to strip tags (default: false)
217 * @throws InvalidArgumentException If a parameter has an invalid value
218 * @throws NullPointerException If this->outputInstance is NULL
219 * @todo Remove $doPrint parameter
221 public function debugMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
223 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
224 if (empty($message)) {
226 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
227 } elseif (is_null($this->getOutputInstance())) {
228 // Should not be NULL
229 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
232 // Use debug output handler
233 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
234 $this->output(sprintf('DEBUG: %s', $message), $stripTags);
237 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
241 * Outputs an informational message whether to debug instance (should be set!) or
242 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
243 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
245 * @param $message Message we shall send out...
246 * @param $doPrint Whether print or die here (default: print)
247 * @paran $stripTags Whether to strip tags (default: false)
249 * @throws InvalidArgumentException If a parameter has an invalid value
250 * @throws NullPointerException If this->outputInstance is NULL
251 * @todo Remove $doPrint parameter
253 public function infoMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
255 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
256 if (empty($message)) {
258 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
259 } elseif (is_null($this->getOutputInstance())) {
260 // Should not be NULL
261 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
264 // Use debug output handler
265 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
266 $this->output(sprintf('INFO: %s', $message), $stripTags);
269 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
273 * Outputs a warning message whether to debug instance (should be set!) or
274 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
275 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
277 * @param $message Message we shall send out...
278 * @param $doPrint Whether print or die here (default: print)
279 * @paran $stripTags Whether to strip tags (default: false)
281 * @throws InvalidArgumentException If a parameter has an invalid value
282 * @throws NullPointerException If this->outputInstance is NULL
283 * @todo Remove $doPrint parameter
285 public function warningMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
287 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
288 if (empty($message)) {
290 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
291 } elseif (is_null($this->getOutputInstance())) {
292 // Should not be NULL
293 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
296 // Use debug output handler
297 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
298 $this->output(sprintf('WARNING: %s', $message), $stripTags);
301 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
305 * Output a partial stub message for the caller method
307 * @param $message An optional message to display
310 public function partialStub (string $message = '') {
312 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message);
313 $stubMessage = 'Partial stub!';
315 // Is an extra message given?
316 if (!empty($message)) {
317 // Then add it as well
318 $stubMessage .= ' Message: ' . $message;
321 // Output stub message
322 $this->output($stubMessage);
325 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
329 * Outputs a deprecated message whether to debug instance (should be set!) or
330 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
331 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
333 * @param $message Message we shall send out...
334 * @param $doPrint Whether print or die here (default: print)
335 * @paran $stripTags Whether to strip tags (default: false)
337 * @throws InvalidArgumentException If a parameter has an invalid value
338 * @throws NullPointerException If this->outputInstance is NULL
339 * @todo Remove $doPrint parameter
340 * @todo When all old method invocations are fixed, renamed this do deprecatedMessage
342 public function debugOutput (string $message, bool $doPrint = true, bool $stripTags = false) {
344 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
345 if (empty($message)) {
347 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
348 } elseif (is_null($this->getOutputInstance())) {
349 // Should not be NULL
350 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
353 // Invoke Inner method
354 $this->outputMessage(sprintf('DEPRECATED: %s', $message), $doPrint, $stripTags);
357 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);