-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
96 lines (86 loc) · 2.69 KB
/
Logger.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
<?php namespace Devtools;
use ErrorException;
abstract class Logger extends Observer\BaseObserver
{
public function __construct()
{
error_reporting(-1);
set_exception_handler(
array($this, 'exceptionHandler')
);
set_error_handler(
array($this, 'errorHandler')
);
}
public abstract function write(
$content, $result = null
);
public function exceptionHandler($e)
{
$this->write(
$e->getMessage() . "\n" . self::getExceptionTraceAsString($e)
);
}
public function errorHandler($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
private static function getExceptionTraceAsString($exception)
{
$rtn = "";
$count = 0;
foreach ($exception->getTrace() as $frame) {
$args = "";
if (isset($frame['args'])) {
$args = array();
foreach ($frame['args'] as $arg) {
if (is_string($arg)) {
$args[] = "'" . $arg . "'";
} elseif (is_null($arg)) {
$args[] = 'NULL';
} elseif (is_bool($arg)) {
$args[] = ($arg) ? "true" : "false";
} elseif (is_object($arg) || is_array($arg)) {
try {
$args[] = serialize($arg);
} catch (\PDOException $e) {
$args[] = var_export($arg, true);
}
} elseif (is_resource($arg)) {
$args[] = get_resource_type($arg);
} else {
$args[] = $arg;
}
}
$args = join(", ", $args);
}
foreach (array('file', 'line', 'function') as $type) {
$frame[$type] = isset($frame[$type]) ? $frame[$type] : '';
}
$rtn .= sprintf(
"#%s %s(%s): %s(%s)\n",
$count,
$frame['file'],
$frame['line'],
$frame['function'],
$args
);
$count++;
}
return $rtn;
}
public function update(\SplSubject $subject)
{
$status = $subject->getStatus();
if (!in_array(gettype($status), array("object", "resource"))
) {
$this->write($status);
}
if (is_a($status, 'Devtools\Assertion')) {
$this->write(
$status->message,
$status->test
);
}
}
}