home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / win.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  7.4 KB  |  250 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/win.php,v 1.15 2004/01/19 08:02:40 jon Exp $
  4.  *
  5.  * @version $Revision: 1.15 $
  6.  * @package Log
  7.  */
  8.  
  9. /**
  10.  * The Log_win class is a concrete implementation of the Log abstract
  11.  * class that logs messages to a separate browser window.
  12.  *
  13.  * The concept for this log handler is based on part by Craig Davis' article
  14.  * entitled "JavaScript Power PHP Debugging:
  15.  *
  16.  *  http://www.zend.com/zend/tut/tutorial-DebugLib.php
  17.  * 
  18.  * @author  Jon Parise <jon@php.net>
  19.  * @since   Log 1.7.0
  20.  * @package Log
  21.  *
  22.  * @example win.php     Using the window handler.
  23.  */
  24. class Log_win extends Log
  25. {
  26.     /**
  27.      * The name of the output window.
  28.      * @var string
  29.      * @access private
  30.      */
  31.     var $_name = 'LogWindow';
  32.  
  33.     /**
  34.      * The title of the output window.
  35.      * @var string
  36.      * @access private
  37.      */
  38.     var $_title = 'Log Output Window';
  39.  
  40.     /**
  41.      * Mapping of log priorities to colors.
  42.      * @var array
  43.      * @access private
  44.      */
  45.     var $_colors = array(
  46.                         PEAR_LOG_EMERG   => 'red',
  47.                         PEAR_LOG_ALERT   => 'orange',
  48.                         PEAR_LOG_CRIT    => 'yellow',
  49.                         PEAR_LOG_ERR     => 'green',
  50.                         PEAR_LOG_WARNING => 'blue',
  51.                         PEAR_LOG_NOTICE  => 'indigo',
  52.                         PEAR_LOG_INFO    => 'violet',
  53.                         PEAR_LOG_DEBUG   => 'black'
  54.                     );
  55.  
  56.     /**
  57.      * String buffer that holds line that are pending output.
  58.      * @var array
  59.      * @access private
  60.      */
  61.     var $_buffer = array();
  62.  
  63.     /**
  64.      * Constructs a new Log_win object.
  65.      * 
  66.      * @param string $name     Ignored.
  67.      * @param string $ident    The identity string.
  68.      * @param array  $conf     The configuration array.
  69.      * @param int    $level    Log messages up to and including this level.
  70.      * @access public
  71.      */
  72.     function Log_win($name, $ident = '', $conf = array(),
  73.                           $level = PEAR_LOG_DEBUG)
  74.     {
  75.         $this->_id = md5(microtime());
  76.         $this->_name = $name;
  77.         $this->_ident = $ident;
  78.         $this->_mask = Log::UPTO($level);
  79.  
  80.         if (isset($conf['title'])) {
  81.             $this->_title = $conf['title'];
  82.         }
  83.         if (isset($conf['colors']) && is_array($conf['colors'])) {
  84.             $this->_colors = $conf['colors'];
  85.         }
  86.  
  87.         register_shutdown_function(array(&$this, '_Log_win'));
  88.     }
  89.  
  90.     /**
  91.      * Destructor
  92.      */
  93.     function _Log_win()
  94.     {
  95.         if ($this->_opened || (count($this->_buffer) > 0)) {
  96.             $this->close();
  97.         }
  98.     }
  99.  
  100.     /**
  101.      * The first time open() is called, it will open a new browser window and
  102.      * prepare it for output.
  103.      *
  104.      * This is implicitly called by log(), if necessary.
  105.      *
  106.      * @access public
  107.      */
  108.     function open()
  109.     {
  110.         if (!$this->_opened) {
  111. ?>
  112. <script language="JavaScript">
  113. win = window.open('', '<?php echo $this->_name; ?>', 'toolbar=no,scrollbars,width=600,height=400');
  114. win.document.writeln('<html>');
  115. win.document.writeln('<head>');
  116. win.document.writeln('<title><?php echo $this->_title; ?></title>');
  117. win.document.writeln('<style type="text/css">');
  118. win.document.writeln('body { font-family: monospace; font-size: 8pt; }');
  119. win.document.writeln('td,th { font-size: 8pt; }');
  120. win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
  121. win.document.writeln('td,th { border-right: #999999 solid 1px; }');
  122. win.document.writeln('</style>');
  123. win.document.writeln('</head>');
  124. win.document.writeln('<body>');
  125. win.document.writeln('<table border="0" cellpadding="2" cellspacing="0">');
  126. win.document.writeln('<tr><th>Time</th>');
  127. <?php if (!empty($this->_ident)): ?>
  128. win.document.writeln('<th>Ident</th>');
  129. <?php endif; ?>
  130. win.document.writeln('<th>Priority</th><th width="100%">Message</th></tr>');
  131. </script>
  132. <?php
  133.             $this->_opened = true;
  134.         }
  135.  
  136.         return $this->_opened;
  137.     }
  138.  
  139.     /**
  140.      * Closes the output stream if it is open.  If there are still pending
  141.      * lines in the output buffer, the output window will be opened so that
  142.      * the buffer can be drained.
  143.      *
  144.      * @access public
  145.      */
  146.     function close()
  147.     {
  148.         /*
  149.          * If there are still lines waiting to be written, open the output
  150.          * window so that we can drain the buffer.
  151.          */
  152.         if (!$this->_opened && (count($this->_buffer) > 0)) {
  153.             $this->open();
  154.         }
  155.  
  156.         if ($this->_opened) {
  157.             $this->_writeln('</table>');
  158.             $this->_writeln('</body></html>');
  159.             $this->_opened = false;
  160.         }
  161.  
  162.         return ($this->_opened === false);
  163.     }
  164.  
  165.     /**
  166.      * Writes a single line of text to the output window.
  167.      *
  168.      * @param string    $line   The line of text to write.
  169.      *
  170.      * @access private
  171.      */
  172.     function _writeln($line)
  173.     {
  174.         /* Add this line to our output buffer. */
  175.         $this->_buffer[] = $line;
  176.  
  177.         /* Buffer the output until this page's headers have been sent. */
  178.         if (!headers_sent()) {
  179.             return;
  180.         }
  181.  
  182.         /* If we haven't already opened the output window, do so now. */
  183.         if (!$this->_opened && !$this->open()) {
  184.             return false;
  185.         }
  186.  
  187.         /* Drain the buffer to the output window. */
  188.         foreach ($this->_buffer as $line) {
  189.             echo "<script language='JavaScript'>\n";
  190.             echo "win.document.writeln('" . addslashes($line) . "');\n";
  191.             echo "self.focus();\n";
  192.             echo "</script>\n";
  193.         }
  194.  
  195.         /* Now that the buffer has been drained, clear it. */
  196.         $this->_buffer = array();
  197.     }
  198.  
  199.     /**
  200.      * Logs $message to the output window.  The message is also passed along
  201.      * to any Log_observer instances that are observing this Log.
  202.      * 
  203.      * @param mixed  $message  String or object containing the message to log.
  204.      * @param string $priority The priority of the message.  Valid
  205.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  206.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  207.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  208.      * @return boolean  True on success or false on failure.
  209.      * @access public
  210.      */
  211.     function log($message, $priority = null)
  212.     {
  213.         /* If a priority hasn't been specified, use the default value. */
  214.         if ($priority === null) {
  215.             $priority = $this->_priority;
  216.         }
  217.  
  218.         /* Abort early if the priority is above the maximum logging level. */
  219.         if (!$this->_isMasked($priority)) {
  220.             return false;
  221.         }
  222.  
  223.         /* Extract the string representation of the message. */
  224.         $message = $this->_extractMessage($message);
  225.  
  226.         list($usec, $sec) = explode(' ', microtime());
  227.  
  228.         /* Build the output line that contains the log entry row. */
  229.         $line  = '<tr align="left" valign="top">';
  230.         $line .= sprintf('<td>%s.%s</td>',
  231.                          strftime('%T', $sec), substr($usec, 2, 2));
  232.         if (!empty($this->_ident)) {
  233.             $line .= '<td>' . $this->_ident . '</td>';
  234.         }
  235.         $line .= '<td>' . ucfirst($this->priorityToString($priority)) . '</td>';
  236.         $line .= sprintf('<td style="color: %s">%s</td>',
  237.                          $this->_colors[$priority],
  238.                          preg_replace('/\r\n|\n|\r/', '<br />', $message));
  239.         $line .= '</tr>';
  240.  
  241.         $this->_writeln($line);
  242.  
  243.         $this->_announce(array('priority' => $priority, 'message' => $message));
  244.  
  245.         return true;
  246.     }
  247. }
  248.  
  249. ?>
  250.