1: <?php
2: /**
3: * Horde Log package
4: *
5: * @author Mike Naberezny <mike@maintainable.com>
6: * @author Chuck Hagenbuch <chuck@horde.org>
7: * @category Horde
8: * @license http://www.horde.org/licenses/bsd BSD
9: * @package Log
10: * @subpackage Handlers
11: */
12:
13: /**
14: * @author Mike Naberezny <mike@maintainable.com>
15: * @author Chuck Hagenbuch <chuck@horde.org>
16: * @category Horde
17: * @license http://www.horde.org/licenses/bsd BSD
18: * @package Log
19: * @subpackage Handlers
20: */
21: class Horde_Log_Handler_Firebug extends Horde_Log_Handler_Base
22: {
23: /**
24: * Formats the log message before writing.
25: *
26: * @var Horde_Log_Formatter
27: */
28: protected $_formatter;
29:
30: /**
31: * Options to be set by setOption().
32: *
33: * @var array
34: */
35: protected $_options = array(
36: 'buffering' => false,
37: 'ident' => ''
38: );
39:
40: /**
41: * Array of buffered output.
42: *
43: * @var string
44: */
45: protected $_buffer = array();
46:
47: /**
48: * Mapping of log priorities to Firebug methods.
49: *
50: * @var array
51: */
52: protected static $_methods = array(
53: Horde_Log::EMERG => 'error',
54: Horde_Log::ALERT => 'error',
55: Horde_Log::CRIT => 'error',
56: Horde_Log::ERR => 'error',
57: Horde_Log::WARN => 'warn',
58: Horde_Log::NOTICE => 'info',
59: Horde_Log::INFO => 'info',
60: Horde_Log::DEBUG => 'debug',
61: );
62:
63: /**
64: * Class Constructor
65: *
66: * @param Horde_Log_Formatter $formatter Log formatter.
67: */
68: public function __construct(Horde_Log_Formatter $formatter = null)
69: {
70: $this->_formatter = is_null($formatter)
71: ? new Horde_Log_Formatter_Simple()
72: : $formatter;
73: }
74:
75: /**
76: * Write a message to the firebug console. This function really just
77: * writes the message to the buffer. If buffering is enabled, the
78: * message won't be output until the buffer is flushed. If
79: * buffering is not enabled, the buffer will be flushed
80: * immediately.
81: *
82: * @param array $event Log event.
83: *
84: * @return boolean True.
85: */
86: public function write($event)
87: {
88: if (!empty($this->_options['ident'])) {
89: $event['message'] = $this->_options['ident'] . ' ' . $event['message'];
90: }
91:
92: $this->_buffer[] = $event;
93:
94: if (empty($this->_options['buffering'])) {
95: $this->flush();
96: }
97:
98: return true;
99: }
100:
101: /**
102: * Flush the buffer.
103: */
104: public function flush()
105: {
106: if (!count($this->_buffer)) {
107: return true;
108: }
109:
110: $output = array();
111: foreach ($this->_buffer as $event) {
112: $line = trim($this->_formatter->format($event));
113:
114: // Normalize line breaks.
115: $line = str_replace("\r\n", "\n", $line);
116:
117: // Escape line breaks
118: $line = str_replace("\n", "\\n\\\n", $line);
119:
120: // Escape quotes.
121: $line = str_replace('"', '\\"', $line);
122:
123: // Firebug call.
124: $method = isset(self::$_methods[$event['level']])
125: ? self::$_methods[$event['level']]
126: : 'log';
127: $output[] = 'console.' . $method . '("' . $line . '");';
128: }
129:
130: echo '<script type="text/javascript">'
131: . "\nif (('console' in window) || ('firebug' in console)) {\n"
132: . implode("\n", $output) . "\n"
133: . "}\n"
134: . "</script>\n";
135:
136: $this->_buffer = array();
137: }
138:
139: }
140: