/
debug.php
216 lines (178 loc) · 6.28 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
<?php
/*
* This software is the property of its authors.
* See the copyright.txt file for more details.
*
*/
if (!defined('debugMode'))
define('debugMode', $_SERVER['debugMode']);
// debugMessage ([$type,] $message)
function debugMessage ($message) {
$args = func_get_args();
$type = isset($args[1]) ? $args[0] : null;
$message = isset($args[1]) ? $args[1] : $message;
$backtrace = debug_backtrace();
$i = 0;
while (isset($backtrace[$i])) {
$backtraceEntry = (object) $backtrace[$i];
$i++;
if (isset($backtraceEntry->file) && realpath($backtraceEntry->file) == realpath(__FILE__))
continue;
break;
}
if (isset($backtrace[$i], $backtrace[$i]['class']))
$backtraceEntry->{'class'} = $backtrace[$i]['class'];
if (isset($backtrace[$i], $backtrace[$i]['type']))
$backtraceEntry->{'type'} = $backtrace[$i]['type'];
if (isset($backtrace[$i], $backtrace[$i]['function']))
$backtraceEntry->{'function'} = $backtrace[$i]['function'];
// format:
// info is the message type (optional)
// #5/6 is the request id / session id
// 123 is the line number
//
// [2000-01-01 18:23:54] #5 (/path/to/file:123) info
// message text
if (!isset($_SERVER['requestId']))
$_SERVER['requestId'] = randomKey(24);
if (!session_id())
session_start();
$timestamp = microtime(true);
if (!isset($GLOBALS['__7iPYslyzfKzZBtZBc7T6aglQ_debugLog']))
$GLOBALS['__7iPYslyzfKzZBtZBc7T6aglQ_debugLog'] = array();
static $counter = 0;
$GLOBALS['__7iPYslyzfKzZBtZBc7T6aglQ_debugLog'][] = (object) array(
'time' => $timestamp,
'number' => ++$counter,
'file' => isset($backtraceEntry, $backtraceEntry->file) ?
str_replace(array('\\', '/'), array('/', '/'), realpath($backtraceEntry->file)) : null,
'line' => isset($backtraceEntry, $backtraceEntry->line) ? $backtraceEntry->line : null,
'type' => isset($type) ? $type : null,
'call' => (isset($backtraceEntry->{'class'}) ? $backtraceEntry->{'class'}
. $backtraceEntry->{'type'} : '') . $backtraceEntry->{'function'} . '()',
'message' => $message,
);
if (isset($_SERVER['debugPath']))
file_put_contents($_SERVER['debugPath'],
"[" . date("Y-m-d H:i:s", $timestamp) . "." . substr(round($timestamp - floor($timestamp), 6), 2)
. "] #{$_SERVER['requestId']}/" . session_id()
. (isset($backtraceEntry->file) ? " ({$backtraceEntry->file}:{$backtraceEntry->line})" : '')
. (isset($type) ? " $type" : '') . "\n" . " "
. (isset($backtraceEntry->{'class'}) ? $backtraceEntry->{'class'} . $backtraceEntry->{'type'} : '')
. $backtraceEntry->{'function'} . "(): $message\n", FILE_APPEND | LOCK_EX);
}
function debugObject ($object) {
if (!isset($GLOBALS['__7iPYslyzfKzZBtZBc7T6aglQ_debugObjects']))
$GLOBALS['__7iPYslyzfKzZBtZBc7T6aglQ_debugObjects'] = (object) array();
$id = randomKey(24);
$GLOBALS['__7iPYslyzfKzZBtZBc7T6aglQ_debugObjects']->$id = $object;
if (isset($_SERVER['debugObjectsPath']))
file_put_contents($_SERVER['debugObjectsPath'] . "/$id.json", json_encode($object), FILE_APPEND | LOCK_EX);
return "[object: $id]";
}
function debugDump ($symbol) {
if ($symbol === null)
return 'null';
if (in_array(gettype($symbol), array('string', 'integer', 'double', 'float', 'array')))
return json_encode($symbol);
if (is_object($symbol))
return get_class($symbol) . ' {}';
assertTrue(false, gettype($symbol));
}
function debugUntraceableMessage ($message) {
$timestamp = microtime(true);
$GLOBALS['__7iPYslyzfKzZBtZBc7T6aglQ_debugLog'][] = (object) array(
'time' => $timestamp,
'message' => $message,
);
if (isset($_SERVER['debugPath']))
file_put_contents($_SERVER['debugPath'],
"[" . date("Y-m-d H:i:s", $timestamp) . "." . substr(round($timestamp - floor($timestamp), 6), 2) . "]" . "\n" .
" $message\n", FILE_APPEND | LOCK_EX);
}
function debugStopwatch_start ($name) {
debugStopwatch('start', $name);
}
function debugStopwatch_end ($name) {
debugStopwatch('end', $name);
}
// TODO: move this to optimizer
function debugStopwatch ($do, $name) {
static $stopwatches = null;
if (!isset($stopwatches))
$stopwatches = (object) array();
static $isRegistered = false;
if (!$isRegistered) {
debug_onShutdown(function () use ($stopwatches) {
foreach ($stopwatches as $stopwatch)
debugUntraceableMessage("stopwatch {$stopwatch->name} measured {$stopwatch->total}"
. " seconds in {$stopwatch->calls} measurements");
});
$isRegistered = true;
}
if (!isset($stopwatches->$name))
$stopwatches->$name = (object) array(
'name' => $name,
'total' => 0,
'calls' => 0,
'stack' => array(),
);
switch ($do) {
case 'all':
return $stopwatches;
case 'start':
$stopwatches->$name->stack[] = microtime(true);
break;
case 'end':
$time = array_pop($stopwatches->$name->stack);
if (count($stopwatches->$name->stack) == 0) {
$stopwatches->$name->calls++;
$stopwatches->$name->total += (microtime(true) - $time);
}
break;
default:
assertTrue(false);
}
}
function debugBacktrace () {
return new debugBacktrace_class();
}
class debugBacktrace_class {
function __construct () {
$this->backtrace = debug_backtrace();
}
function __toString () {
foreach ($this->backtrace as $k => $v)
$this->backtrace[$k]['object'] = null;
ob_start();
var_dump($this->backtrace);
return ob_get_clean();
}
}
function debug_onShutdown ($callback) {
static $list = array();
if ($callback == 'P28dLKkZZPjKsQzjkIELGRpC') {
foreach ($list as $f)
call_user_func($f);
return;
}
assertTrue(is_callable($callback));
$list[] = $callback;
}
function debug_instance_shutdown () {
debug_onShutdown('P28dLKkZZPjKsQzjkIELGRpC');
debugMessage("instance shutdown");
debugMessage("peak memory usage: " . memory_get_peak_usage() . ' bytes');
}
if (debugMode) {
static $debugInit;
if (!isset($debugInit)) {
$url = null;
if (isset($_SERVER['HTTP_HOST']))
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http')
. "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
debugMessage("instance startup" . ($url ? " on url: $url" : ''));
register_shutdown_function('debug_instance_shutdown');
}
$debugInit = true;
}