<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Exceptions Class
* 異常處理類
* @package CodeIgniter
* @subpackage Libraries
* @category Exceptions
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/libraries/exceptions.html
*/
class CI_Exceptions {
var $action;
var $severity;
var $message;
var $filename;
var $line;
/**
* Nesting level of the output buffering mechanism
* 輸出緩存機制的嵌套級別
* @var int
* @access public
*/
var $ob_level;
/**
* List if available error levels
* 有效地錯誤級別列表
* @var array
* @access public
*/
var $levels = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice'
);
/**
* Constructor
*/
public function __construct()
{
$this->ob_level = ob_get_level();
// Note: Do not log messages from this constructor.
}
// --------------------------------------------------------------------
/**
* Exception Logger
* 異常日誌的記錄
* This function logs PHP generated error messages
* 這個函數將記錄php產生的錯誤消息
* @access private
* @param string the error severity
* @param string the error string
* @param string the error filepath
* @param string the error line number
* @return string
*/
function log_exception($severity, $message, $filepath, $line)
{
// 根據錯誤級別設置錯誤嚴重性
$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE);
}
// --------------------------------------------------------------------
/**
* 404 Page Not Found Handler
* 404 頁面
* @access private
* @param string the page
* @param bool log error yes/no
* @return string
*/
function show_404($page = '', $log_error = TRUE)
{
$heading = "404 Page Not Found";
$message = "The page you requested was not found.";
// By default we log this, but allow a dev to skip it
if ($log_error)
{
log_message('error', '404 Page Not Found --> '.$page);
}
echo $this->show_error($heading, $message, 'error_404', 404);
exit;
}
// --------------------------------------------------------------------
/**
* General Error Page
* 一般的錯誤頁面
* This function takes an error message as input
* (either as a string or an array) and displays
* it using the specified template.
* 這個函數將使用指定的模板顯示一個錯誤消息
* @access private
* @param string the heading
* @param string the message
* @param string the template name
* @param int the status code
* @return string
*/
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
// 默認是500
set_status_header($status_code);
// 組合錯誤消息
$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
/*
* 如果還沒看過core/Loader.php,下面這個判斷可能讓人有點迷惑。
* ob_get_level()是取得當前緩衝機制的嵌套級別。(緩衝是可以一層嵌一層的。)
* 右邊的$this->ob_level是在__construct()裏面同樣通過ob_get_level()被賦值的。
* 也就是說,有可能出現:Exception組件被加載時(也就是應用剛開始運行時)
* 的緩衝級別(其實也就是程序最開始的時候的緩衝級別,那時
* 候是還沒有ob_start()過的),與發生錯誤的時候的緩衝級別相差1。
* 在控制器執行$this->load->view("xxx");的時候,實質,
* Loader引入並執行這個視圖文件的時候,是先把緩衝打開,即
* 先ob_start(),所有輸出放到緩衝區(詳見:core/Loader.php中的_ci_load()),然後再由Output處理輸出。
* 因此,如果是在視圖文件發生錯誤,則就會出現緩衝級別相差1的情況
* 這就會導致輸出的內容不僅僅只是這個的錯誤信息
* 此時先把輸出的內容給flush出來,然後再把錯誤信息輸出。
*
*/
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/'.$template.'.php');
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
// --------------------------------------------------------------------
/**
* Native PHP error handler
* 本地php錯誤處理
* @access private
* @param string the error severity
* @param string the error string
* @param string the error filepath
* @param string the error line number
* @return string
*/
function show_php_error($severity, $message, $filepath, $line)
{
$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
$filepath = str_replace("\\", "/", $filepath);
// For safety reasons we do not show the full file path
if (FALSE !== strpos($filepath, '/'))
{
$x = explode('/', $filepath);
$filepath = $x[count($x)-2].'/'.end($x);
}
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/error_php.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
}
// END Exceptions Class
/* End of file Exceptions.php */
/* Location: ./system/core/Exceptions.php */
CI框架源碼閱讀---------Exceptions
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.