diff --git a/cake/libs/cake_log.php b/cake/libs/cake_log.php index 0161d47c6..3d7a29974 100644 --- a/cake/libs/cake_log.php +++ b/cake/libs/cake_log.php @@ -1,6 +1,4 @@ <?php -/* SVN FILE: $Id$ */ - /** * Logging. * @@ -9,20 +7,17 @@ * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org) - * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource - * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs * @since CakePHP(tm) v 0.2.9 - * @version $Revision$ - * @modifiedby $LastChangedBy$ - * @lastmodified $Date$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ @@ -102,5 +97,49 @@ class CakeLog { return $log->append($output); } } + +/** + * An error_handler that will log errors to file using CakeLog::write(); + * + * @param integer $code Code of error + * @param string $description Error description + * @param string $file File on which error occurred + * @param integer $line Line that triggered the error + * @param array $context Context + * @return void + **/ + function handleError($code, $description, $file = null, $line = null, $context = null) { + if ($code === 2048 || $code === 8192) { + return; + } + switch ($code) { + case E_PARSE: + case E_ERROR: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + case E_USER_ERROR: + $level = LOG_ERROR; + break; + case E_WARNING: + case E_USER_WARNING: + case E_COMPILE_WARNING: + case E_RECOVERABLE_ERROR: + $level = LOG_WARNING; + break; + case E_NOTICE: + case E_USER_NOTICE: + $level = LOG_NOTICE; + break; + default: + return false; + break; + } + $message = '(' . $code . ') ' . $description . ' in [' . $file . ', line ' . $line . ']'; + CakeLog::write($level, $message); + } +} + +if (!defined('DISABLE_DEFAULT_ERROR_HANDLING')) { + set_error_handler(array('CakeLog', 'handleError')); } ?> \ No newline at end of file diff --git a/cake/libs/configure.php b/cake/libs/configure.php index ab424c3ed..4e0ca6f5c 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -42,7 +42,7 @@ class Configure extends Object { * @var integer * @access public */ - var $debug = null; + var $debug = 0; /** * Returns a singleton instance of the Configure class. @@ -106,24 +106,30 @@ class Configure extends Object { } if (isset($config['debug'])) { + $reporting = 0; if ($_this->debug) { - error_reporting(E_ALL & ~E_DEPRECATED); - - if (function_exists('ini_set')) { - ini_set('display_errors', 1); - } - if (!class_exists('Debugger')) { require LIBS . 'debugger.php'; } + $reporting = E_ALL & ~E_DEPRECATED; + if (function_exists('ini_set')) { + ini_set('display_errors', 1); + } + } elseif (function_exists('ini_set')) { + ini_set('display_errors', 0); + } + + if (isset($_this->log) && $_this->log) { if (!class_exists('CakeLog')) { require LIBS . 'cake_log.php'; } - Configure::write('log', LOG_NOTICE); - } else { - error_reporting(0); - Configure::write('log', LOG_NOTICE); + if (is_integer($_this->log) && !$_this->debug) { + $reporting = $_this->log; + } else { + $reporting = E_ALL & ~E_DEPRECATED; + } } + error_reporting($reporting); } } @@ -143,13 +149,6 @@ class Configure extends Object { $_this =& Configure::getInstance(); if ($var === 'debug') { - if (!isset($_this->debug)) { - if (defined('DEBUG')) { - $_this->debug = DEBUG; - } else { - $_this->debug = 0; - } - } return $_this->debug; } diff --git a/cake/tests/cases/libs/cake_log.test.php b/cake/tests/cases/libs/cake_log.test.php index d548de6b8..68319cdab 100644 --- a/cake/tests/cases/libs/cake_log.test.php +++ b/cake/tests/cases/libs/cake_log.test.php @@ -1,6 +1,4 @@ <?php -/* SVN FILE: $Id$ */ - /** * CakeLogTest file * @@ -9,20 +7,17 @@ * PHP versions 4 and 5 * * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite> - * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * * Licensed under The Open Group Test Suite License * Redistributions of files must retain the above copyright notice. * * @filesource - * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests * @package cake * @subpackage cake.tests.cases.libs * @since CakePHP(tm) v 1.2.0.5432 - * @version $Revision$ - * @modifiedby $LastChangedBy$ - * @lastmodified $Date$ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ App::import('Core', 'Log'); @@ -54,5 +49,28 @@ class CakeLogTest extends CakeTestCase { $this->assertPattern('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result); unlink(LOGS . 'error.log'); } + +/** + * Test logging with the error handler. + * + * @return void + **/ + function testLoggingWithErrorHandling() { + @unlink(LOGS . 'debug.log'); + Configure::write('log', E_ALL & ~E_DEPRECATED); + Configure::write('debug', 0); + + $logger = new CakeLog(); + Debugger::invoke($logger); + $out .= ''; + + $result = file(LOGS . 'debug.log'); + $this->assertEqual(count($result), 1); + $this->assertPattern( + '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: \(8\) Undefined variable: out in \[.+ line \d{2}\]$/', + $result[0] + ); + @unlink(LOGS . 'debug.log'); + } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/configure.test.php b/cake/tests/cases/libs/configure.test.php index c4e88e2e7..c2ece8a6a 100644 --- a/cake/tests/cases/libs/configure.test.php +++ b/cake/tests/cases/libs/configure.test.php @@ -144,13 +144,15 @@ class ConfigureTest extends CakeTestCase { * @return void **/ function testSetErrorReportingLevel() { + Configure::write('log', false); + Configure::write('debug', 0); $result = ini_get('error_reporting'); $this->assertEqual($result, 0); Configure::write('debug', 2); $result = ini_get('error_reporting'); - $this->assertEqual($result, E_ALL); + $this->assertEqual($result, E_ALL & ~E_DEPRECATED); $result = ini_get('display_errors'); $this->assertEqual($result, 1); @@ -160,6 +162,28 @@ class ConfigureTest extends CakeTestCase { $this->assertEqual($result, 0); } +/** + * test that log and debug configure values interact well. + * + * @return void + **/ + function testInteractionOfDebugAndLog() { + Configure::write('log', false); + + Configure::write('debug', 0); + $this->assertEqual(ini_get('error_reporting'), 0); + $this->assertEqual(ini_get('display_errors'), 0); + + Configure::write('log', E_WARNING); + Configure::write('debug', 0); + $this->assertEqual(ini_get('error_reporting'), E_WARNING); + $this->assertEqual(ini_get('display_errors'), 0); + + Configure::write('debug', 2); + $this->assertEqual(ini_get('error_reporting'), E_ALL & ~E_DEPRECATED); + $this->assertEqual(ini_get('display_errors'), 1); + } + /** * testDelete method * diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 52b3e9ceb..d72df5ab8 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -28,8 +28,7 @@ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { define('CAKEPHP_UNIT_TEST_EXECUTION', 1); } -App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql')); -App::import('Model', 'App'); +App::import('Model', array('Model', 'DataSource', 'DboSource', 'DboMysql', 'App')); require_once dirname(dirname(__FILE__)) . DS . 'models.php'; /**