9f19b3274a51973eaac6585a4b4dae6c7ebe80a8

Author: Mark Story

Date: 2009-11-10 00:54:25 -0500

Fixing file log parsing when Multiline log entries are made, or long entries containing datestamps.

diff --git a/controllers/components/toolbar.php b/controllers/components/toolbar.php index 2889241..a70b1e3 100644 --- a/controllers/components/toolbar.php +++ b/controllers/components/toolbar.php @@ -686,16 +686,34 @@ class LogPanel extends DebugPanel { * @return array */ function _parseFile($filename) { - $file =& new File($filename); - $contents = $file->read(); - $timePattern = '/(\d{4}-\d{2}\-\d{2}\s\d{1,2}\:\d{1,2}\:\d{1,2})/'; - $chunks = preg_split($timePattern, $contents, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); - for ($i = 0, $len = count($chunks); $i < $len; $i += 2) { - if (strtotime($chunks[$i]) < $this->startTime) { - unset($chunks[$i], $chunks[$i + 1]); + $fh = fopen($filename, 'r'); + $timePattern = '/^(\d{4}-\d{2}\-\d{2}\s\d{1,2}\:\d{1,2}\:\d{1,2})\s(.*)/'; + + $out = array(); + $entry = ''; + $done = false; + + while (!feof($fh)) { + $line = fgets($fh); + if (preg_match($timePattern, $line, $matches)) { + if (strtotime($matches[1]) < $this->startTime) { + continue; + } + $out[] = $matches[1]; + $out[] = $matches[2]; + } elseif (count($out) - 1 > 0) { + $currentIndex = count($out) - 1; + while (!feof($fh)) { + $line = fgets($fh); + if (preg_match($timePattern, $line)) { + break; + } + $out[$currentIndex] .= $line; + } } } - return array_values($chunks); + fclose($fh); + return $out; } } diff --git a/tests/cases/controllers/components/toolbar.test.php b/tests/cases/controllers/components/toolbar.test.php index c533a1f..016f95f 100644 --- a/tests/cases/controllers/components/toolbar.test.php +++ b/tests/cases/controllers/components/toolbar.test.php @@ -419,7 +419,7 @@ class DebugToolbarTestCase extends CakeTestCase { * @return void **/ function testLogPanel() { - usleep(20); + sleep(1); $this->Controller->log('This is a log I made this request'); $this->Controller->log('This is the second log I made this request'); $this->Controller->log('This time in the debug log!', LOG_DEBUG); @@ -442,6 +442,23 @@ class DebugToolbarTestCase extends CakeTestCase { $this->assertEqual(trim($result['content']['debug.log'][1]), 'Debug: This time in the debug log!'); $this->assertEqual(trim($result['content']['error.log'][1]), 'Error: This is a log I made this request'); + + $data = array( + 'Post' => array( + 'id' => 1, + 'title' => 'post!', + 'body' => 'some text here', + 'created' => '2009-11-07 23:23:23' + ), + 'Comment' => array( + 'id' => 23 + ) + ); + $this->Controller->log($data); + $this->Controller->Component->beforeRender($this->Controller); + $result = $this->Controller->viewVars['debugToolbarPanels']['log']; + $this->assertPattern('/\[created\] => 2009-11-07 23:23:23/', $result['content']['error.log'][5]); + $this->assertPattern('/\[Comment\] => Array/', $result['content']['error.log'][5]); } /** * Test that history state urls set prefix = null and admin = null so generated urls do not