1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom |
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
10
|
|
|
use Symfony\Component\VarDumper\Dumper\CliDumper; |
11
|
|
|
use Monolog\Logger; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is a debugger class. |
15
|
|
|
* |
16
|
|
|
* Helps in debugging your code. It lets you decide which messages are logged into the |
17
|
|
|
* logfile by setting loglevels for the debugger and for each message. |
18
|
|
|
* |
19
|
|
|
* There are five loglevel constants you can use when setting the loglevel or when |
20
|
|
|
* logging messages: |
21
|
|
|
* |
22
|
|
|
* - MIDCOM_LOG_DEBUG |
23
|
|
|
* - MIDCOM_LOG_INFO |
24
|
|
|
* - MIDCOM_LOG_WARN |
25
|
|
|
* - MIDCOM_LOG_ERROR |
26
|
|
|
* - MIDCOM_LOG_CRIT |
27
|
|
|
* |
28
|
|
|
* @package midcom |
29
|
|
|
*/ |
30
|
|
|
class midcom_debug |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Logger |
34
|
|
|
*/ |
35
|
|
|
private $logger; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Standard constructor |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(Logger $logger) |
41
|
|
|
{ |
42
|
3 |
|
$this->logger = $logger; |
43
|
3 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Converts MidCOM log levels to Monolog |
47
|
|
|
*/ |
48
|
691 |
|
public static function convert_level(int $level) : int |
49
|
|
|
{ |
50
|
|
|
$level_map = [ |
51
|
691 |
|
MIDCOM_LOG_DEBUG => Logger::DEBUG, |
52
|
|
|
MIDCOM_LOG_INFO => Logger::INFO, |
53
|
|
|
MIDCOM_LOG_WARN => Logger::WARNING, |
54
|
|
|
MIDCOM_LOG_ERROR => Logger::ERROR, |
55
|
|
|
MIDCOM_LOG_CRIT => Logger::CRITICAL |
56
|
|
|
]; |
57
|
691 |
|
return $level_map[$level] ?? $level; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function log_php_error(int $loglevel) |
61
|
|
|
{ |
62
|
|
|
$error = error_get_last(); |
63
|
|
|
if (!empty($error['message'])) { |
64
|
|
|
$this->log('Last PHP error was: ' . $error['message'], $loglevel); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Log a message |
70
|
|
|
*/ |
71
|
690 |
|
public function log(string $message, int $loglevel = MIDCOM_LOG_DEBUG) |
72
|
|
|
{ |
73
|
690 |
|
$bt = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1); |
74
|
690 |
|
$this->logger->pushProcessor(function(array $record) use ($bt) { |
75
|
690 |
|
return $this->get_caller($record, $bt); |
76
|
690 |
|
}); |
77
|
690 |
|
$this->logger->addRecord(self::convert_level($loglevel), trim($message)); |
78
|
690 |
|
$this->logger->popProcessor(); |
79
|
690 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @internal |
83
|
|
|
*/ |
84
|
693 |
|
public function get_caller(array $record, array $bt) : array |
85
|
|
|
{ |
86
|
693 |
|
$record['extra']['caller'] = ''; |
87
|
693 |
|
while ($bt) { |
88
|
693 |
|
$caller = array_shift($bt); |
89
|
693 |
|
if (!in_array($caller['class'] ?? '', [midcom_debug::class])) { |
90
|
693 |
|
break; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
693 |
|
$record['extra']['caller'] .= $caller['file'] . ':' . $caller['line'] . ''; |
|
|
|
|
95
|
693 |
|
return $record; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Dump a variable |
100
|
|
|
*/ |
101
|
23 |
|
public function print_r(string $message, $variable, int $loglevel = MIDCOM_LOG_DEBUG) |
102
|
|
|
{ |
103
|
23 |
|
$this->logger->pushProcessor(function(array $record) use ($variable) { |
104
|
23 |
|
$cloner = new VarCloner(); |
105
|
23 |
|
$dumper = new CliDumper(); |
106
|
23 |
|
$record['message'] .= ' ' . $dumper->dump($cloner->cloneVar($variable), true); |
107
|
23 |
|
return $record; |
108
|
23 |
|
}); |
109
|
23 |
|
$this->log($message, $loglevel); |
110
|
23 |
|
$this->logger->popProcessor(); |
111
|
23 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Dump stack trace |
115
|
|
|
*/ |
116
|
26 |
|
public function print_function_stack(string $message, int $loglevel = MIDCOM_LOG_DEBUG) |
117
|
|
|
{ |
118
|
26 |
|
$this->logger->pushProcessor(function(array $record) { |
119
|
|
|
// the last four levels are already inside the debugging system, so skip those |
120
|
26 |
|
$stack = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 4); |
121
|
26 |
|
$stacktrace = ""; |
122
|
26 |
|
foreach ($stack as $number => $frame) { |
123
|
26 |
|
$stacktrace .= $number + 1; |
124
|
26 |
|
if (isset($frame['file'])) { |
125
|
26 |
|
$stacktrace .= ": {$frame['file']}:{$frame['line']} "; |
126
|
|
|
} |
127
|
26 |
|
if (array_key_exists('class', $frame)) { |
128
|
26 |
|
$stacktrace .= "{$frame['class']}::{$frame['function']}"; |
129
|
26 |
|
} elseif (array_key_exists('function', $frame)) { |
130
|
26 |
|
$stacktrace .= $frame['function']; |
131
|
|
|
} else { |
132
|
|
|
$stacktrace .= 'require, include or eval'; |
133
|
|
|
} |
134
|
26 |
|
$stacktrace .= "\n"; |
135
|
|
|
} |
136
|
|
|
|
137
|
26 |
|
$record['message'] .= "\n{$stacktrace}"; |
138
|
26 |
|
return $record; |
139
|
26 |
|
}); |
140
|
26 |
|
$this->log($message, $loglevel); |
141
|
26 |
|
$this->logger->popProcessor(); |
142
|
26 |
|
} |
143
|
|
|
} |
144
|
|
|
|