diff --git a/cake/libs/debugger.php b/cake/libs/debugger.php index 30c44d398..b35eb7c27 100644 --- a/cake/libs/debugger.php +++ b/cake/libs/debugger.php @@ -112,6 +112,22 @@ class Debugger extends Object { $_this = Debugger::getInstance(); pr($_this->exportVar($var)); } +/** + * neatly logs a given var +*/ + function log($var, $level = 7) { + $_this = Debugger::getInstance(); + $trace = $_this->trace(array('start' => 1, 'depth' => 2, 'format' => 'array')); + $source = null; + + if (is_object($trace[0]['object']) && isset($trace[0]['object']->_reporter->_test_stack)) { + $stack = $trace[0]['object']->_reporter->_test_stack; + $source = "[". $stack[0].", ". $stack[2] ."::" . $stack[3] ."()]\n"; + } + + CakeLog::write($level, $source . $_this->exportVar($var)); + } + /** * Overrides PHP's default error handling * @@ -255,6 +271,8 @@ class Debugger extends Object { $back[] = array('file' => $trace['file'], 'line' => $trace['line']); } elseif (empty($options['format'])) { $back[] = $function . ' - ' . Debugger::trimPath($trace['file']) . ', line ' . $trace['line']; + } else { + $back[] = $trace; } } @@ -438,7 +456,7 @@ class Debugger extends Object { $files = $_this->trace(array('start' => 2, 'format' => 'points')); $listing = $_this->excerpt($files[0]['file'], $files[0]['line'] - 1, 1); - $trace = $_this->trace(array('start' => 2)); + $trace = $_this->trace(array('start' => 2, 'depth' => '20')); $context = array(); foreach ((array)$kontext as $var => $value) { diff --git a/cake/tests/cases/libs/debugger.test.php b/cake/tests/cases/libs/debugger.test.php index f5170090d..d6089f1bd 100644 --- a/cake/tests/cases/libs/debugger.test.php +++ b/cake/tests/cases/libs/debugger.test.php @@ -177,5 +177,33 @@ TestManager::runTestCase() - CORE/cake/tests/lib/test_manager.php, line 93 $this->assertEqual($result, $expected); } + function testLog() { + if (file_exists(LOGS . 'debug.log')) { + unlink(LOGS . 'debug.log'); + } + + Debugger::log('cool'); + $result = file_get_contents(LOGS . 'debug.log'); + $this->assertPattern('/DebuggerTest::testLog/', $result); + $this->assertPattern('/"cool"/', $result); + + unlink(TMP . 'logs' . DS . 'debug.log'); + + Debugger::log(array('whatever', 'here')); + $result = file_get_contents(TMP . 'logs' . DS . 'debug.log'); + + $this->assertPattern('/DebuggerTest::testLog/', $result); + $this->assertPattern('/array/', $result); + $this->assertPattern('/"whatever",/', $result); + $this->assertPattern('/"here"/', $result); + } + + function setUp() { + Configure::write('log', false); + } + function tearDown() { + Configure::write('log', true); + } + } ?> \ No newline at end of file