1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xmf; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Debugging tools for developers |
16
|
|
|
* |
17
|
|
|
* @category Xmf\Debug |
18
|
|
|
* @package Xmf |
19
|
|
|
* @author trabis <[email protected]> |
20
|
|
|
* @author Richard Griffith <[email protected]> |
21
|
|
|
* @copyright 2011-2018 XOOPS Project (https://xoops.org) |
22
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
23
|
|
|
* @link https://xoops.org |
24
|
|
|
*/ |
25
|
|
|
class Debug extends \Kint |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* associative array of timers |
29
|
|
|
* |
30
|
|
|
* @var float[] |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
private static $times = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* indexed array of timer data in form |
37
|
|
|
* array('label' => string, 'start' => float, 'elapsed' => float) |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
private static $timerQueue = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* associative array of timer labels |
46
|
|
|
* |
47
|
|
|
* @var string[] |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
private static $timerLabels = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Force dump via debug.log event, if possible |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
private static $eventDumper = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* doOnce - do some local housekeeping on first use. Any method needing this |
60
|
|
|
* assist just calls every time, the one time logic is all here. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
private static function doOnce() |
65
|
|
|
{ |
66
|
|
|
static $done; |
67
|
|
|
if (true !== $done) { |
68
|
|
|
$done = true; |
69
|
|
|
$class = get_called_class(); |
70
|
|
|
parent::$aliases[] = [$class, 'dump']; |
71
|
|
|
parent::$aliases[] = [$class, 'backtrace']; |
72
|
|
|
\Kint_Renderer_Rich::$theme = 'aante-light.css'; // options: 'original' (default), 'solarized', 'solarized-dark' and 'aante-light' |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Dump one or more variables |
78
|
|
|
* |
79
|
|
|
* @param mixed $data variable(s) to dump |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public static function dump($data = null) |
84
|
|
|
{ |
85
|
|
|
$args = func_get_args(); |
86
|
|
|
|
87
|
|
|
$events = \Xoops::getInstance()->events(); |
88
|
|
|
$eventName = 'debug.log'; |
89
|
|
|
|
90
|
|
|
if (self::$eventDumper && $events->hasListeners($eventName)) { |
91
|
|
|
foreach ($args as $var) { |
92
|
|
|
$events->triggerEvent($eventName, $var); |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
static::doOnce(); |
96
|
|
|
forward_static_call_array(array('parent', 'dump'), $args); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Dump one or more variables to the log |
102
|
|
|
* |
103
|
|
|
* @param mixed $data variable(s) to dump |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public static function log($data = null) |
108
|
|
|
{ |
109
|
|
|
$args = func_get_args(); |
110
|
|
|
|
111
|
|
|
$events = \Xoops::getInstance()->events(); |
112
|
|
|
$eventName = 'debug.log'; |
113
|
|
|
|
114
|
|
|
foreach ($args as $var) { |
115
|
|
|
$events->triggerEvent($eventName, $var); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* dump using debug.log event if possible (i.e. in debugbar, instead of in page) |
121
|
|
|
* |
122
|
|
|
* @param bool $value true to use event |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public static function useEventDumper($value = true) |
127
|
|
|
{ |
128
|
|
|
self::$eventDumper = (bool) $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Display debug backtrace |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public static function backtrace() |
137
|
|
|
{ |
138
|
|
|
static::dump(debug_backtrace()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Start a timer |
143
|
|
|
* |
144
|
|
|
* @param string $name unique name for timer |
145
|
|
|
* @param string|null $label optional label for this timer |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
public static function startTimer($name, $label = null) |
150
|
|
|
{ |
151
|
|
|
$events = \Xoops::getInstance()->events(); |
152
|
|
|
$var = array($name); |
153
|
|
|
$var[] = empty($label) ? $name : $label; |
154
|
|
|
$eventName = 'debug.timer.start'; |
155
|
|
|
if ($events->hasListeners($eventName)) { |
156
|
|
|
$events->triggerEvent($eventName, $var); |
157
|
|
|
} else { |
158
|
|
|
self::$times[$name] = microtime(true); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Stop a timer |
164
|
|
|
* |
165
|
|
|
* @param string $name unique name for timer |
166
|
|
|
* |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public static function stopTimer($name) |
170
|
|
|
{ |
171
|
|
|
$events = \Xoops::getInstance()->events(); |
172
|
|
|
$eventName = 'debug.timer.stop'; |
173
|
|
|
if ($events->hasListeners($eventName)) { |
174
|
|
|
$events->triggerEvent($eventName, $name); |
175
|
|
|
} else { |
176
|
|
|
echo $name . ' - ' . (int)(microtime(true) - self::$times[$name]) . " \n"; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Start a queued timer. Queued timers are stored and only dumped by request. |
182
|
|
|
* This makes them useful in recording timing when immediate output is not |
183
|
|
|
* possible practical, such as early system startup activities. Timers can be |
184
|
|
|
* queued at any point once the Xmf\Debug class can be loaded then dumped |
185
|
|
|
* when system facilities are available. |
186
|
|
|
* |
187
|
|
|
* @param string $name unique name for timer |
188
|
|
|
* @param string|null $label optional label for this timer |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public static function startQueuedTimer($name, $label = null) |
193
|
|
|
{ |
194
|
|
|
self::$times[$name] = microtime(true); |
195
|
|
|
self::$timerLabels[$name] = empty($label) ? $name : $label; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Stop a queued timer |
200
|
|
|
* |
201
|
|
|
* @param string $name unique name for timer |
202
|
|
|
* |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public static function stopQueuedTimer($name) |
206
|
|
|
{ |
207
|
|
|
if (isset(self::$timerLabels[$name]) && isset(self::$times[$name])) { |
208
|
|
|
$queueItem = array( |
209
|
|
|
'label' => self::$timerLabels[$name], |
210
|
|
|
'start' => self::$times[$name], |
211
|
|
|
'elapsed' => microtime(true) - self::$times[$name], |
212
|
|
|
); |
213
|
|
|
self::$timerQueue[] = $queueItem; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* dump and queued timer data and reset the queue |
219
|
|
|
* |
220
|
|
|
* Note: The DebugBar logger will add any unprocessed queue data to its |
221
|
|
|
* timeline automatically, if you use queued timers and don't call this. |
222
|
|
|
* |
223
|
|
|
* @param boolean $returnOnly if true do not dump queue, only return it |
224
|
|
|
* |
225
|
|
|
* @return array of time data see \Xmf\Debug::$timerQueue |
226
|
|
|
*/ |
227
|
|
|
public static function dumpQueuedTimers($returnOnly = false) |
228
|
|
|
{ |
229
|
|
|
$queue = self::$timerQueue; |
230
|
|
|
self::$timerQueue = array(); |
231
|
|
|
if (!$returnOnly) { |
232
|
|
|
static::dump($queue); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $queue; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* start_trace - turn on xdebug trace |
240
|
|
|
* |
241
|
|
|
* Requires xdebug extension |
242
|
|
|
* |
243
|
|
|
* @param string $tracefile file name for trace file |
244
|
|
|
* @param string $collect_params argument for ini_set('xdebug.collect_params',?) |
245
|
|
|
* Controls display of parameters in trace output |
246
|
|
|
* @param string $collect_return argument for ini_set('xdebug.collect_return',?) |
247
|
|
|
* Controls display of function return value in trace |
248
|
|
|
* |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public static function startTrace($tracefile = '', $collect_params = '3', $collect_return = 'On') |
252
|
|
|
{ |
253
|
|
|
if (function_exists('xdebug_start_trace')) { |
254
|
|
|
ini_set('xdebug.collect_params', $collect_params); |
255
|
|
|
ini_set('xdebug.collect_return', $collect_return); |
256
|
|
|
if ($tracefile == '') { |
257
|
|
|
$tracefile = \XoopsBaseConfig::get('var-path') . '/logs/php_trace'; |
258
|
|
|
} |
259
|
|
|
xdebug_start_trace($tracefile); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* stop_trace - turn off xdebug trace |
265
|
|
|
* |
266
|
|
|
* Requires xdebug extension |
267
|
|
|
* |
268
|
|
|
* @return void |
269
|
|
|
*/ |
270
|
|
|
public static function stopTrace() |
271
|
|
|
{ |
272
|
|
|
if (function_exists('xdebug_stop_trace')) { |
273
|
|
|
xdebug_stop_trace(); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|