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\NullPointerException;
8 use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
10 use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer;
13 use \InvalidArgumentException;
16 * The middlware debug output system. A *real* or concrete output class shall
17 * become registered with this middleware because the back-fall class will
18 * become deprecated soon.
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
25 * @deprecated See LoggerFactory for a more flexible approach
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class DebugMiddleware extends BaseMiddleware implements Registerable {
42 * An instance of this class
44 private static $selfInstance = NULL;
47 * Protected constructor
51 private function __construct () {
52 // Call parent constructor
53 //* NOISY-DEBUG: */ printf('[%s:%d]: CONSTRUCTED!' . PHP_EOL, __METHOD__, __LINE__);
54 parent::__construct(__CLASS__);
57 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
61 * Create a new debug output system.
62 * If no output is given this class is currently being used for back-fall.
63 * This fall-back mechanism will become deprecated very soon.
65 * @param $outputClass The class name which we shall use for
66 * registering the *real* debug output
67 * @param $className Class where a output should be created and
69 * @return $debugInstance An instance of this middleware class
70 * @throws InvalidArgumentException If a parameter has an invalid value
72 public static final function createDebugMiddleware (string $outputClass, string $className) {
74 //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s,className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $outputClass, $className);
75 if (empty($outputClass)) {
77 throw new InvalidArgumentException('Parameter "outputClass" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
78 } elseif (empty($className)) {
80 throw new InvalidArgumentException('Parameter "className" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
83 // Is a static instance there?
84 //* NOISY-DEBUG: */ printf('[%s:%d]: self::selfInstance[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype(self::$selfInstance));
85 if (is_null(self::$selfInstance)) {
86 // Create an instance if this middleware
87 self::$selfInstance = new DebugMiddleware();
90 // Is there a valid output instance provided?
91 //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s' . PHP_EOL, __METHOD__, __LINE__, $outputClass);
92 if (class_exists($outputClass) && is_null(self::$selfInstance->getOutputInstance())) {
93 // A name for a debug output class has been provided so we try to get it
94 //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing outputClass=%s ...' . PHP_EOL, __METHOD__, __LINE__, $outputClass);
95 $outputInstance = ObjectFactory::createObjectByName($outputClass);
97 // Set this as output class
98 //* NOISY-DEBUG: */ printf('[%s:%d]: outputInstance=%s' . PHP_EOL, __METHOD__, __LINE__, $outputInstance->__toString());
99 self::$selfInstance->setOutputInstance($outputInstance);
102 // Is the output class loadable and an output instance is set?
103 if (class_exists($outputClass) && !is_null(self::$selfInstance->getOutputInstance())) {
104 // Then set class name
105 //* NOISY-DEBUG: */ printf('[%s:%d]: Setting className=%s as logger class ...' . PHP_EOL, __METHOD__, __LINE__, $className);
106 self::$selfInstance->getOutputInstance()->setLoggerClassName($className);
110 //* NOISY-DEBUG: */ printf('[%s:%d]: debugInstance=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, self::$selfInstance->__toString());
111 return self::$selfInstance;
115 * This method shall send debug output which can be HTML code for the
116 * browser or debug lines for a log file, etc. to the registered debug
119 * @param $message Data we shall 'stream' out to the world
120 * @param $stripTags Whether HTML tags shall be stripped out
122 * @throws NullPointerException If this->outputInstance is NULL
124 private function output (string $message, bool $stripTags = false) {
125 // Is the output stream set
126 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
127 if (empty($message)) {
128 // @TODO Initialization phase
129 //* NOISY-DEBUG: */ printf('[%s:%d]: message is empty - EXIT!' . PHP_EOL, __METHOD__, __LINE__);
134 $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT);
136 // Is the deprecated debugOutput() or partialStub() invoked before?
137 if (isset($backtrace[4]) && $backtrace[3]['function'] == 'partialStub') {
138 // Prepend class::function:line from 2nd element
139 //* NOISY-DEBUG: */ printf('[%s:%d]: partialStub() was invoked ...' . PHP_EOL, __METHOD__, __LINE__);
140 $message = sprintf('[%s::%s:%d]: %s',
141 $backtrace[4]['class'],
142 $backtrace[4]['function'],
143 (isset($backtrace[4]['line']) ? $backtrace[4]['line'] : '0'),
146 } elseif (isset($backtrace[3]) && $backtrace[2]['function'] == 'debugOutput') {
147 // Prepend class::function:line from 2nd element
148 //* NOISY-DEBUG: */ printf('[%s:%d]: debugOutput() was invoked ...' . PHP_EOL, __METHOD__, __LINE__);
149 $message = sprintf('[%s::%s:%d]: DEPRECATED: %s',
150 $backtrace[3]['class'],
151 $backtrace[3]['function'],
152 (isset($backtrace[3]['line']) ? $backtrace[3]['line'] : '0'),
156 // Prepend class::function:line from 2nd element
157 //* NOISY-DEBUG: */ printf('[%s:%d]: Ordinary invocation ...' . PHP_EOL, __METHOD__, __LINE__);
158 $message = sprintf('[%s::%s:%d]: %s',
159 $backtrace[2]['class'],
160 $backtrace[2]['function'],
161 (isset($backtrace[2]['line']) ? $backtrace[2]['line'] : '0'),
166 // Use the output instance
167 $this->getOutputInstance()->outputStream($message, $stripTags);
170 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
174 * Getter for an instance of this class
176 * @return $selfInstance An instance of this class
178 public static final function getSelfInstance() {
179 return self::$selfInstance;
183 * Outputs a trace message whether to debug instance (should be set!) or
184 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
185 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
187 * @param $message Message we shall send out...
188 * @param $doPrint Whether print or die here (default: print)
189 * @paran $stripTags Whether to strip tags (default: false)
191 * @throws InvalidArgumentException If a parameter has an invalid value
192 * @throws NullPointerException If this->outputInstance is NULL
193 * @todo Remove $doPrint parameter
195 public function traceMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
197 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
198 if (empty($message)) {
200 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
201 } elseif (is_null($this->getOutputInstance())) {
202 // Should not be NULL
203 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
206 // Use debug output handler
207 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
208 $this->output($message, $stripTags);
211 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
215 * Outputs a debug message whether to debug instance (should be set!) or
216 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
217 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
219 * @param $message Message we shall send out...
220 * @param $doPrint Whether print or die here (default: print)
221 * @paran $stripTags Whether to strip tags (default: false)
223 * @throws InvalidArgumentException If a parameter has an invalid value
224 * @throws NullPointerException If this->outputInstance is NULL
225 * @todo Remove $doPrint parameter
227 public function debugMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
229 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
230 if (empty($message)) {
232 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
233 } elseif (is_null($this->getOutputInstance())) {
234 // Should not be NULL
235 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
238 // Use debug output handler
239 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
240 $this->output($message, $stripTags);
243 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
247 * Outputs a warning message whether to debug instance (should be set!) or
248 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
249 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
251 * @param $message Message we shall send out...
252 * @param $doPrint Whether print or die here (default: print)
253 * @paran $stripTags Whether to strip tags (default: false)
255 * @throws InvalidArgumentException If a parameter has an invalid value
256 * @throws NullPointerException If this->outputInstance is NULL
257 * @todo Remove $doPrint parameter
259 public function warningMessage (string $message, bool $doPrint = true, bool $stripTags = false) {
261 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
262 if (empty($message)) {
264 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
265 } elseif (is_null($this->getOutputInstance())) {
266 // Should not be NULL
267 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
270 // Use debug output handler
271 //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking this->output(%s,%d) ...' . PHP_EOL, __METHOD__, __LINE__, $message, intval($stripTags));
272 $this->output($message, $stripTags);
275 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
279 * Outputs a debug message whether to debug instance (should be set!) or
280 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
281 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
283 * @param $message Message we shall send out...
284 * @param $doPrint Whether print or die here (default: print)
285 * @paran $stripTags Whether to strip tags (default: false)
287 * @throws InvalidArgumentException If a parameter has an invalid value
288 * @throws NullPointerException If this->outputInstance is NULL
289 * @todo Remove $doPrint parameter
290 * @deprecated Rewrite to "new" methods above
292 public function debugOutput (string $message, bool $doPrint = true, bool $stripTags = false) {
294 //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags));
295 if (empty($message)) {
297 throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
298 } elseif (is_null($this->getOutputInstance())) {
299 // Should not be NULL
300 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
303 // Invoke new version
304 $this->debugMessage($message, $doPrint, $stripTags);
307 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);