|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace suda\framework; |
|
4
|
|
|
|
|
5
|
|
|
use function constant; |
|
6
|
|
|
use ErrorException; |
|
7
|
|
|
use function is_writable; |
|
8
|
|
|
use ReflectionException; |
|
9
|
|
|
use function restore_error_handler; |
|
10
|
|
|
use function restore_exception_handler; |
|
11
|
|
|
use function set_error_handler; |
|
12
|
|
|
use function set_exception_handler; |
|
13
|
|
|
|
|
14
|
|
|
use suda\framework\debug\Debug; |
|
15
|
|
|
use suda\framework\runnable\Runnable; |
|
16
|
|
|
use suda\framework\context\PHPContext; |
|
17
|
|
|
use suda\framework\filesystem\FileSystem; |
|
18
|
|
|
use suda\framework\debug\log\LoggerInterface; |
|
19
|
|
|
use suda\framework\debug\log\logger\FileLogger; |
|
20
|
|
|
use suda\framework\debug\log\logger\NullLogger; |
|
21
|
|
|
use Throwable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* 调试器 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Debugger extends Debug |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* 环境内容 |
|
30
|
|
|
* |
|
31
|
|
|
* @var PHPContext |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $context; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 初始化 |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->setLogger(new NullLogger); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* 注册错误处理函数 |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
|
|
public function register() |
|
48
|
|
|
{ |
|
49
|
|
|
register_shutdown_function([$this, 'uncaughtFatalError']); |
|
50
|
|
|
set_error_handler([$this, 'uncaughtError']); |
|
51
|
|
|
set_exception_handler([$this, 'uncaughtException']); |
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* 创建调式工具 |
|
57
|
|
|
* |
|
58
|
|
|
* @param PHPContext $context |
|
59
|
|
|
* @return Debugger |
|
60
|
|
|
*/ |
|
61
|
|
|
public function load(PHPContext $context): Debugger |
|
62
|
|
|
{ |
|
63
|
|
|
$this->applyConfig([ |
|
64
|
|
|
'start-time' => constant('SUDA_START_TIME'), |
|
65
|
|
|
'start-memory' => constant('SUDA_START_MEMORY'), |
|
66
|
|
|
]); |
|
67
|
|
|
$this->context = $context; |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* 获取原始记录器 |
|
73
|
|
|
* |
|
74
|
|
|
* @return LoggerInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getLogger(): LoggerInterface |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->logger; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* 末异常处理 |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function uncaughtFatalError() |
|
87
|
|
|
{ |
|
88
|
|
|
if (function_exists('fastcgi_finish_request')) { |
|
89
|
|
|
fastcgi_finish_request(); |
|
90
|
|
|
} |
|
91
|
|
|
if ($e = error_get_last()) { |
|
92
|
|
|
$isFatalError = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING; |
|
93
|
|
|
if ($e['type'] === ($e['type'] & $isFatalError)) { |
|
94
|
|
|
$errorHander = set_error_handler(null); |
|
95
|
|
|
if ($errorHander !== null) { |
|
96
|
|
|
$errorHander($e['type'], $e['message'], $e['file'], $e['line']); |
|
97
|
|
|
} |
|
98
|
|
|
restore_error_handler(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* 异常托管 |
|
105
|
|
|
* |
|
106
|
|
|
* @param Throwable $exception |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function uncaughtException($exception) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->error($exception->getMessage(), ['exception' => $exception]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* 错误托管 |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $errno |
|
118
|
|
|
* @param string $errstr |
|
119
|
|
|
* @param string $errfile |
|
120
|
|
|
* @param int $errline |
|
121
|
|
|
* @return void |
|
122
|
|
|
* @throws ErrorException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function uncaughtError($errno, $errstr, $errfile, $errline) |
|
125
|
|
|
{ |
|
126
|
|
|
$severity = |
|
127
|
|
|
1 * E_ERROR | |
|
128
|
|
|
1 * E_WARNING | |
|
129
|
|
|
0 * E_PARSE | |
|
130
|
|
|
1 * E_NOTICE | |
|
131
|
|
|
0 * E_CORE_ERROR | |
|
132
|
|
|
1 * E_CORE_WARNING | |
|
133
|
|
|
0 * E_COMPILE_ERROR | |
|
134
|
|
|
1 * E_COMPILE_WARNING | |
|
135
|
|
|
0 * E_USER_ERROR | |
|
136
|
|
|
1 * E_USER_WARNING | |
|
137
|
|
|
1 * E_USER_NOTICE | |
|
138
|
|
|
0 * E_STRICT | |
|
139
|
|
|
0 * E_RECOVERABLE_ERROR | |
|
140
|
|
|
0 * E_DEPRECATED | |
|
141
|
|
|
0 * E_USER_DEPRECATED; |
|
142
|
|
|
$exception = new ErrorException($errstr, $errno, $errno, $errfile, $errline); |
|
143
|
|
|
if ($exception->getSeverity() & $severity === 0) { |
|
144
|
|
|
throw $exception; |
|
145
|
|
|
} else { |
|
146
|
|
|
$exceptionHandler = set_exception_handler(null); |
|
147
|
|
|
if ($exceptionHandler !== null) { |
|
148
|
|
|
$exceptionHandler($exception); |
|
149
|
|
|
} |
|
150
|
|
|
restore_exception_handler(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getDefaultConfig(): array |
|
155
|
|
|
{ |
|
156
|
|
|
return [ |
|
157
|
|
|
'log-format' => '%time-format% - %memory-format% [%level%] %file%:%line% %message%', |
|
158
|
|
|
'start-time' => 0, |
|
159
|
|
|
'start-memory' => 0, |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* 设置忽略前缀 |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getIgnoreTraces(): array |
|
169
|
|
|
{ |
|
170
|
|
|
$trace = parent::getIgnoreTraces(); |
|
171
|
|
|
$trace[] = __FILE__; |
|
172
|
|
|
return $trace; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|