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 / syslog.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.3 KB  |  180 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/syslog.php,v 1.25 2007/01/29 05:09:07 jon Exp $
  4.  * $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $
  5.  *
  6.  * @version $Revision: 1.25 $
  7.  * @package Log
  8.  */
  9.  
  10. /**
  11.  * The Log_syslog class is a concrete implementation of the Log::
  12.  * abstract class which sends messages to syslog on UNIX-like machines
  13.  * (PHP emulates this with the Event Log on Windows machines).
  14.  *
  15.  * @author  Chuck Hagenbuch <chuck@horde.org>
  16.  * @author  Jon Parise <jon@php.net>
  17.  * @since   Horde 1.3
  18.  * @since   Log 1.0
  19.  * @package Log
  20.  *
  21.  * @example syslog.php      Using the syslog handler.
  22.  */
  23. class Log_syslog extends Log
  24. {
  25.     /**
  26.      * Integer holding the log facility to use.
  27.      * @var integer
  28.      * @access private
  29.      */
  30.     var $_name = LOG_SYSLOG;
  31.  
  32.     /**
  33.      * Should we inherit the current syslog connection for this process, or
  34.      * should we call openlog() to start a new syslog connection?
  35.      * @var boolean
  36.      * @access private
  37.      */
  38.     var $_inherit = false;
  39.  
  40.     /**
  41.      * Constructs a new syslog object.
  42.      *
  43.      * @param string $name     The syslog facility.
  44.      * @param string $ident    The identity string.
  45.      * @param array  $conf     The configuration array.
  46.      * @param int    $level    Log messages up to and including this level.
  47.      * @access public
  48.      */
  49.     function Log_syslog($name, $ident = '', $conf = array(),
  50.                         $level = PEAR_LOG_DEBUG)
  51.     {
  52.         /* Ensure we have a valid integer value for $name. */
  53.         if (empty($name) || !is_int($name)) {
  54.             $name = LOG_SYSLOG;
  55.         }
  56.  
  57.         if (isset($conf['inherit'])) {
  58.             $this->_inherit = $conf['inherit'];
  59.             $this->_opened = $this->_inherit;
  60.         }
  61.  
  62.         $this->_id = md5(microtime());
  63.         $this->_name = $name;
  64.         $this->_ident = $ident;
  65.         $this->_mask = Log::UPTO($level);
  66.     }
  67.  
  68.     /**
  69.      * Opens a connection to the system logger, if it has not already
  70.      * been opened.  This is implicitly called by log(), if necessary.
  71.      * @access public
  72.      */
  73.     function open()
  74.     {
  75.         if (!$this->_opened) {
  76.             $this->_opened = openlog($this->_ident, LOG_PID, $this->_name);
  77.         }
  78.  
  79.         return $this->_opened;
  80.     }
  81.  
  82.     /**
  83.      * Closes the connection to the system logger, if it is open.
  84.      * @access public
  85.      */
  86.     function close()
  87.     {
  88.         if ($this->_opened && !$this->_inherit) {
  89.             closelog();
  90.             $this->_opened = false;
  91.         }
  92.  
  93.         return true;
  94.     }
  95.  
  96.     /**
  97.      * Sends $message to the currently open syslog connection.  Calls
  98.      * open() if necessary. Also passes the message along to any Log_observer
  99.      * instances that are observing this Log.
  100.      *
  101.      * @param mixed $message String or object containing the message to log.
  102.      * @param int $priority (optional) The priority of the message.  Valid
  103.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  104.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  105.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  106.      * @return boolean  True on success or false on failure.
  107.      * @access public
  108.      */
  109.     function log($message, $priority = null)
  110.     {
  111.         /* If a priority hasn't been specified, use the default value. */
  112.         if ($priority === null) {
  113.             $priority = $this->_priority;
  114.         }
  115.  
  116.         /* Abort early if the priority is above the maximum logging level. */
  117.         if (!$this->_isMasked($priority)) {
  118.             return false;
  119.         }
  120.  
  121.         /* If the connection isn't open and can't be opened, return failure. */
  122.         if (!$this->_opened && !$this->open()) {
  123.             return false;
  124.         }
  125.  
  126.         /* Extract the string representation of the message. */
  127.         $message = $this->_extractMessage($message);
  128.  
  129.         /* Build a syslog priority value based on our current configuration. */
  130.         $priority = $this->_toSyslog($priority);
  131.         if ($this->_inherit) {
  132.             $priority |= $this->_name;
  133.         }
  134.  
  135.         if (!syslog($priority, $message)) {
  136.             return false;
  137.         }
  138.  
  139.         $this->_announce(array('priority' => $priority, 'message' => $message));
  140.  
  141.         return true;
  142.     }
  143.  
  144.     /**
  145.      * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
  146.      *
  147.      * This function exists because, under Windows, not all of the LOG_*
  148.      * constants have unique values.  Instead, the PEAR_LOG_* were introduced
  149.      * for global use, with the conversion to the LOG_* constants kept local to
  150.      * to the syslog driver.
  151.      *
  152.      * @param int $priority     PEAR_LOG_* value to convert to LOG_* value.
  153.      *
  154.      * @return  The LOG_* representation of $priority.
  155.      *
  156.      * @access private
  157.      */
  158.     function _toSyslog($priority)
  159.     {
  160.         static $priorities = array(
  161.             PEAR_LOG_EMERG   => LOG_EMERG,
  162.             PEAR_LOG_ALERT   => LOG_ALERT,
  163.             PEAR_LOG_CRIT    => LOG_CRIT,
  164.             PEAR_LOG_ERR     => LOG_ERR,
  165.             PEAR_LOG_WARNING => LOG_WARNING,
  166.             PEAR_LOG_NOTICE  => LOG_NOTICE,
  167.             PEAR_LOG_INFO    => LOG_INFO,
  168.             PEAR_LOG_DEBUG   => LOG_DEBUG
  169.         );
  170.  
  171.         /* If we're passed an unknown priority, default to LOG_INFO. */
  172.         if (!is_int($priority) || !in_array($priority, $priorities)) {
  173.             return LOG_INFO;
  174.         }
  175.  
  176.         return $priorities[$priority];
  177.     }
  178.  
  179. }
  180.