/
debug.php
353 lines (313 loc) · 9.66 KB
/
debug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* @package midcom
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
use Doctrine\Common\Util\Debug;
/**
* This is a debugger class.
*
* Helps in debugging your code. It lets you decide which messages are logged into the
* logfile by setting loglevels for the debugger and for each message.
*
* There are five loglevel constants you can use when setting the loglevel or when
* logging messages:
*
* - MIDCOM_LOG_DEBUG
* - MIDCOM_LOG_INFO
* - MIDCOM_LOG_WARN
* - MIDCOM_LOG_ERROR
* - MIDCOM_LOG_CRIT
*
* This file declares shortcuts like debug_add (see below).
*
* @package midcom
*/
class midcom_debug
{
/**
* Logfile name
*
* @var string
*/
private $_filename;
/**
* Current loglevel
*
* @var int
*/
private $_loglevel;
/**
* All available loglevels
*
* @var array
*/
private $_loglevels = [
MIDCOM_LOG_DEBUG => "debug",
MIDCOM_LOG_INFO => "info",
MIDCOM_LOG_WARN => "warn",
MIDCOM_LOG_ERROR => "error",
MIDCOM_LOG_CRIT => "critical"
];
/**
* Standard constructor
*/
public function __construct($filename = null)
{
if (null === $filename) {
$filename = midcom::get()->config->get('log_filename');
}
$this->_filename = $filename;
$this->_loglevel = midcom::get()->config->get('log_level');
}
/**
* Set log level
*
* @param int $loglevel New log level
*/
public function set_loglevel($loglevel)
{
$this->_loglevel = $loglevel;
}
/**
* Get log level
*
* @return int
*/
public function get_loglevel()
{
return $this->_loglevel;
}
public function log_php_error($loglevel = MIDCOM_LOG_DEBUG)
{
$error = error_get_last();
if (!empty($error['message'])) {
$this->log('Last PHP error was: ' . $error['message'], $loglevel);
}
}
/**
* Log a message
*
* @param string $message The message to be logged
* @param int $loglevel The log level
*/
public function log($message, $loglevel = MIDCOM_LOG_DEBUG)
{
if (!$this->check_level($loglevel)) {
return;
}
$file = fopen($this->_filename, 'a+');
if (function_exists('xdebug_memory_usage')) {
static $lastmem = 0;
$curmem = xdebug_memory_usage();
$delta = $curmem - $lastmem;
$lastmem = $curmem;
$prefix = sprintf("%s (%012.9f, %9s, %7s):\t",
date('M d Y H:i:s'),
xdebug_time_index(),
number_format($curmem, 0, ',', '.'),
number_format($delta, 0, ',', '.')
);
} else {
$prefix = date('M d Y H:i:s') . "\t";
}
if (array_key_exists($loglevel, $this->_loglevels)) {
$prefix .= '[' . $this->_loglevels[$loglevel] . '] ';
}
//find the proper caller
$prefix .= $this->_get_caller();
fputs($file, $prefix . trim($message) . "\n");
fclose($file);
}
private function check_level($loglevel)
{
return ($this->_loglevel >= $loglevel);
}
private function _get_caller()
{
$return = '';
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
while ($bt) {
$caller = array_shift($bt);
if ( array_key_exists('class', $caller)
&& $caller['class'] == 'midcom_debug') {
continue;
}
if ( !array_key_exists('function', $bt[0])
|| $bt[0]['function'] != 'require') {
$caller = array_shift($bt);
}
break;
}
if (array_key_exists('class', $caller)) {
$return .= $caller['class'] . '::';
}
if ( array_key_exists('function', $caller)
&& substr($caller['function'], 0, 6) != 'debug_') {
$return .= $caller['function'] . ': ';
} else {
$return .= $caller['file'] . ' (' . $caller['line']. '): ';
}
return $return;
}
/**
* Dump a variable
*
* @param string $message The message to be logged
* @param mixed $variable The variable to be logged
* @param int $loglevel The log level
*/
public function print_r($message, $variable, $loglevel = MIDCOM_LOG_DEBUG)
{
if (!$this->check_level($loglevel)) {
return;
}
// This is mainly for midgard objects, because print_r on Entities can
// choke the system pretty badly
ob_start();
Debug::dump($variable);
$varstring = ob_get_contents();
ob_end_clean();
$this->log(trim($message) . ' ' . $varstring, $loglevel);
}
/**
* Dump stack trace, only working when XDebug is present.
*
* @param string $message The message to be logged
* @param int $loglevel The log level
* @link http://www.xdebug.org/ xdebug.org
*/
public function print_function_stack($message, $loglevel = MIDCOM_LOG_DEBUG)
{
if (!$this->check_level($loglevel)) {
return;
}
if (function_exists('xdebug_get_function_stack')) {
$stack = array_reverse(xdebug_get_function_stack());
} else {
$stack = debug_backtrace(false);
}
//the last two levels are already inside the debugging system, so skip those
array_shift($stack);
array_shift($stack);
$stacktrace = "";
foreach ($stack as $number => $frame) {
$stacktrace .= $number + 1;
if (isset($frame['file'])) {
$stacktrace .= ": {$frame['file']}:{$frame['line']} ";
}
if (array_key_exists('class', $frame)) {
if (!array_key_exists('function', $frame)) {
// workaround for what is most likely a bug in xdebug 2.4.rc3 and/or PHP 7.0.3
continue;
}
$stacktrace .= "{$frame['class']}::{$frame['function']}";
} elseif (array_key_exists('function', $frame)) {
$stacktrace .= $frame['function'];
} else {
$stacktrace .= 'require, include or eval';
}
$stacktrace .= "\n";
}
$this->log(trim($message) . "\n{$stacktrace}", $loglevel);
}
/**
* Dump a variable's type
*
* @param string $message The message to be logged
* @param mixed $variable The variable of which the type should be logged
* @param int $loglevel The log level
*/
public function print_type($message, $variable, $loglevel = MIDCOM_LOG_DEBUG)
{
if (!$this->check_level($loglevel)) {
return;
}
$type = gettype($variable);
if ($type == "object") {
$type .= ": " . get_class($variable);
}
$this->log(trim($message) . "\nVariable Type: $type", $loglevel);
}
/**
* Dump the current memory usage and the delta to the last call of this function.
* Useful for tracking memory leaks.
*
* Format will be:
*
* $curmem (delta $delta): $message
*
* @param string $message The message to be logged
* @param int $loglevel The log level
*/
public function print_dump_mem($message, $loglevel = MIDCOM_LOG_DEBUG)
{
if (!$this->check_level($loglevel)) {
return;
}
if (!function_exists('memory_get_usage')) {
return false;
}
static $lastmem = 0;
$curmem = memory_get_usage();
$delta = $curmem - $lastmem;
$lastmem = $curmem;
$curmem = str_pad(number_format($curmem), 10, " ", STR_PAD_LEFT);
$delta = str_pad(number_format($delta), 10, " ", STR_PAD_LEFT);
$this->log("{$curmem} (delta {$delta}): {$message}", $loglevel);
}
}
/**
* Shortcut: Log a message
*
* @param string $message The message to be logged
* @param int $loglevel The log level
*/
function debug_add($message, $loglevel = MIDCOM_LOG_DEBUG)
{
midcom::get()->debug->log($message, $loglevel);
}
/**
* Shortcut: Dump a variable
*
* @param string $message The message to be logged
* @param mixed $variable The variable to be logged
* @param int $loglevel The log level
*/
function debug_print_r($message, $variable, $loglevel = MIDCOM_LOG_DEBUG)
{
midcom::get()->debug->print_r($message, $variable, $loglevel);
}
/**
* Shortcut: Create a stack trace and dump it.
*
* @param string $message The message to be logged
* @param int $loglevel The log level
*/
function debug_print_function_stack($message, $loglevel = MIDCOM_LOG_DEBUG)
{
midcom::get()->debug->print_function_stack($message, $loglevel);
}
/**
* Shortcut: Dump a variable's type
*
* @param string $message The message to be logged
* @param mixed $variable The variable of which the type should be logged
* @param int $loglevel The log level
*/
function debug_print_type($message, $variable, $loglevel = MIDCOM_LOG_DEBUG)
{
midcom::get()->debug->print_type($message, $variable, $loglevel);
}
/**
* Shortcut: Dump the current memory usage and the delta to the last call of this function.
*
* @param string $message The message to be logged
* @param int $loglevel The log level
*/
function debug_dump_mem($message, $loglevel = MIDCOM_LOG_DEBUG)
{
midcom::get()->debug->print_dump_mem($message, $loglevel);
}