home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Log / console.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.5 KB  |  209 lines

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