4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Diagnostics;
18
* FireLogger console logger.
20
* @see http://firelogger.binaryage.com
23
class FireLogger extends Nette\Object
25
const DEBUG = 'debug',
29
CRITICAL = 'critical';
31
private static $payload = array('logs' => array());
35
* Sends message to FireLogger console.
37
* @return bool was successful?
39
public static function log($message, $priority = self::DEBUG)
41
if (!isset($_SERVER['HTTP_X_FIRELOGGER']) || headers_sent()) {
48
'order' => count(self::$payload['logs']),
49
'time' => str_pad(number_format((microtime(TRUE) - Debugger::$time) * 1000, 1, '.', ' '), 8, '0', STR_PAD_LEFT) . ' ms',
52
'style' => 'background:#767ab6',
55
$args = func_get_args();
56
if (isset($args[0]) && is_string($args[0])) {
57
$item['template'] = array_shift($args);
60
if (isset($args[0]) && $args[0] instanceof \Exception) {
61
$e = array_shift($args);
62
$trace = $e->getTrace();
63
if (isset($trace[0]['class']) && $trace[0]['class'] === 'Nette\Diagnostics\Debugger'
64
&& ($trace[0]['function'] === '_shutdownHandler' || $trace[0]['function'] === '_errorHandler')
69
$file = str_replace(dirname(dirname(dirname($e->getFile()))), "\xE2\x80\xA6", $e->getFile());
70
$item['template'] = ($e instanceof \ErrorException ? '' : get_class($e) . ': ')
71
. $e->getMessage() . ($e->getCode() ? ' #' . $e->getCode() : '') . ' in ' . $file . ':' . $e->getLine();
72
$item['pathname'] = $e->getFile();
73
$item['lineno'] = $e->getLine();
76
$trace = debug_backtrace();
77
if (isset($trace[1]['class']) && $trace[1]['class'] === 'Nette\Diagnostics\Debugger'
78
&& ($trace[1]['function'] === 'fireLog')
83
foreach ($trace as $frame) {
84
if (isset($frame['file']) && is_file($frame['file'])) {
85
$item['pathname'] = $frame['file'];
86
$item['lineno'] = $frame['line'];
92
$item['exc_info'] = array('', '', array());
93
$item['exc_frames'] = array();
95
foreach ($trace as $frame) {
96
$frame += array('file' => NULL, 'line' => NULL, 'class' => NULL, 'type' => NULL, 'function' => NULL, 'object' => NULL, 'args' => NULL);
97
$item['exc_info'][2][] = array($frame['file'], $frame['line'], "$frame[class]$frame[type]$frame[function]", $frame['object']);
98
$item['exc_frames'][] = $frame['args'];
101
if (isset($args[0]) && in_array($args[0], array(self::DEBUG, self::INFO, self::WARNING, self::ERROR, self::CRITICAL), TRUE)) {
102
$item['level'] = array_shift($args);
105
$item['args'] = $args;
107
self::$payload['logs'][] = self::jsonDump($item, -1);
108
foreach (str_split(base64_encode(@json_encode(self::$payload)), 4990) as $k => $v) { // intentionally @
109
header("FireLogger-de11e-$k:$v");
116
* Dump implementation for JSON.
117
* @param mixed variable to dump
118
* @param int current recursion level
121
private static function jsonDump(& $var, $level = 0)
123
if (is_bool($var) || is_null($var) || is_int($var) || is_float($var)) {
126
} elseif (is_string($var)) {
127
if (Debugger::$maxLen && strlen($var) > Debugger::$maxLen) {
128
$var = substr($var, 0, Debugger::$maxLen) . " \xE2\x80\xA6 ";
130
return Nette\Utils\Strings::fixEncoding($var);
132
} elseif (is_array($var)) {
134
if ($marker === NULL) {
135
$marker = uniqid("\x00", TRUE);
137
if (isset($var[$marker])) {
138
return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
140
} elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth) {
141
$var[$marker] = TRUE;
143
foreach ($var as $k => & $v) {
144
if ($k !== $marker) {
145
$res[self::jsonDump($k)] = self::jsonDump($v, $level + 1);
148
unset($var[$marker]);
152
return " \xE2\x80\xA6 ";
155
} elseif (is_object($var)) {
157
static $list = array();
158
if (in_array($var, $list, TRUE)) {
159
return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
161
} elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth) {
163
$res = array("\x00" => '(object) ' . get_class($var));
164
foreach ($arr as $k => & $v) {
165
if ($k[0] === "\x00") {
166
$k = substr($k, strrpos($k, "\x00") + 1);
168
$res[self::jsonDump($k)] = self::jsonDump($v, $level + 1);
174
return " \xE2\x80\xA6 ";
177
} elseif (is_resource($var)) {
178
return "resource " . get_resource_type($var);
181
return "unknown type";