~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.1.0RC/Nette/Diagnostics/FireLogger.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * This file is part of the Nette Framework (http://nette.org)
 
5
 *
 
6
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 
7
 *
 
8
 * For the full copyright and license information, please view
 
9
 * the file license.txt that was distributed with this source code.
 
10
 */
 
11
 
 
12
namespace Nette\Diagnostics;
 
13
 
 
14
use Nette;
 
15
 
 
16
 
 
17
/**
 
18
 * FireLogger console logger.
 
19
 *
 
20
 * @see http://firelogger.binaryage.com
 
21
 * @author     David Grudl
 
22
 */
 
23
class FireLogger extends Nette\Object
 
24
{
 
25
        const DEBUG = 'debug',
 
26
                INFO = 'info',
 
27
                WARNING = 'warning',
 
28
                ERROR = 'error',
 
29
                CRITICAL = 'critical';
 
30
 
 
31
        private static $payload = array('logs' => array());
 
32
 
 
33
 
 
34
        /**
 
35
         * Sends message to FireLogger console.
 
36
         * @param  mixed
 
37
         * @return bool    was successful?
 
38
         */
 
39
        public static function log($message, $priority = self::DEBUG)
 
40
        {
 
41
                if (!isset($_SERVER['HTTP_X_FIRELOGGER']) || headers_sent()) {
 
42
                        return FALSE;
 
43
                }
 
44
 
 
45
                $item = array(
 
46
                        'name' => 'PHP',
 
47
                        'level' => $priority,
 
48
                        'order' => count(self::$payload['logs']),
 
49
                        'time' => str_pad(number_format((microtime(TRUE) - Debugger::$time) * 1000, 1, '.', ' '), 8, '0', STR_PAD_LEFT) . ' ms',
 
50
                        'template' => '',
 
51
                        'message' => '',
 
52
                        'style' => 'background:#767ab6',
 
53
                );
 
54
 
 
55
                $args = func_get_args();
 
56
                if (isset($args[0]) && is_string($args[0])) {
 
57
                        $item['template'] = array_shift($args);
 
58
                }
 
59
 
 
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')
 
65
                        ) {
 
66
                                unset($trace[0]);
 
67
                        }
 
68
 
 
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();
 
74
 
 
75
                } else {
 
76
                        $trace = debug_backtrace();
 
77
                        if (isset($trace[1]['class']) && $trace[1]['class'] === 'Nette\Diagnostics\Debugger'
 
78
                                && ($trace[1]['function'] === 'fireLog')
 
79
                        ) {
 
80
                                unset($trace[0]);
 
81
                        }
 
82
 
 
83
                        foreach ($trace as $frame) {
 
84
                                if (isset($frame['file']) && is_file($frame['file'])) {
 
85
                                        $item['pathname'] = $frame['file'];
 
86
                                        $item['lineno'] = $frame['line'];
 
87
                                        break;
 
88
                                }
 
89
                        }
 
90
                }
 
91
 
 
92
                $item['exc_info'] = array('', '', array());
 
93
                $item['exc_frames'] = array();
 
94
 
 
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'];
 
99
                }
 
100
 
 
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);
 
103
                }
 
104
 
 
105
                $item['args'] = $args;
 
106
 
 
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");
 
110
                }
 
111
                return TRUE;
 
112
        }
 
113
 
 
114
 
 
115
        /**
 
116
         * Dump implementation for JSON.
 
117
         * @param  mixed  variable to dump
 
118
         * @param  int    current recursion level
 
119
         * @return string
 
120
         */
 
121
        private static function jsonDump(& $var, $level = 0)
 
122
        {
 
123
                if (is_bool($var) || is_null($var) || is_int($var) || is_float($var)) {
 
124
                        return $var;
 
125
 
 
126
                } elseif (is_string($var)) {
 
127
                        if (Debugger::$maxLen && strlen($var) > Debugger::$maxLen) {
 
128
                                $var = substr($var, 0, Debugger::$maxLen) . " \xE2\x80\xA6 ";
 
129
                        }
 
130
                        return Nette\Utils\Strings::fixEncoding($var);
 
131
 
 
132
                } elseif (is_array($var)) {
 
133
                        static $marker;
 
134
                        if ($marker === NULL) {
 
135
                                $marker = uniqid("\x00", TRUE);
 
136
                        }
 
137
                        if (isset($var[$marker])) {
 
138
                                return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
 
139
 
 
140
                        } elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth) {
 
141
                                $var[$marker] = TRUE;
 
142
                                $res = array();
 
143
                                foreach ($var as $k => & $v) {
 
144
                                        if ($k !== $marker) {
 
145
                                                $res[self::jsonDump($k)] = self::jsonDump($v, $level + 1);
 
146
                                        }
 
147
                                }
 
148
                                unset($var[$marker]);
 
149
                                return $res;
 
150
 
 
151
                        } else {
 
152
                                return " \xE2\x80\xA6 ";
 
153
                        }
 
154
 
 
155
                } elseif (is_object($var)) {
 
156
                        $arr = (array) $var;
 
157
                        static $list = array();
 
158
                        if (in_array($var, $list, TRUE)) {
 
159
                                return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
 
160
 
 
161
                        } elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth) {
 
162
                                $list[] = $var;
 
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);
 
167
                                        }
 
168
                                        $res[self::jsonDump($k)] = self::jsonDump($v, $level + 1);
 
169
                                }
 
170
                                array_pop($list);
 
171
                                return $res;
 
172
 
 
173
                        } else {
 
174
                                return " \xE2\x80\xA6 ";
 
175
                        }
 
176
 
 
177
                } elseif (is_resource($var)) {
 
178
                        return "resource " . get_resource_type($var);
 
179
 
 
180
                } else {
 
181
                        return "unknown type";
 
182
                }
 
183
        }
 
184
 
 
185
}