Skip to content
This repository
Browse code

Automatic console logging streams were not respecting --quiet

  • Loading branch information...
commit ed4493da0c9492d4d91eda000924fb7c582b9dc8 1 parent 591022f
José Lorenzo Rodríguez authored July 06, 2012
41  lib/Cake/Console/Shell.php
@@ -165,23 +165,13 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
165 165
 		if ($this->stdout == null) {
166 166
 			$this->stdout = new ConsoleOutput('php://stdout');
167 167
 		}
168  
-		CakeLog::config('stdout', array(
169  
-			'engine' => 'ConsoleLog',
170  
-			'types' => array('notice', 'info'),
171  
-			'stream' => $this->stdout,
172  
-		));
173 168
 		if ($this->stderr == null) {
174 169
 			$this->stderr = new ConsoleOutput('php://stderr');
175 170
 		}
176  
-		CakeLog::config('stderr', array(
177  
-			'engine' => 'ConsoleLog',
178  
-			'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
179  
-			'stream' => $this->stderr,
180  
-		));
181 171
 		if ($this->stdin == null) {
182 172
 			$this->stdin = new ConsoleInput('php://stdin');
183 173
 		}
184  
-
  174
+		$this->_useLogger();
185 175
 		$parent = get_parent_class($this);
186 176
 		if ($this->tasks !== null && $this->tasks !== false) {
187 177
 			$this->_mergeVars(array('tasks'), $parent, true);
@@ -379,6 +369,10 @@ public function runCommand($command, $argv) {
379 369
 			return false;
380 370
 		}
381 371
 
  372
+		if (!empty($this->params['quiet'])) {
  373
+			$this->_useLogger(false);
  374
+		}
  375
+
382 376
 		$this->command = $command;
383 377
 		if (!empty($this->params['help'])) {
384 378
 			return $this->_displayHelp($command);
@@ -825,4 +819,29 @@ protected function _pluginPath($pluginName) {
825 819
 		return current(App::path('plugins')) . $pluginName . DS;
826 820
 	}
827 821
 
  822
+/**
  823
+ * Used to enable or disable logging stream output to stdout and stderr
  824
+ * If you don't wish to see in your stdout or stderr everything that is logged
  825
+ * through CakeLog, call this function with first param as false
  826
+ *
  827
+ * @param boolean $enable wheter to enable CakeLog output or not
  828
+ * @return void
  829
+ **/
  830
+	protected function _useLogger($enable = true) {
  831
+		if (!$enable) {
  832
+			CakeLog::drop('stdout');
  833
+			CakeLog::drop('stderr');
  834
+			return;
  835
+		}
  836
+		CakeLog::config('stdout', array(
  837
+			'engine' => 'ConsoleLog',
  838
+			'types' => array('notice', 'info'),
  839
+			'stream' => $this->stdout,
  840
+		));
  841
+		CakeLog::config('stderr', array(
  842
+			'engine' => 'ConsoleLog',
  843
+			'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
  844
+			'stream' => $this->stderr,
  845
+		));
  846
+	}
828 847
 }
34  lib/Cake/Test/Case/Console/ShellTest.php
@@ -80,6 +80,10 @@ public function mergeVars($properties, $class, $normalize = true) {
80 80
 		return $this->_mergeVars($properties, $class, $normalize);
81 81
 	}
82 82
 
  83
+	public function useLogger($enable = true) {
  84
+		$this->_useLogger($enable);
  85
+	}
  86
+
83 87
 }
84 88
 
85 89
 /**
@@ -825,7 +829,7 @@ public function testFileAndConsoleLogging() {
825 829
 		require_once CORE_TEST_CASES . DS . 'Log' . DS . 'Engine' . DS . 'ConsoleLogTest.php';
826 830
 		$mock = $this->getMock('ConsoleLog', array('write'), array(
827 831
 			array('types' => 'error'),
828  
-			));
  832
+		));
829 833
 		TestCakeLog::config('console', array(
830 834
 			'engine' => 'ConsoleLog',
831 835
 			'stream' => 'php://stderr',
@@ -840,4 +844,32 @@ public function testFileAndConsoleLogging() {
840 844
 		$this->assertContains($this->Shell->testMessage, $contents);
841 845
 	}
842 846
 
  847
+/**
  848
+ * Tests that _useLogger works properly
  849
+ *
  850
+ * @return void
  851
+ **/
  852
+	public function testProtectedUseLogger() {
  853
+		CakeLog::drop('stdout');
  854
+		CakeLog::drop('stderr');
  855
+		$this->Shell->useLogger(true);
  856
+		$this->assertNotEmpty(CakeLog::stream('stdout'));
  857
+		$this->assertNotEmpty(CakeLog::stream('stderr'));
  858
+		$this->Shell->useLogger(false);
  859
+		$this->assertFalse(CakeLog::stream('stdout'));
  860
+		$this->assertFalse(CakeLog::stream('stderr'));
  861
+	}
  862
+
  863
+/**
  864
+ * Test file and console and logging quiet output
  865
+ */
  866
+	public function testQuietLog() {
  867
+		$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
  868
+		$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
  869
+		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
  870
+		$this->Shell = $this->getMock('ShellTestShell', array('_useLogger'), array($output, $error, $in));
  871
+		$this->Shell->expects($this->once())->method('_useLogger')->with(false);
  872
+		$this->Shell->runCommand('foo', array('--quiet'));
  873
+	}
  874
+
843 875
 }

0 notes on commit ed4493d

Please sign in to comment.
Something went wrong with that request. Please try again.