Index: branches/5.3.x/core/kernel/application.php =================================================================== diff -u -N -r15483 -r15578 --- branches/5.3.x/core/kernel/application.php (.../application.php) (revision 15483) +++ branches/5.3.x/core/kernel/application.php (.../application.php) (revision 15578) @@ -1,6 +1,6 @@ Debugger->appendMemoryUsage('Application before Init:'); } - if ( !$this->isDebugMode() && !kUtil::constOn('DBG_ZEND_PRESENT') ) { - error_reporting(0); - ini_set('display_errors', 0); - } - - if ( !kUtil::constOn('DBG_ZEND_PRESENT') ) { - $error_handler = set_error_handler(Array (&$this, 'handleError')); - if ( $error_handler ) { - // wrap around previous error handler, if any was set - $this->errorHandlers[] = $error_handler; - } - - $exception_handler = set_exception_handler(Array (&$this, 'handleException')); - if ( $exception_handler ) { - // wrap around previous exception handler, if any was set - $this->exceptionHandlers[] = $exception_handler; - } - } - + $this->_logger = new kLogger($this->_logger); $this->Factory = new $factory_class(); $this->registerDefaultClasses(); $vars = kUtil::parseConfig(true); $db_class = isset($vars['Databases']) ? 'kDBLoadBalancer' : ($this->isDebugMode() ? 'kDBConnectionDebug' : 'kDBConnection'); - $this->Conn = $this->Factory->makeClass($db_class, Array (SQL_TYPE, Array (&$this, 'handleSQLError'))); + $this->Conn = $this->Factory->makeClass($db_class, Array (SQL_TYPE, Array ($this->_logger, 'handleSQLError'))); $this->Conn->setup($vars); $this->cacheManager = $this->makeClass('kCacheManager'); @@ -752,6 +726,7 @@ $this->registerClass('kTempTablesHandler', KERNEL_PATH . '/utility/temp_handler.php'); $this->registerClass('kValidator', KERNEL_PATH . '/utility/validator.php'); $this->registerClass('kOpenerStack', KERNEL_PATH . '/utility/opener_stack.php'); + $this->registerClass('kLogger', KERNEL_PATH . '/utility/logger.php'); $this->registerClass('kUnitConfigReader', KERNEL_PATH . '/utility/unit_config_reader.php'); @@ -2385,224 +2360,15 @@ * @param string $sql * @return bool * @access public + * @throws Exception + * @deprecated */ public function handleSQLError($code, $msg, $sql) { - if ( isset($this->Debugger) ) { - $long_error_msg = '' . $msg . ' (' . $code . ')
SQL: ' . $this->Debugger->formatSQL($sql); - $long_id = $this->Debugger->mapLongError($long_error_msg); - $error_msg = mb_substr($msg . ' (' . $code . ') [' . $sql . ']', 0, 1000) . ' #' . $long_id; - - if ( kUtil::constOn('DBG_SQL_FAILURE') && !defined('IS_INSTALL') ) { - throw new Exception($error_msg); - } - else { - $this->Debugger->appendTrace(); - } - } - else { - // when not debug mode, then fatal database query won't break anything - $error_msg = 'SQL Error in sql: ' . $sql . ', code ' . $code . ' (' . $msg . ')'; - } - - trigger_error($error_msg, E_USER_WARNING); - - return true; + return $this->_logger->handleSQLError($code, $msg, $sql); } /** - * Default error handler - * - * @param int $errno - * @param string $errstr - * @param string $errfile - * @param int $errline - * @param Array $errcontext - * @return bool - * @access public - */ - public function handleError($errno, $errstr, $errfile = null, $errline = null, $errcontext = Array ()) - { - $this->errorLogSilent($errno, $errstr, $errfile, $errline); - - $debug_mode = defined('DEBUG_MODE') && DEBUG_MODE; - $skip_reporting = defined('DBG_SKIP_REPORTING') && DBG_SKIP_REPORTING; - - if ( !$this->errorHandlers || ($debug_mode && $skip_reporting) ) { - // when debugger absent OR it's present, but we actually can't see it's error report (e.g. during ajax request) - if ( $errno == E_USER_ERROR ) { - $this->errorDisplayFatal('Fatal Error: ' . "{$errstr} in {$errfile} on line {$errline}"); - } - - if ( !$this->errorHandlers ) { - return true; - } - } - - $res = false; - /* @var $handler Closure */ - - foreach ($this->errorHandlers as $handler) { - if ( is_array($handler) ) { - $object =& $handler[0]; - $method = $handler[1]; - $res = $object->$method($errno, $errstr, $errfile, $errline, $errcontext); - } - else { - $res = $handler($errno, $errstr, $errfile, $errline, $errcontext); - } - } - - return $res; - } - - /** - * Handles exception - * - * @param Exception $exception - * @return bool - * @access public - */ - public function handleException($exception) - { - // transform exception to regular error (no need to rewrite existing error handlers) - $errno = $exception->getCode(); - $errstr = $exception->getMessage(); - $errfile = $exception->getFile(); - $errline = $exception->getLine(); - - $this->errorLogSilent($errno, $errstr, $errfile, $errline); - - $debug_mode = defined('DEBUG_MODE') && DEBUG_MODE; - $skip_reporting = defined('DBG_SKIP_REPORTING') && DBG_SKIP_REPORTING; - - if ( $exception instanceof kRedirectException ) { - /* @var $exception kRedirectException */ - - $exception->run(); - } - - if ( !$this->exceptionHandlers || ($debug_mode && $skip_reporting) ) { - // when debugger absent OR it's present, but we actually can't see it's error report (e.g. during ajax request) - $this->errorDisplayFatal('' . get_class($exception) . ': ' . "{$errstr} in {$errfile} on line {$errline}"); - - if ( !$this->exceptionHandlers ) { - return true; - } - } - - $res = false; - /* @var $handler Closure */ - - foreach ($this->exceptionHandlers as $handler) { - if ( is_array($handler) ) { - $object =& $handler[0]; - $method = $handler[1]; - $res = $object->$method($exception); - } - else { - $res = $handler($exception); - } - } - - return $res; - } - - /** - * Silently saves each given error message to "silent_log.txt" file, when silent log mode is enabled - * @param int $errno - * @param string $errstr - * @param string $errfile - * @param int $errline - * @return void - * @access protected - */ - protected function errorLogSilent($errno, $errstr = '', $errfile = '', $errline = null) - { - if ( !defined('SILENT_LOG') || !SILENT_LOG ) { - return; - } - - if ( !(defined('DBG_IGNORE_STRICT_ERRORS') && DBG_IGNORE_STRICT_ERRORS && defined('E_STRICT') && ($errno == E_STRICT)) ) { - $time = adodb_date('d/m/Y H:i:s'); - - $fp = fopen((defined('RESTRICTED') ? RESTRICTED : FULL_PATH) . '/silent_log.txt', 'a'); - fwrite($fp, '[' . $time . '] #' . $errno . ': ' . strip_tags($errstr) . ' in [' . $errfile . '] on line ' . $errline . "\n"); - fclose($fp); - } - } - - /** - * Displays div with given error message - * - * @param string $msg - * @return void - * @access protected - */ - protected function errorDisplayFatal($msg) - { - $margin = $this->isAdmin ? '8px' : 'auto'; - echo '
' . $msg . '
'; - exit; - } - - /** - * Prints trace, when debug mode is not available - * - * @param bool $return_result - * @param int $skip_levels - * @return string - * @access public - */ - public function printTrace($return_result = false, $skip_levels = 1) - { - $ret = Array (); - $trace = debug_backtrace(false); - - for ($i = 0; $i < $skip_levels; $i++) { - array_shift($trace); - } - - foreach ($trace as $level => $trace_info) { - if ( isset($trace_info['class']) ) { - $object = $trace_info['class']; - } - elseif ( isset($trace_info['object']) ) { - $object = get_class($trace_info['object']); - } - else { - $object = ''; - } - - $args = ''; - $type = isset($trace_info['type']) ? $trace_info['type'] : ''; - - if ( isset($trace_info['args']) ) { - foreach ($trace_info['args'] as $argument) { - if ( is_object($argument) ) { - $args .= get_class($argument) . ' instance, '; - } - else { - $args .= is_array($argument) ? 'Array' : substr($argument, 0, 10) . ' ..., '; - } - } - - $args = substr($args, 0, -2); - } - - $ret[] = '#' . $level . ' ' . $object . $type . $trace_info['function'] . '(' . $args . ') called at [' . $trace_info['file'] . ':' . $trace_info['line'] . ']'; - } - - if ( $return_result ) { - return implode("\n", $ret); - } - - echo implode("\n", $ret); - - return ''; - } - - /** * Returns & blocks next ResourceId available in system * * @return int @@ -3219,4 +2985,38 @@ $already_set = true; header($header); } + + /** + * Posts message to event log + * + * @param string $message + * @param int $code + * @param bool $write_now Allows further customization of log record by returning kLog object + * @return bool|int|kLogger + * @access public + */ + public function log($message, $code = null, $write_now = false) + { + $log = $this->_logger->prepare($message, $code)->addSource($this->_logger->createTrace(null, 1)); + + if ( $write_now ) { + return $log->write(); + } + + return $log; + } + + /** + * Deletes log with given id from database or disk, when database isn't available + * + * @param int $unique_id + * @param int $storage_medium + * @return void + * @access public + * @throws InvalidArgumentException + */ + public function deleteLog($unique_id, $storage_medium = kLogger::LS_AUTOMATIC) + { + $this->_logger->delete($unique_id, $storage_medium); + } } \ No newline at end of file