/**
  * Creates the log parser.
  *
  * You must pass a open, readable stream.
  *
  * The timeout specifies how long the parser will wait while not
  * receiving data, to automatically do a flush. If this is specified as -1,
  * it will wait indefinitely.
  *
  * @param resource $stream
  * @param int $timeout
  */
 public function __construct($stream, $bufferSize = 65536, $timeout = -1)
 {
     $this->buffer = new Buffer($stream, Buffer::READ);
     $this->bufferSize = $bufferSize;
     $this->buffer->onRead = array($this, 'readLines');
     $this->buffer->onError = function ($buffer, $err) {
         die('Buffer error: ' . $err);
     };
     $self = $this;
     $this->buffer->onEOF = array($this, 'readLines');
     if ($timeout !== -1) {
         $this->buffer->setTimeout($timeout);
     }
     $this->buffer->setReadBufferSize($bufferSize);
     $this->buffer->onTimeout = function ($buffer) use($self) {
         $self->readLines();
         // Need to re-enable the buffer. We're just using the timeout to
         // force a flush
         $buffer->enable();
     };
 }