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 / file.php < prev    next >
Encoding:
PHP Script  |  2006-04-07  |  9.3 KB  |  325 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/file.php,v 1.44 2005/09/19 04:24:14 jon Exp $
  4.  *
  5.  * @version $Revision: 1.44 $
  6.  * @package Log
  7.  */
  8.  
  9. /**
  10.  * The Log_file class is a concrete implementation of the Log abstract
  11.  * class that logs messages to a text file.
  12.  * 
  13.  * @author  Jon Parise <jon@php.net>
  14.  * @author  Roman Neuhauser <neuhauser@bellavista.cz>
  15.  * @since   Log 1.0
  16.  * @package Log
  17.  *
  18.  * @example file.php    Using the file handler.
  19.  */
  20. class Log_file extends Log
  21. {
  22.     /**
  23.      * String containing the name of the log file.
  24.      * @var string
  25.      * @access private
  26.      */
  27.     var $_filename = 'php.log';
  28.  
  29.     /**
  30.      * Handle to the log file.
  31.      * @var resource
  32.      * @access private
  33.      */
  34.     var $_fp = false;
  35.  
  36.     /**
  37.      * Should new log entries be append to an existing log file, or should the
  38.      * a new log file overwrite an existing one?
  39.      * @var boolean
  40.      * @access private
  41.      */
  42.     var $_append = true;
  43.  
  44.     /**
  45.      * Should advisory file locking (i.e., flock()) be used?
  46.      * @var boolean
  47.      * @access private
  48.      */
  49.     var $_locking = false;
  50.  
  51.     /**
  52.      * Integer (in octal) containing the log file's permissions mode.
  53.      * @var integer
  54.      * @access private
  55.      */
  56.     var $_mode = 0644;
  57.  
  58.     /**
  59.      * Integer (in octal) specifying the file permission mode that will be
  60.      * used when creating directories that do not already exist.
  61.      * @var integer
  62.      * @access private
  63.      */
  64.     var $_dirmode = 0755;
  65.  
  66.     /**
  67.      * String containing the format of a log line.
  68.      * @var string
  69.      * @access private
  70.      */
  71.     var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  72.  
  73.     /**
  74.      * String containing the timestamp format.  It will be passed directly to
  75.      * strftime().  Note that the timestamp string will generated using the
  76.      * current locale.
  77.      * @var string
  78.      * @access private
  79.      */
  80.     var $_timeFormat = '%b %d %H:%M:%S';
  81.  
  82.     /**
  83.      * Hash that maps canonical format keys to position arguments for the
  84.      * "line format" string.
  85.      * @var array
  86.      * @access private
  87.      */
  88.     var $_formatMap = array('%{timestamp}'  => '%1$s',
  89.                             '%{ident}'      => '%2$s',
  90.                             '%{priority}'   => '%3$s',
  91.                             '%{message}'    => '%4$s',
  92.                             '%\{'           => '%%{');
  93.  
  94.     /**
  95.      * String containing the end-on-line character sequence.
  96.      * @var string
  97.      * @access private
  98.      */
  99.     var $_eol = "\n";
  100.  
  101.     /**
  102.      * Constructs a new Log_file object.
  103.      *
  104.      * @param string $name     Ignored.
  105.      * @param string $ident    The identity string.
  106.      * @param array  $conf     The configuration array.
  107.      * @param int    $level    Log messages up to and including this level.
  108.      * @access public
  109.      */
  110.     function Log_file($name, $ident = '', $conf = array(),
  111.                       $level = PEAR_LOG_DEBUG)
  112.     {
  113.         $this->_id = md5(microtime());
  114.         $this->_filename = $name;
  115.         $this->_ident = $ident;
  116.         $this->_mask = Log::UPTO($level);
  117.  
  118.         if (isset($conf['append'])) {
  119.             $this->_append = $conf['append'];
  120.         }
  121.  
  122.         if (isset($conf['locking'])) {
  123.             $this->_locking = $conf['locking'];
  124.         }
  125.  
  126.         if (!empty($conf['mode'])) {
  127.             if (is_string($conf['mode'])) {
  128.                 $this->_mode = octdec($conf['mode']);
  129.             } else {
  130.                 $this->_mode = $conf['mode'];
  131.             }
  132.         }
  133.  
  134.         if (!empty($conf['dirmode'])) {
  135.             if (is_string($conf['dirmode'])) {
  136.                 $this->_dirmode = octdec($conf['dirmode']);
  137.             } else {
  138.                 $this->_dirmode = $conf['dirmode'];
  139.             }
  140.         }
  141.  
  142.         if (!empty($conf['lineFormat'])) {
  143.             $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  144.                                              array_values($this->_formatMap),
  145.                                              $conf['lineFormat']);
  146.         }
  147.  
  148.         if (!empty($conf['timeFormat'])) {
  149.             $this->_timeFormat = $conf['timeFormat'];
  150.         }
  151.  
  152.         if (!empty($conf['eol'])) {
  153.             $this->_eol = $conf['eol'];
  154.         } else {
  155.             $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
  156.         }
  157.  
  158.         register_shutdown_function(array(&$this, '_Log_file'));
  159.     }
  160.  
  161.     /**
  162.      * Destructor
  163.      */
  164.     function _Log_file()
  165.     {
  166.         if ($this->_opened) {
  167.             $this->close();
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * Creates the given directory path.  If the parent directories don't
  173.      * already exist, they will be created, too.
  174.      *
  175.      * This implementation is inspired by Python's os.makedirs function.
  176.      *
  177.      * @param   string  $path       The full directory path to create.
  178.      * @param   integer $mode       The permissions mode with which the
  179.      *                              directories will be created.
  180.      *
  181.      * @return  True if the full path is successfully created or already
  182.      *          exists.
  183.      *
  184.      * @access  private
  185.      */
  186.     function _mkpath($path, $mode = 0700)
  187.     {
  188.         /* Separate the last pathname component from the rest of the path. */
  189.         $head = dirname($path);
  190.         $tail = basename($path);
  191.  
  192.         /* Make sure we've split the path into two complete components. */
  193.         if (empty($tail)) {
  194.             $head = dirname($path);
  195.             $tail = basename($path);
  196.         }
  197.  
  198.         /* Recurse up the path if our current segment does not exist. */
  199.         if (!empty($head) && !empty($tail) && !is_dir($head)) {
  200.             $this->_mkpath($head, $mode);
  201.         }
  202.  
  203.         /* Create this segment of the path. */
  204.         return @mkdir($head, $mode);
  205.     }
  206.  
  207.     /**
  208.      * Opens the log file for output.  If the specified log file does not
  209.      * already exist, it will be created.  By default, new log entries are
  210.      * appended to the end of the log file.
  211.      *
  212.      * This is implicitly called by log(), if necessary.
  213.      *
  214.      * @access public
  215.      */
  216.     function open()
  217.     {
  218.         if (!$this->_opened) {
  219.             /* If the log file's directory doesn't exist, create it. */
  220.             if (!is_dir(dirname($this->_filename))) {
  221.                 $this->_mkpath($this->_filename, $this->_dirmode);
  222.             }
  223.  
  224.             /* Determine whether the log file needs to be created. */
  225.             $creating = !file_exists($this->_filename);
  226.  
  227.             /* Obtain a handle to the log file. */
  228.             $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
  229.  
  230.             /* We consider the file "opened" if we have a valid file pointer. */
  231.             $this->_opened = ($this->_fp !== false);
  232.  
  233.             /* Attempt to set the file's permissions if we just created it. */
  234.             if ($creating && $this->_opened) {
  235.                 chmod($this->_filename, $this->_mode);
  236.             }
  237.         }
  238.  
  239.         return $this->_opened;
  240.     }
  241.  
  242.     /**
  243.      * Closes the log file if it is open.
  244.      *
  245.      * @access public
  246.      */
  247.     function close()
  248.     {
  249.         /* If the log file is open, close it. */
  250.         if ($this->_opened && fclose($this->_fp)) {
  251.             $this->_opened = false;
  252.         }
  253.  
  254.         return ($this->_opened === false);
  255.     }
  256.  
  257.     /**
  258.      * Flushes all pending data to the file handle.
  259.      *
  260.      * @access public
  261.      * @since Log 1.8.2
  262.      */
  263.     function flush()
  264.     {
  265.         return fflush($this->_fp);
  266.     }
  267.  
  268.     /**
  269.      * Logs $message to the output window.  The message is also passed along
  270.      * to any Log_observer instances that are observing this Log.
  271.      *
  272.      * @param mixed  $message  String or object containing the message to log.
  273.      * @param string $priority The priority of the message.  Valid
  274.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  275.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  276.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  277.      * @return boolean  True on success or false on failure.
  278.      * @access public
  279.      */
  280.     function log($message, $priority = null)
  281.     {
  282.         /* If a priority hasn't been specified, use the default value. */
  283.         if ($priority === null) {
  284.             $priority = $this->_priority;
  285.         }
  286.  
  287.         /* Abort early if the priority is above the maximum logging level. */
  288.         if (!$this->_isMasked($priority)) {
  289.             return false;
  290.         }
  291.  
  292.         /* If the log file isn't already open, open it now. */
  293.         if (!$this->_opened && !$this->open()) {
  294.             return false;
  295.         }
  296.  
  297.         /* Extract the string representation of the message. */
  298.         $message = $this->_extractMessage($message);
  299.  
  300.         /* Build the string containing the complete log line. */
  301.         $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
  302.                 $this->_ident, $this->priorityToString($priority),
  303.                 $message) . $this->_eol;
  304.  
  305.         /* If locking is enabled, acquire an exclusive lock on the file. */
  306.         if ($this->_locking) {
  307.             flock($this->_fp, LOCK_EX);
  308.         }
  309.  
  310.         /* Write the log line to the log file. */
  311.         $success = (fwrite($this->_fp, $line) !== false);
  312.  
  313.         /* Unlock the file now that we're finished writing to it. */ 
  314.         if ($this->_locking) {
  315.             flock($this->_fp, LOCK_UN);
  316.         }
  317.  
  318.         /* Notify observers about this log message. */
  319.         $this->_announce(array('priority' => $priority, 'message' => $message));
  320.  
  321.         return $success;
  322.     }
  323.  
  324. }
  325.