File indexing completed on 2024-12-22 05:36:50

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Log
0017  * @subpackage Writer
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Log */
0024 // require_once 'Zend/Log.php';
0025 
0026 /** Zend_Log_Writer_Abstract */
0027 // require_once 'Zend/Log/Writer/Abstract.php';
0028 
0029 /** Zend_Log_Formatter_Firebug */
0030 // require_once 'Zend/Log/Formatter/Firebug.php';
0031 
0032 /** Zend_Wildfire_Plugin_FirePhp */
0033 // require_once 'Zend/Wildfire/Plugin/FirePhp.php';
0034 
0035 /**
0036  * Writes log messages to the Firebug Console via FirePHP.
0037  *
0038  * @category   Zend
0039  * @package    Zend_Log
0040  * @subpackage Writer
0041  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0042  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0043  */
0044 class Zend_Log_Writer_Firebug extends Zend_Log_Writer_Abstract
0045 {
0046     /**
0047      * Maps logging priorities to logging display styles
0048      *
0049      * @var array
0050      */
0051     protected $_priorityStyles = array(Zend_Log::EMERG  => Zend_Wildfire_Plugin_FirePhp::ERROR,
0052                                        Zend_Log::ALERT  => Zend_Wildfire_Plugin_FirePhp::ERROR,
0053                                        Zend_Log::CRIT   => Zend_Wildfire_Plugin_FirePhp::ERROR,
0054                                        Zend_Log::ERR    => Zend_Wildfire_Plugin_FirePhp::ERROR,
0055                                        Zend_Log::WARN   => Zend_Wildfire_Plugin_FirePhp::WARN,
0056                                        Zend_Log::NOTICE => Zend_Wildfire_Plugin_FirePhp::INFO,
0057                                        Zend_Log::INFO   => Zend_Wildfire_Plugin_FirePhp::INFO,
0058                                        Zend_Log::DEBUG  => Zend_Wildfire_Plugin_FirePhp::LOG);
0059 
0060     /**
0061      * The default logging style for un-mapped priorities
0062      *
0063      * @var string
0064      */
0065     protected $_defaultPriorityStyle = Zend_Wildfire_Plugin_FirePhp::LOG;
0066 
0067     /**
0068      * Flag indicating whether the log writer is enabled
0069      *
0070      * @var boolean
0071      */
0072     protected $_enabled = true;
0073 
0074     /**
0075      * Class constructor
0076      *
0077      * @return void
0078      */
0079     public function __construct()
0080     {
0081         if (php_sapi_name() == 'cli') {
0082             $this->setEnabled(false);
0083         }
0084 
0085         $this->_formatter = new Zend_Log_Formatter_Firebug();
0086     }
0087 
0088     /**
0089      * Create a new instance of Zend_Log_Writer_Firebug
0090      *
0091      * @param  array|Zend_Config $config
0092      * @return Zend_Log_Writer_Firebug
0093      */
0094     static public function factory($config)
0095     {
0096         return new self();
0097     }
0098 
0099     /**
0100      * Enable or disable the log writer.
0101      *
0102      * @param boolean $enabled Set to TRUE to enable the log writer
0103      * @return boolean The previous value.
0104      */
0105     public function setEnabled($enabled)
0106     {
0107         $previous = $this->_enabled;
0108         $this->_enabled = $enabled;
0109         return $previous;
0110     }
0111 
0112     /**
0113      * Determine if the log writer is enabled.
0114      *
0115      * @return boolean Returns TRUE if the log writer is enabled.
0116      */
0117     public function getEnabled()
0118     {
0119         return $this->_enabled;
0120     }
0121 
0122     /**
0123      * Set the default display style for user-defined priorities
0124      *
0125      * @param string $style The default log display style
0126      * @return string Returns previous default log display style
0127      */
0128     public function setDefaultPriorityStyle($style)
0129     {
0130         $previous = $this->_defaultPriorityStyle;
0131         $this->_defaultPriorityStyle = $style;
0132         return $previous;
0133     }
0134 
0135     /**
0136      * Get the default display style for user-defined priorities
0137      *
0138      * @return string Returns the default log display style
0139      */
0140     public function getDefaultPriorityStyle()
0141     {
0142         return $this->_defaultPriorityStyle;
0143     }
0144 
0145     /**
0146      * Set a display style for a logging priority
0147      *
0148      * @param int $priority The logging priority
0149      * @param string $style The logging display style
0150      * @return string|boolean The previous logging display style if defined or TRUE otherwise
0151      */
0152     public function setPriorityStyle($priority, $style)
0153     {
0154         $previous = true;
0155         if (array_key_exists($priority,$this->_priorityStyles)) {
0156             $previous = $this->_priorityStyles[$priority];
0157         }
0158         $this->_priorityStyles[$priority] = $style;
0159         return $previous;
0160     }
0161 
0162     /**
0163      * Get a display style for a logging priority
0164      *
0165      * @param int $priority The logging priority
0166      * @return string|boolean The logging display style if defined or FALSE otherwise
0167      */
0168     public function getPriorityStyle($priority)
0169     {
0170         if (array_key_exists($priority,$this->_priorityStyles)) {
0171             return $this->_priorityStyles[$priority];
0172         }
0173         return false;
0174     }
0175 
0176     /**
0177      * Log a message to the Firebug Console.
0178      *
0179      * @param array $event The event data
0180      * @return void
0181      */
0182     protected function _write($event)
0183     {
0184         if (!$this->getEnabled()) {
0185             return;
0186         }
0187 
0188         if (array_key_exists($event['priority'],$this->_priorityStyles)) {
0189             $type = $this->_priorityStyles[$event['priority']];
0190         } else {
0191             $type = $this->_defaultPriorityStyle;
0192         }
0193 
0194         $message = $this->_formatter->format($event);
0195 
0196         $label = isset($event['firebugLabel'])?$event['firebugLabel']:null;
0197 
0198         Zend_Wildfire_Plugin_FirePhp::getInstance()->send($message,
0199                                                           $label,
0200                                                           $type,
0201                                                           array('traceOffset'=>4,
0202                                                                 'fixZendLogOffsetIfApplicable'=>true));
0203     }
0204 }