[795] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * $Header: /repository/pear/Log/Log/firebug.php,v 1.6 2008/01/19 21:56:16 jon Exp $ |
---|
| 4 | * |
---|
| 5 | * @version $Revision: 1.6 $ |
---|
| 6 | * @package Log |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * The Log_firebug class is a concrete implementation of the Log:: |
---|
| 11 | * abstract class which writes message into Firebug console. |
---|
| 12 | * |
---|
| 13 | * http://www.getfirebug.com/ |
---|
| 14 | * |
---|
| 15 | * @author Mika Tuupola <tuupola@appelsiini.net> |
---|
| 16 | * @since Log 1.9.11 |
---|
| 17 | * @package Log |
---|
| 18 | * |
---|
| 19 | * @example firebug.php Using the firebug handler. |
---|
| 20 | */ |
---|
| 21 | class Log_firebug extends Log |
---|
| 22 | { |
---|
| 23 | /** |
---|
| 24 | * Should the output be buffered or displayed immediately? |
---|
| 25 | * @var string |
---|
| 26 | * @access private |
---|
| 27 | */ |
---|
| 28 | var $_buffering = false; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * String holding the buffered output. |
---|
| 32 | * @var string |
---|
| 33 | * @access private |
---|
| 34 | */ |
---|
| 35 | var $_buffer = array(); |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * String containing the format of a log line. |
---|
| 39 | * @var string |
---|
| 40 | * @access private |
---|
| 41 | */ |
---|
| 42 | var $_lineFormat = '%2$s [%3$s] %4$s'; |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * String containing the timestamp format. It will be passed directly to |
---|
| 46 | * strftime(). Note that the timestamp string will generated using the |
---|
| 47 | * current locale. |
---|
| 48 | * |
---|
| 49 | * Note! Default lineFormat of this driver does not display time. |
---|
| 50 | * |
---|
| 51 | * @var string |
---|
| 52 | * @access private |
---|
| 53 | */ |
---|
| 54 | var $_timeFormat = '%b %d %H:%M:%S'; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Mapping of log priorities to Firebug methods. |
---|
| 58 | * @var array |
---|
| 59 | * @access private |
---|
| 60 | */ |
---|
| 61 | var $_methods = array( |
---|
| 62 | PEAR_LOG_EMERG => 'error', |
---|
| 63 | PEAR_LOG_ALERT => 'error', |
---|
| 64 | PEAR_LOG_CRIT => 'error', |
---|
| 65 | PEAR_LOG_ERR => 'error', |
---|
| 66 | PEAR_LOG_WARNING => 'warn', |
---|
| 67 | PEAR_LOG_NOTICE => 'info', |
---|
| 68 | PEAR_LOG_INFO => 'info', |
---|
| 69 | PEAR_LOG_DEBUG => 'debug' |
---|
| 70 | ); |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Constructs a new Log_firebug object. |
---|
| 74 | * |
---|
| 75 | * @param string $name Ignored. |
---|
| 76 | * @param string $ident The identity string. |
---|
| 77 | * @param array $conf The configuration array. |
---|
| 78 | * @param int $level Log messages up to and including this level. |
---|
| 79 | * @access public |
---|
| 80 | */ |
---|
| 81 | function Log_firebug($name = '', $ident = 'PHP', $conf = array(), |
---|
| 82 | $level = PEAR_LOG_DEBUG) |
---|
| 83 | { |
---|
| 84 | $this->_id = md5(microtime()); |
---|
| 85 | $this->_ident = $ident; |
---|
| 86 | $this->_mask = Log::UPTO($level); |
---|
| 87 | if (isset($conf['buffering'])) { |
---|
| 88 | $this->_buffering = $conf['buffering']; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if ($this->_buffering) { |
---|
| 92 | register_shutdown_function(array(&$this, '_Log_firebug')); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | if (!empty($conf['lineFormat'])) { |
---|
| 96 | $this->_lineFormat = str_replace(array_keys($this->_formatMap), |
---|
| 97 | array_values($this->_formatMap), |
---|
| 98 | $conf['lineFormat']); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if (!empty($conf['timeFormat'])) { |
---|
| 102 | $this->_timeFormat = $conf['timeFormat']; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Opens the firebug handler. |
---|
| 108 | * |
---|
| 109 | * @access public |
---|
| 110 | */ |
---|
| 111 | function open() |
---|
| 112 | { |
---|
| 113 | $this->_opened = true; |
---|
| 114 | return true; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Destructor |
---|
| 119 | */ |
---|
| 120 | function _Log_firebug() |
---|
| 121 | { |
---|
| 122 | $this->close(); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Closes the firebug handler. |
---|
| 127 | * |
---|
| 128 | * @access public |
---|
| 129 | */ |
---|
| 130 | function close() |
---|
| 131 | { |
---|
| 132 | $this->flush(); |
---|
| 133 | $this->_opened = false; |
---|
| 134 | return true; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Flushes all pending ("buffered") data. |
---|
| 139 | * |
---|
| 140 | * @access public |
---|
| 141 | */ |
---|
| 142 | function flush() { |
---|
| 143 | if (count($this->_buffer)) { |
---|
| 144 | print '<script type="text/javascript">'; |
---|
| 145 | print "\nif (('console' in window) && ('firebug' in console)) {\n"; |
---|
| 146 | foreach ($this->_buffer as $line) { |
---|
| 147 | print " $line\n"; |
---|
| 148 | } |
---|
| 149 | print "}\n"; |
---|
| 150 | print "</script>\n"; |
---|
| 151 | }; |
---|
| 152 | $this->_buffer = array(); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * Writes $message to Firebug console. Also, passes the message |
---|
| 157 | * along to any Log_observer instances that are observing this Log. |
---|
| 158 | * |
---|
| 159 | * @param mixed $message String or object containing the message to log. |
---|
| 160 | * @param string $priority The priority of the message. Valid |
---|
| 161 | * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, |
---|
| 162 | * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, |
---|
| 163 | * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG. |
---|
| 164 | * @return boolean True on success or false on failure. |
---|
| 165 | * @access public |
---|
| 166 | */ |
---|
| 167 | function log($message, $priority = null) |
---|
| 168 | { |
---|
| 169 | /* If a priority hasn't been specified, use the default value. */ |
---|
| 170 | if ($priority === null) { |
---|
| 171 | $priority = $this->_priority; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /* Abort early if the priority is above the maximum logging level. */ |
---|
| 175 | if (!$this->_isMasked($priority)) { |
---|
| 176 | return false; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /* Extract the string representation of the message. */ |
---|
| 180 | $message = $this->_extractMessage($message); |
---|
| 181 | $method = $this->_methods[$priority]; |
---|
| 182 | |
---|
| 183 | /* normalize line breaks */ |
---|
| 184 | $message = str_replace("\r\n", "\n", $message); |
---|
| 185 | |
---|
| 186 | /* escape line breaks */ |
---|
| 187 | $message = str_replace("\n", "\\n\\\n", $message); |
---|
| 188 | |
---|
| 189 | /* escape quotes */ |
---|
| 190 | $message = str_replace('"', '\\"', $message); |
---|
| 191 | |
---|
| 192 | /* Build the string containing the complete log line. */ |
---|
| 193 | $line = $this->_format($this->_lineFormat, |
---|
| 194 | strftime($this->_timeFormat), |
---|
| 195 | $priority, |
---|
| 196 | $message); |
---|
| 197 | |
---|
| 198 | if ($this->_buffering) { |
---|
| 199 | $this->_buffer[] = sprintf('console.%s("%s");', $method, $line); |
---|
| 200 | } else { |
---|
| 201 | print '<script type="text/javascript">'; |
---|
| 202 | print "\nif (('console' in window) && ('firebug' in console)) {\n"; |
---|
| 203 | /* Build and output the complete log line. */ |
---|
| 204 | printf(' console.%s("%s");', $method, $line); |
---|
| 205 | print "\n}\n"; |
---|
| 206 | print "</script>\n"; |
---|
| 207 | } |
---|
| 208 | /* Notify observers about this log message. */ |
---|
| 209 | $this->_announce(array('priority' => $priority, 'message' => $message)); |
---|
| 210 | |
---|
| 211 | return true; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | } |
---|