1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Tracy;
9:
10:
11: 12: 13: 14: 15: 16:
17: class FireLogger implements ILogger
18: {
19:
20: public $maxDepth = 3;
21:
22:
23: public $maxLength = 150;
24:
25:
26: private $payload = ['logs' => []];
27:
28:
29: 30: 31: 32: 33:
34: public function log($message, $priority = self::DEBUG)
35: {
36: if (!isset($_SERVER['HTTP_X_FIRELOGGER']) || headers_sent()) {
37: return false;
38: }
39:
40: $item = [
41: 'name' => 'PHP',
42: 'level' => $priority,
43: 'order' => count($this->payload['logs']),
44: 'time' => str_pad(number_format((microtime(true) - Debugger::$time) * 1000, 1, '.', ' '), 8, '0', STR_PAD_LEFT) . ' ms',
45: 'template' => '',
46: 'message' => '',
47: 'style' => 'background:#767ab6',
48: ];
49:
50: $args = func_get_args();
51: if (isset($args[0]) && is_string($args[0])) {
52: $item['template'] = array_shift($args);
53: }
54:
55: if (isset($args[0]) && ($args[0] instanceof \Exception || $args[0] instanceof \Throwable)) {
56: $e = array_shift($args);
57: $trace = $e->getTrace();
58: if (
59: isset($trace[0]['class'])
60: && $trace[0]['class'] === 'Tracy\Debugger'
61: && ($trace[0]['function'] === 'shutdownHandler' || $trace[0]['function'] === 'errorHandler')
62: ) {
63: unset($trace[0]);
64: }
65:
66: $file = str_replace(dirname(dirname(dirname($e->getFile()))), "\xE2\x80\xA6", $e->getFile());
67: $item['template'] = ($e instanceof \ErrorException ? '' : Helpers::getClass($e) . ': ')
68: . $e->getMessage() . ($e->getCode() ? ' #' . $e->getCode() : '') . ' in ' . $file . ':' . $e->getLine();
69: $item['pathname'] = $e->getFile();
70: $item['lineno'] = $e->getLine();
71:
72: } else {
73: $trace = debug_backtrace();
74: if (
75: isset($trace[1]['class'])
76: && $trace[1]['class'] === 'Tracy\Debugger'
77: && ($trace[1]['function'] === 'fireLog')
78: ) {
79: unset($trace[0]);
80: }
81:
82: foreach ($trace as $frame) {
83: if (isset($frame['file']) && is_file($frame['file'])) {
84: $item['pathname'] = $frame['file'];
85: $item['lineno'] = $frame['line'];
86: break;
87: }
88: }
89: }
90:
91: $item['exc_info'] = ['', '', []];
92: $item['exc_frames'] = [];
93:
94: foreach ($trace as $frame) {
95: $frame += ['file' => null, 'line' => null, 'class' => null, 'type' => null, 'function' => null, 'object' => null, 'args' => null];
96: $item['exc_info'][2][] = [$frame['file'], $frame['line'], "$frame[class]$frame[type]$frame[function]", $frame['object']];
97: $item['exc_frames'][] = $frame['args'];
98: }
99:
100: if (isset($args[0]) && in_array($args[0], [self::DEBUG, self::INFO, self::WARNING, self::ERROR, self::CRITICAL], true)) {
101: $item['level'] = array_shift($args);
102: }
103:
104: $item['args'] = $args;
105:
106: $this->payload['logs'][] = $this->jsonDump($item, -1);
107: foreach (str_split(base64_encode(json_encode($this->payload)), 4990) as $k => $v) {
108: header("FireLogger-de11e-$k:$v");
109: }
110: return true;
111: }
112:
113:
114: 115: 116: 117: 118: 119:
120: private function jsonDump(&$var, $level = 0)
121: {
122: if (is_bool($var) || $var === null || is_int($var) || is_float($var)) {
123: return $var;
124:
125: } elseif (is_string($var)) {
126: return Dumper::encodeString($var, $this->maxLength);
127:
128: } elseif (is_array($var)) {
129: static $marker;
130: if ($marker === null) {
131: $marker = uniqid("\x00", true);
132: }
133: if (isset($var[$marker])) {
134: return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
135:
136: } elseif ($level < $this->maxDepth || !$this->maxDepth) {
137: $var[$marker] = true;
138: $res = [];
139: foreach ($var as $k => &$v) {
140: if ($k !== $marker) {
141: $res[$this->jsonDump($k)] = $this->jsonDump($v, $level + 1);
142: }
143: }
144: unset($var[$marker]);
145: return $res;
146:
147: } else {
148: return " \xE2\x80\xA6 ";
149: }
150:
151: } elseif (is_object($var)) {
152: $arr = (array) $var;
153: static $list = [];
154: if (in_array($var, $list, true)) {
155: return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
156:
157: } elseif ($level < $this->maxDepth || !$this->maxDepth) {
158: $list[] = $var;
159: $res = ["\x00" => '(object) ' . Helpers::getClass($var)];
160: foreach ($arr as $k => &$v) {
161: if (isset($k[0]) && $k[0] === "\x00") {
162: $k = substr($k, strrpos($k, "\x00") + 1);
163: }
164: $res[$this->jsonDump($k)] = $this->jsonDump($v, $level + 1);
165: }
166: array_pop($list);
167: return $res;
168:
169: } else {
170: return " \xE2\x80\xA6 ";
171: }
172:
173: } elseif (is_resource($var)) {
174: return 'resource ' . get_resource_type($var);
175:
176: } else {
177: return 'unknown type';
178: }
179: }
180: }
181: