Re: [PATCH] bug in pear/Log/Log/composite.php
| From: | Jon Parise | Date: | Thu, 03 Apr 2003 07:51:32 +0000 |
| Subject: | Re: [PATCH] bug in pear/Log/Log/composite.php | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-14835@lists.php.net to get a copy of this message | ||
On Tue, Mar 25, 2003 at 04:30:28PM +0100, Roman Neuhauser wrote:
> There is a bug in the Log_composite class: although it stores
> references, it acts on their copies: all messages sent to the children
> enumerate the list with foreach().
I did a bit of testing (using Benchmark::Timer), and the attached
patch seems to implement the faster, simplest behavior. Here's my
test:
require_once 'Log.php';
require_once 'Benchmark/Timer.php';
$conf = array('mode' => 0600, 'timeFormat' => '%X %x');
$file1 = &Log::singleton('file', 'out1.log', 'test1', $conf,
LOG_INFO);
$file2 = &Log::singleton('file', 'out2.log', 'test2', $conf,
LOG_INFO);
$log = &Log::singleton('composite');
$log->addChild($file1);
$log->addChild($file2);
$timer->setMarker('Start');
for ($i = 0; $i < 10000; $i++) {
$log->log("foo: $i", LOG_INFO);
}
$timer->setMarker('Stop');
$log->close();
If everything looks reasonable, I'll commit the improved version of
Log/composite.php.
--
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)
Index: Log/composite.php =================================================================== RCS file: /repository/pear/Log/Log/composite.php,v retrieving revision 1.10 diff -u -r1.10 composite.php --- Log/composite.php 3 Apr 2003 07:23:47 -0000 1.10 +++ Log/composite.php 3 Apr 2003 07:45:41 -0000 @@ -47,8 +47,8 @@ function open() { if (!$this->_opened) { - foreach ($this->_children as $child) { - $child->open(); + foreach ($this->_children as $id => $child) { + $this->_children[$id]->open(); } } } @@ -61,8 +61,8 @@ function close() { if ($this->_opened) { - foreach ($this->_children as $child) { - $child->close(); + foreach ($this->_children as $id => $child) { + $this->_children[$id]->close(); } } } @@ -85,8 +85,8 @@ */ function log($message, $priority = PEAR_LOG_INFO) { - foreach ($this->_children as $child) { - $child->log($message, $priority); + foreach ($this->_children as $id => $child) { + $this->_children[$id]->log($message, $priority); } $this->notifyAll(array('priority' => $priority, 'message' => $message)); @@ -122,8 +122,9 @@ return false; } - $child->_childID = uniqid(rand()); - $this->_children[$child->_childID] = &$child; + $id = md5(microtime()); + $child->_id = $id; + $this->_children[$id] = &$child; return true; } @@ -139,11 +140,11 @@ */ function removeChild($child) { - if (!isset($this->_children[$child->_childID])) { + if (!isset($this->_children[$child->_id])) { return false; } - unset($this->_children[$child->_childID]); + unset($this->_children[$child->_id]); return true; }
Index: Log/composite.php =================================================================== RCS file: /repository/pear/Log/Log/composite.php,v retrieving revision 1.10 diff -u -r1.10 composite.php --- Log/composite.php 3 Apr 2003 07:23:47 -0000 1.10 +++ Log/composite.php 3 Apr 2003 07:45:41 -0000 @@ -47,8 +47,8 @@ function open() { if (!$this->_opened) { - foreach ($this->_children as $child) { - $child->open(); + foreach ($this->_children as $id => $child) { + $this->_children[$id]->open(); } } } @@ -61,8 +61,8 @@ function close() { if ($this->_opened) { - foreach ($this->_children as $child) { - $child->close(); + foreach ($this->_children as $id => $child) { + $this->_children[$id]->close(); } } } @@ -85,8 +85,8 @@ */ function log($message, $priority = PEAR_LOG_INFO) { - foreach ($this->_children as $child) { - $child->log($message, $priority); + foreach ($this->_children as $id => $child) { + $this->_children[$id]->log($message, $priority); } $this->notifyAll(array('priority' => $priority, 'message' => $message)); @@ -122,8 +122,9 @@ return false; } - $child->_childID = uniqid(rand()); - $this->_children[$child->_childID] = &$child; + $id = md5(microtime()); + $child->_id = $id; + $this->_children[$id] = &$child; return true; } @@ -139,11 +140,11 @@ */ function removeChild($child) { - if (!isset($this->_children[$child->_childID])) { + if (!isset($this->_children[$child->_id])) { return false; } - unset($this->_children[$child->_childID]); + unset($this->_children[$child->_id]); return true; }