Skip to content

Commit

Permalink
Automatic console logging streams were not respecting --quiet
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 6, 2012
1 parent 591022f commit ed4493d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
41 changes: 30 additions & 11 deletions lib/Cake/Console/Shell.php
Expand Up @@ -165,23 +165,13 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
if ($this->stdout == null) {
$this->stdout = new ConsoleOutput('php://stdout');
}
CakeLog::config('stdout', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info'),
'stream' => $this->stdout,
));
if ($this->stderr == null) {
$this->stderr = new ConsoleOutput('php://stderr');
}
CakeLog::config('stderr', array(
'engine' => 'ConsoleLog',
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
'stream' => $this->stderr,
));
if ($this->stdin == null) {
$this->stdin = new ConsoleInput('php://stdin');
}

$this->_useLogger();
$parent = get_parent_class($this);
if ($this->tasks !== null && $this->tasks !== false) {
$this->_mergeVars(array('tasks'), $parent, true);
Expand Down Expand Up @@ -379,6 +369,10 @@ public function runCommand($command, $argv) {
return false;
}

if (!empty($this->params['quiet'])) {
$this->_useLogger(false);
}

$this->command = $command;
if (!empty($this->params['help'])) {
return $this->_displayHelp($command);
Expand Down Expand Up @@ -825,4 +819,29 @@ protected function _pluginPath($pluginName) {
return current(App::path('plugins')) . $pluginName . DS;
}

/**
* Used to enable or disable logging stream output to stdout and stderr
* If you don't wish to see in your stdout or stderr everything that is logged
* through CakeLog, call this function with first param as false
*
* @param boolean $enable wheter to enable CakeLog output or not
* @return void
**/
protected function _useLogger($enable = true) {
if (!$enable) {
CakeLog::drop('stdout');
CakeLog::drop('stderr');
return;
}
CakeLog::config('stdout', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info'),
'stream' => $this->stdout,
));
CakeLog::config('stderr', array(
'engine' => 'ConsoleLog',
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
'stream' => $this->stderr,
));
}
}
34 changes: 33 additions & 1 deletion lib/Cake/Test/Case/Console/ShellTest.php
Expand Up @@ -80,6 +80,10 @@ public function mergeVars($properties, $class, $normalize = true) {
return $this->_mergeVars($properties, $class, $normalize);
}

public function useLogger($enable = true) {
$this->_useLogger($enable);
}

}

/**
Expand Down Expand Up @@ -825,7 +829,7 @@ public function testFileAndConsoleLogging() {
require_once CORE_TEST_CASES . DS . 'Log' . DS . 'Engine' . DS . 'ConsoleLogTest.php';
$mock = $this->getMock('ConsoleLog', array('write'), array(
array('types' => 'error'),
));
));
TestCakeLog::config('console', array(
'engine' => 'ConsoleLog',
'stream' => 'php://stderr',
Expand All @@ -840,4 +844,32 @@ public function testFileAndConsoleLogging() {
$this->assertContains($this->Shell->testMessage, $contents);
}

/**
* Tests that _useLogger works properly
*
* @return void
**/
public function testProtectedUseLogger() {
CakeLog::drop('stdout');
CakeLog::drop('stderr');
$this->Shell->useLogger(true);
$this->assertNotEmpty(CakeLog::stream('stdout'));
$this->assertNotEmpty(CakeLog::stream('stderr'));
$this->Shell->useLogger(false);
$this->assertFalse(CakeLog::stream('stdout'));
$this->assertFalse(CakeLog::stream('stderr'));
}

/**
* Test file and console and logging quiet output
*/
public function testQuietLog() {
$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->Shell = $this->getMock('ShellTestShell', array('_useLogger'), array($output, $error, $in));
$this->Shell->expects($this->once())->method('_useLogger')->with(false);
$this->Shell->runCommand('foo', array('--quiet'));
}

}

0 comments on commit ed4493d

Please sign in to comment.