home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Log / daemon.php < prev    next >
Encoding:
PHP Script  |  2006-04-07  |  6.5 KB  |  231 lines

  1. <?php
  2. // $Id: daemon.php,v 1.2 2005/02/26 14:48:58 chagenbu Exp $
  3.  
  4. /**
  5.  * The Log_daemon class is a concrete implementation of the Log::
  6.  * abstract class which sends messages to syslog daemon on UNIX-like machines.
  7.  * This class uses the syslog protocol: http://www.ietf.org/rfc/rfc3164.txt
  8.  *
  9.  * @author  Bart van der Schans <schans@dds.nl>
  10.  * @version $Revision: 1.2 $
  11.  * @package Log
  12.  */
  13. class Log_daemon extends Log
  14. {
  15.     /**
  16.      * Integer holding the log facility to use.
  17.      * @var string
  18.      */
  19.     var $_name = LOG_DAEMON;
  20.  
  21.     /**
  22.      * Var holding the resource pointer to the socket
  23.      * @var resource
  24.      */
  25.     var $_socket;
  26.  
  27.     /**
  28.      * The ip address or servername
  29.      * @see http://www.php.net/manual/en/transports.php
  30.      * @var string
  31.      */
  32.     var $_ip = '127.0.0.1';
  33.  
  34.     /**
  35.      * Protocol to use (tcp, udp, etc.)
  36.      * @see http://www.php.net/manual/en/transports.php
  37.      * @var string
  38.      */
  39.     var $_proto = 'udp';
  40.  
  41.     /**
  42.      * Port to connect to
  43.      * @var int
  44.      */
  45.     var $_port = 514;
  46.  
  47.     /**
  48.      * Maximum message length in bytes
  49.      * @var int
  50.      */
  51.     var $_maxsize = 4096;
  52.  
  53.     /**
  54.      * Socket timeout in seconds
  55.      * @var int
  56.      */
  57.     var $_timeout = 1;
  58.  
  59.  
  60.     /**
  61.      * Constructs a new syslog object.
  62.      *
  63.      * @param string $name     The syslog facility.
  64.      * @param string $ident    The identity string.
  65.      * @param array  $conf     The configuration array.
  66.      * @param int    $maxLevel Maximum level at which to log.
  67.      * @access public
  68.      */
  69.     function Log_daemon($name, $ident = '', $conf = array(),
  70.                         $level = PEAR_LOG_DEBUG)
  71.     {
  72.         /* Ensure we have a valid integer value for $name. */
  73.         if (empty($name) || !is_int($name)) {
  74.             $name = LOG_SYSLOG;
  75.         }
  76.  
  77.         $this->_id = md5(microtime());
  78.         $this->_name = $name;
  79.         $this->_ident = $ident;
  80.         $this->_mask = Log::UPTO($level);
  81.  
  82.         if (isset($conf['ip'])) {
  83.             $this->_ip = $conf['ip'];
  84.         }
  85.         if (isset($conf['proto'])) {
  86.             $this->_proto = $conf['proto'];
  87.         }
  88.         if (isset($conf['port'])) {
  89.             $this->_port = $conf['port'];
  90.         }
  91.         if (isset($conf['maxsize'])) {
  92.             $this->_maxsize = $conf['maxsize'];
  93.         }
  94.         if (isset($conf['timeout'])) {
  95.             $this->_timeout = $conf['timeout'];
  96.         }
  97.         $this->_proto = $this->_proto . '://';
  98.  
  99.         register_shutdown_function(array(&$this, '_Log_daemon'));
  100.     }
  101.  
  102.     /**
  103.      * Destructor.
  104.      *
  105.      * @access private
  106.      */
  107.     function _Log_daemon()
  108.     {
  109.         $this->close();
  110.     }
  111.  
  112.     /**
  113.      * Opens a connection to the system logger, if it has not already
  114.      * been opened.  This is implicitly called by log(), if necessary.
  115.      * @access public
  116.      */
  117.     function open()
  118.     {
  119.         if (!$this->_opened) {
  120.             $this->_opened = (bool)($this->_socket = @fsockopen(
  121.                                                 $this->_proto . $this->_ip,
  122.                                                 $this->_port,
  123.                                                 $errno,
  124.                                                 $errstr,
  125.                                                 $this->_timeout));
  126.         }
  127.         return $this->_opened;
  128.     }
  129.  
  130.     /**
  131.      * Closes the connection to the system logger, if it is open.
  132.      * @access public
  133.      */
  134.     function close()
  135.     {
  136.         if ($this->_opened) {
  137.             $this->_opened = false;
  138.             return fclose($this->_socket);
  139.         }
  140.         return true;
  141.     }
  142.  
  143.     /**
  144.      * Sends $message to the currently open syslog connection.  Calls
  145.      * open() if necessary. Also passes the message along to any Log_observer
  146.      * instances that are observing this Log.
  147.      *
  148.      * @param string $message  The textual message to be logged.
  149.      * @param int $priority (optional) The priority of the message.  Valid
  150.      *                  values are: LOG_EMERG, LOG_ALERT, LOG_CRIT,
  151.      *                  LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO,
  152.      *                  and LOG_DEBUG.  The default is LOG_INFO.
  153.      * @access public
  154.      */
  155.     function log($message, $priority = null)
  156.     {
  157.         /* If a priority hasn't been specified, use the default value. */
  158.         if ($priority === null) {
  159.             $priority = $this->_priority;
  160.         }
  161.  
  162.         /* Abort early if the priority is above the maximum logging level. */
  163.         if (!$this->_isMasked($priority)) {
  164.             return false;
  165.         }
  166.  
  167.         /* If the connection isn't open and can't be opened, return failure. */
  168.         if (!$this->_opened && !$this->open()) {
  169.             return false;
  170.         }
  171.  
  172.         /* Extract the string representation of the message. */
  173.         $message = $this->_extractMessage($message);
  174.  
  175.         /* Set the facility level. */
  176.         $facility_level = intval($this->_name) +
  177.                           intval($this->_toSyslog($priority));
  178.  
  179.         /* Prepend ident info. */
  180.         if (!empty($this->_ident)) {
  181.             $message = $this->_ident . ' ' . $message;
  182.         }
  183.  
  184.         /* Check for message length. */
  185.         if (strlen($message) > $this->_maxsize) {
  186.             $message = substr($message, 0, ($this->_maxsize) - 10) . ' [...]';
  187.         }
  188.  
  189.         /* Write to socket. */
  190.         fwrite($this->_socket, '<' . $facility_level . '>' . $message . "\n");
  191.  
  192.         $this->_announce(array('priority' => $priority, 'message' => $message));
  193.     }
  194.  
  195.     /**
  196.      * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
  197.      *
  198.      * This function exists because, under Windows, not all of the LOG_*
  199.      * constants have unique values.  Instead, the PEAR_LOG_* were introduced
  200.      * for global use, with the conversion to the LOG_* constants kept local to
  201.      * to the syslog driver.
  202.      *
  203.      * @param int $priority     PEAR_LOG_* value to convert to LOG_* value.
  204.      *
  205.      * @return  The LOG_* representation of $priority.
  206.      *
  207.      * @access private
  208.      */
  209.     function _toSyslog($priority)
  210.     {
  211.         static $priorities = array(
  212.             PEAR_LOG_EMERG   => LOG_EMERG,
  213.             PEAR_LOG_ALERT   => LOG_ALERT,
  214.             PEAR_LOG_CRIT    => LOG_CRIT,
  215.             PEAR_LOG_ERR     => LOG_ERR,
  216.             PEAR_LOG_WARNING => LOG_WARNING,
  217.             PEAR_LOG_NOTICE  => LOG_NOTICE,
  218.             PEAR_LOG_INFO    => LOG_INFO,
  219.             PEAR_LOG_DEBUG   => LOG_DEBUG
  220.         );
  221.  
  222.         /* If we're passed an unknown priority, default to LOG_INFO. */
  223.         if (!is_int($priority) || !in_array($priority, $priorities)) {
  224.             return LOG_INFO;
  225.         }
  226.  
  227.         return $priorities[$priority];
  228.     }
  229.  
  230. }
  231.