DebugError.php
4.23 KB
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
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 14/12/7
* Time: 下午9:52
*/
namespace Hood\Debug;
class DebugError
{
/**
* 错误code
* @var integer
*/
protected $errorLevel;
public function exceptionHandler($exception)
{
$errorList = array(
'error_message' => $exception->getMessage()
);
foreach ($exception->getTrace() as $key => $val) {
$errorList['error_list'][] = array(
'file' => empty($val['file']) ? '↑' : $val['file'],
'function' => $val['function'],
'line' => empty($val['line']) ? '' : $val['line'],
'class' => empty($val['class']) ? '' : $val['class'],
'type' => empty($val['type']) ? '' : $val['type'],
'args' => empty($val['args']) ? array() : $val['args']
);
}
if (defined('APPLICATION_DEBUG_FORMAT') == false || APPLICATION_DEBUG_FORMAT == 'html') {
$this->debugToHtml($errorList, 'exception');
} else {
$this->debugToString($errorList, 'exception');
}
}
public function errorHandler($errno, $errstr, $errfile, $errline)
{
$errorList = array(
'error_message' => $errstr
);
foreach (debug_backtrace() as $key => $val) {
$errorList['error_list'][] = array(
'file' => empty($val['file']) ? __FILE__ : $val['file'],
'function' => $val['function'],
'line' => empty($val['line']) ? __LINE__ : $val['line'],
'class' => empty($val['class']) ? __CLASS__ : $val['class'],
'type' => empty($val['type']) ? '' : $val['type'],
'args' => empty($val['args']) ? array() : $val['args']
);
}
if (defined('APPLICATION_DEBUG_FORMAT') == false || APPLICATION_DEBUG_FORMAT == 'html') {
$this->debugToHtml($errorList);
} else {
$this->debugToString($errorList);
}
}
private function debugToString($errorList, $type = 'error')
{
}
private function debugToHtml($errorList, $type = 'error')
{
$html = <<<EOD
<style type="text/css">
.warning{
background:#eeeeec;
border-color:#eeeeec;
}
strong {
font-weight:bolder;
font-style:normal;
margin-right: 0.5rem;
font-size: 1.125rem;
}
th ,td{
padding:4px 10px;
}
</style>
EOD;
$html .= '<div class="warning"><table style="width: 100%;"><tbody>';
$html .= '<tr><th colspan="2" align="left" bgcolor="#f57900" style="height: 40px;padding-left: 10px;"><span style="background-color: #cc0000; color: #fce94f; font-size: x-large;">( ! )</span> ' . $errorList['error_message'] . '</th></tr>';
$html .= '<tr><th align="left" bgcolor="#e9b96e" colspan="2">Call Stack</th></tr>';
$html .= '<tr><td>Function</td><td>Location</td></td></tr>';
if (count($errorList['error_list']) > 1) {
unset($errorList['error_list'][0]);
}
foreach ($errorList['error_list'] as $key => $val) {
$args = '(..)';
if ($type == 'exception') {
$args = '(' . implode(',', $val['args']) . ')';
}
$function = $val['class'] . $val['type'] . $val['function'] . $args;
if (empty($val['type'])) {
$function = '<a href="http://php.net/manual/en/function.' . str_replace('_', '-', $val['function']) . '.php" target="_blank">' . $val['function'] . $args . '</a>';
}
$line = '';
if (!empty($val['line'])) {
$line .= ' ( ' . $val['line'] . ' ) ';
}
$html .= '<tr><td style="color: #0000BB">' . $function . '</td><td>' . $val['file'] . $line . '</td></tr>';
}
$html .= '</tbody></table></div><br />';
echo $html;
}
public function setUserDebug()
{
$this->errorLevel = ini_get('error_reporting');
error_reporting(0);
set_exception_handler(array($this, 'exceptionHandler'));
set_error_handler(array($this, 'errorHandler'));
}
public function restoreUserDebug()
{
error_reporting($this->errorLevel);
restore_error_handler();
restore_exception_handler();
}
}