cvs: pear /Log/Log win.php
| From: | Jon Parise | Date: | Thu, 26 Jun 2003 07:43:19 +0000 |
| Subject: | cvs: pear /Log/Log win.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-11178@lists.php.net to get a copy of this message | ||
jon Thu Jun 26 03:43:19 2003 EDT
Modified files:
/pear/Log/Log win.php
Log:
Buffer the output so that we don't interfere with the page's regular
headers.
Index: pear/Log/Log/win.php
diff -u pear/Log/Log/win.php:1.2 pear/Log/Log/win.php:1.3
--- pear/Log/Log/win.php:1.2 Fri Jun 20 01:45:42 2003
+++ pear/Log/Log/win.php Thu Jun 26 03:43:19 2003
@@ -1,5 +1,5 @@
<?php
-// $Id: win.php,v 1.2 2003/06/20 05:45:42 jon Exp $
+// $Id: win.php,v 1.3 2003/06/26 07:43:19 jon Exp $
/**
* The Log_win class is a concrete implementation of the Log abstract
@@ -11,7 +11,7 @@
* http://www.zend.com/zend/tut/tutorial-DebugLib.php
*
* @author Jon Parise <jon@php.net>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
* @package Log
*/
class Log_win extends Log
@@ -47,6 +47,13 @@
);
/**
+ * String buffer that holds line that are pending output.
+ * @var array
+ * @access private
+ */
+ var $_buffer = array();
+
+ /**
* Constructs a new Log_win object.
*
* @param string $name Ignored.
@@ -69,6 +76,13 @@
if (isset($conf['colors']) && is_array($conf['colors'])) {
$this->_colors = $conf['colors'];
}
+
+ register_shutdown_function(array(&$this, '_Log_win'));
+ }
+
+ function _Log_win()
+ {
+ $this->close();
}
/**
@@ -90,12 +104,14 @@
win.document.writeln('<title><?php echo $this->_title; ?></title>');
win.document.writeln('<style type="text/css">');
win.document.writeln('body { font-family: monospace; font-size: 8pt; }');
-win.document.writeln('td { font-size: 8pt; }');
-win.document.writeln('td { border-bottom: #999999 solid 1px; }');
-win.document.writeln('td { border-right: #999999 solid 1px; }');
+win.document.writeln('td,th { font-size: 8pt; }');
+win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
+win.document.writeln('td,th { border-right: #999999 solid 1px; }');
win.document.writeln('</style>');
win.document.writeln('</head>');
win.document.writeln('<body>');
+win.document.writeln('<table border="0" cellpadding="2"
cellspacing="0">');
+win.document.writeln('<tr><th>Time</th><th>Ident</th><th>Message</th></tr>');
</script>
<?php
$this->_opened = true;
@@ -103,13 +119,24 @@
}
/**
- * Closes the connection to the system logger, if it is open.
+ * Closes the output stream if it is open. If there are still pending
+ * lines in the output buffer, the output window will be opened so that
+ * the buffer can be drained.
*
* @access public
*/
function close()
{
+ /*
+ * If there are still lines waiting to be written, open the output
+ * window so that we can drain the buffer.
+ */
+ if (count($this->_buffer > 0)) {
+ $this->open();
+ }
+
if ($this->_opened) {
+ $this->_writeln('</table>');
$this->_writeln('<br /><b>-- End --</b>');
$this->_writeln('</body></html>');
$this->_opened = false;
@@ -125,10 +152,29 @@
*/
function _writeln($line)
{
- echo "<script language='JavaScript'>\n";
- echo "win.document.writeln('$line');\n";
- echo "self.focus();\n";
- echo "</script>\n";
+ /* Add this line to our output buffer. */
+ $this->_buffer[] = $line;
+
+ /* Buffer the output until this page's headers have been sent. */
+ if (!headers_sent()) {
+ return;
+ }
+
+ /* If we haven't already opened the output window, do so now. */
+ if (!$this->_opened) {
+ $this->open();
+ }
+
+ /* Drain the buffer to the output window. */
+ foreach ($this->_buffer as $line) {
+ echo "<script language='JavaScript'>\n";
+ echo "win.document.writeln('$line');\n";
+ echo "self.focus();\n";
+ echo "</script>\n";
+ }
+
+ /* Now that the buffer has been drained, clear it. */
+ $this->_buffer = array();
}
/**
@@ -151,20 +197,20 @@
return false;
}
- if (!$this->_opened) {
+ if (!$this->_opened && headers_sent()) {
$this->open();
}
list($usec, $sec) = explode(' ', microtime());
- $line = '<table border="0" cellpadding="2"
cellspacing="0">';
- $line .= '<tr align="left" valign="top">';
+ /* Build the output line that contains the log entry row. */
+ $line = '<tr align="left" valign="top">';
$line .= sprintf('<td>%s.%s</td><td>%s</td>',
strftime('%T', $sec), substr($usec, 2, 2),
$this->_ident);
$line .= sprintf('<td style="color: %s"
width="100%%">%s</td>',
$this->_colors[$priority], nl2br($message));
- $line .= '</tr></table>';
+ $line .= '</tr>';
$this->_writeln($line);