Permalink
Browse files

Merge branch 'hotfix/zf2-541' into develop

Close #2370
  • Loading branch information...
2 parents e98c3f4 + 1a57370 commit 7abb3e94cb12b956a9d9e860226212bdd321b130 @weierophinney weierophinney committed Sep 18, 2012
@@ -13,6 +13,7 @@
use Zend\Log\Exception;
use Zend\Log\Filter;
use Zend\Log\Formatter\FormatterInterface as Formatter;
+use Zend\Stdlib\ErrorHandler;
/**
* @category Zend
@@ -43,6 +44,20 @@
protected $formatter;
/**
+ * Use Zend\Stdlib\ErrorHandler to report errors during calls to write
+ *
+ * @var bool
+ */
+ protected $convertWriteErrorsToExceptions = true;
+
+ /**
+ * Error level passed to Zend\Stdlib\ErrorHandler::start for errors reported during calls to write
+ *
+ * @var bool
+ */
+ protected $errorsToExceptionsConversionLevel = E_WARNING;
+
+ /**
* Add a filter specific to this writer.
*
* @param int|string|Filter\FilterInterface $filter
@@ -134,8 +149,30 @@ public function write(array $event)
}
}
- // exception occurs on error
- $this->doWrite($event);
+ $errorHandlerStarted = false;
+
+ if ($this->convertWriteErrorsToExceptions && !ErrorHandler::started()) {
+ ErrorHandler::start($this->errorsToExceptionsConversionLevel);
+ $errorHandlerStarted = true;
+ }
+
+ try {
+ $this->doWrite($event);
+ } catch (\Exception $e) {
+ if ($errorHandlerStarted) {
+ ErrorHandler::stop();
+ $errorHandlerStarted = false;
+ }
+ throw $e;
+ }
+
+ if ($errorHandlerStarted) {
+ $error = ErrorHandler::stop();
+ $errorHandlerStarted = false;
+ if ($error) {
+ throw new Exception\RuntimeException("Unable to write", 0, $error);
+ }
+ }
}
/**
@@ -151,6 +188,16 @@ public function setFormatter(Formatter $formatter)
}
/**
+ * Set convert write errors to exception flag
+ *
+ * @param bool $ignoreWriteErrors
+ */
+ public function setConvertWriteErrorsToExceptions($convertErrors)
+ {
+ $this->convertWriteErrorsToExceptions = $convertErrors;
+ }
+
+ /**
* Perform shutdown activities such as closing open resources
*
* @return void
@@ -109,13 +109,7 @@ public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
protected function doWrite(array $event)
{
$line = $this->formatter->format($event) . $this->logSeparator;
-
- ErrorHandler::start(E_WARNING);
- $result = fwrite($this->stream, $line);
- $error = ErrorHandler::stop();
- if (false === $result) {
- throw new Exception\RuntimeException("Unable to write to stream", 0, $error);
- }
+ fwrite($this->stream, $line);
}
/**
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Log
+ */
+
+namespace ZendTest\Log\TestAsset;
+
+use Zend\Log\Writer\AbstractWriter;
+
+class ErrorGeneratingWriter extends AbstractWriter
+{
+ protected function doWrite(array $event)
+ {
+ $stream = fopen("php://memory", "r");
+ fclose($stream);
+ fwrite($stream, "test");
+ }
+}
@@ -11,6 +11,7 @@
namespace ZendTest\Log\Writer;
use ZendTest\Log\TestAsset\ConcreteWriter;
+use ZendTest\Log\TestAsset\ErrorGeneratingWriter;
use Zend\Log\Formatter\Simple as SimpleFormatter;
use Zend\Log\Filter\Regex as RegexFilter;
@@ -69,4 +70,15 @@ public function testFluentInterface()
$this->assertTrue($instance instanceof ConcreteWriter);
}
-}
+
+ public function testConvertErrorsToException()
+ {
+ $writer = new ErrorGeneratingWriter();
+ $this->setExpectedException('Zend\Log\Exception\RuntimeException');
+ $writer->write(array('message' => 'test'));
+
+ $writer->setConvertWriteErrorsToExceptions(false);
+ $this->setExpectedException('PHPUnit_Framework_Error_Warning');
+ $writer->write(array('message' => 'test'));
+ }
+}

0 comments on commit 7abb3e9

Please sign in to comment.