$factory = new CommandFactory( $limits, $cgroup );
$factory->setLogger( LoggerFactory::getInstance( 'exec' ) );
+ $factory->logStderr();
return $factory;
},
private $method;
/** @var bool */
- private $useStderr = false;
+ private $doIncludeStderr = false;
+
+ /** @var bool */
+ private $doLogStderr = false;
/** @var bool */
private $everExecuted = false;
* @return $this
*/
public function includeStderr( $yesno = true ) {
- $this->useStderr = $yesno;
+ $this->doIncludeStderr = $yesno;
+
+ return $this;
+ }
+
+ /**
+ * When enabled, text sent to stderr will be logged with a level of 'error'.
+ *
+ * @param bool $yesno
+ * @return $this
+ */
+ public function logStderr( $yesno = true ) {
+ $this->doLogStderr = $yesno;
return $this;
}
$cmd = '/bin/bash ' . escapeshellarg( __DIR__ . '/limit.sh' ) . ' ' .
escapeshellarg( $cmd ) . ' ' .
escapeshellarg(
- "MW_INCLUDE_STDERR=" . ( $this->useStderr ? '1' : '' ) . ';' .
+ "MW_INCLUDE_STDERR=" . ( $this->doIncludeStderr ? '1' : '' ) . ';' .
"MW_CPU_LIMIT=$time; " .
'MW_CGROUP=' . escapeshellarg( $this->cgroup ) . '; ' .
"MW_MEM_LIMIT=$mem; " .
$useLogPipe = true;
}
}
- if ( !$useLogPipe && $this->useStderr ) {
+ if ( !$useLogPipe && $this->doIncludeStderr ) {
$cmd .= ' 2>&1';
}
$this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
}
+ if ( $errBuffer && $this->doLogStderr ) {
+ $this->logger->error( "Error running {command}: {error}", [
+ 'command' => $cmd,
+ 'error' => $errBuffer,
+ 'exitcode' => $retval,
+ 'exception' => new Exception( 'Shell error' ),
+ ] );
+ }
+
return new Result( $retval, $outBuffer, $errBuffer );
}
}
/** @var string|bool */
private $cgroup;
+ /** @var bool */
+ private $doLogStderr = false;
+
/**
* Constructor
*
$this->setLogger( new NullLogger() );
}
+ /**
+ * When enabled, text sent to stderr will be logged with a level of 'error'.
+ *
+ * @param bool $yesno
+ * @see Command::logStderr
+ */
+ public function logStderr( $yesno = true ) {
+ $this->doLogStderr = $yesno;
+ }
+
/**
* Instantiates a new Command
*
return $command
->limits( $this->limits )
- ->cgroup( $this->cgroup );
+ ->cgroup( $this->cgroup )
+ ->logStderr( $this->doLogStderr );
}
}
$factory = new CommandFactory( $limits, $cgroup );
$factory->setLogger( $logger );
+ $factory->logStderr();
$command = $factory->create();
$wrapper = TestingAccessWrapper::newFromObject( $command );
$this->assertSame( $logger, $wrapper->logger );
$this->assertSame( $cgroup, $wrapper->cgroup );
$this->assertSame( $limits, $wrapper->limits );
+ $this->assertTrue( $wrapper->doLogStderr );
}
}
$this->assertEquals( 333333, strlen( $output ) );
}
}
+
+ public function testLogStderr() {
+ $this->requirePosix();
+
+ $logger = new TestLogger( true, function ( $message, $level, $context ) {
+ return $level === Psr\Log\LogLevel::ERROR ? '1' : null;
+ }, true );
+ $command = new Command();
+ $command->setLogger( $logger );
+ $command->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' );
+ $command->execute();
+ $this->assertEmpty( $logger->getBuffer() );
+
+ $command = new Command();
+ $command->setLogger( $logger );
+ $command->logStderr();
+ $command->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' );
+ $command->execute();
+ $this->assertSame( 1, count( $logger->getBuffer() ) );
+ $this->assertSame( trim( $logger->getBuffer()[0][2]['error'] ), 'ThisIsStderr' );
+ }
}