PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 11ms RepoModel.GetById 30ms app.codeStats 0ms

/lib/external/tracy/src/Tracy/FireLogger.php

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