zoukankan      html  css  js  c++  java
  • CI框架 -- 核心文件 之 Exceptions.php

    使用CI框架,我们通常使用一下三个函数处理错误:

    show_error('消息' [, int $status_code = 500 ] )
    show_404('页面' [, 'log_error'])
    log_message('级别', '消息'),有一下三种错误信息:
    1. 错误类型的消息。 这种是真正的错误消息. 例如PHP错误或者用户错误。 
    2. 调试类型的消息。 这种是用来帮助调试的消息。 例如, 如果当一个类被初始化时,你可以将这个初始化纪录下来,然后用于调试。 
    3. 信息类型的消息。 这种是最低优先级别的消息,它只是简单的提供了关于运行的一些信息。 CodeIgniter 不会自动产生任何信息类型的消息,但是你可能会在你的程序里使用它  
      1 class CI_Exceptions {
      2     protected $action;
      3     protected $severity;
      4     protected $message;
      5     protected $filename;
      6     protected $line;
      7  
      8     //嵌套的输出缓冲处理程序的级别;如果输出缓冲区不起作用,返回零。
      9     protected $ob_level;
     10  
     11     //PHP error level
     12     protected $levels = array(
     13                 E_ERROR                =>    'Error',//致命错误
     14                 E_WARNING            =>    'Warning',//非致命运行错误
     15                 E_PARSE                =>    'Parsing Error',//编译错误
     16                 E_NOTICE            =>    'Notice',//notice错误
     17                 E_CORE_ERROR        =>    'Core Error',//PHP启动时致命错误
     18                 E_CORE_WARNING        =>    'Core Warning',     //PHP启动时非致命错误
     19                 E_COMPILE_ERROR        =>    'Compile Error',//致命的编译错误
     20                 E_COMPILE_WARNING    =>    'Compile Warning',//非致命的编译错误
     21                 E_USER_ERROR        =>    'User Error',//致命的用户生成错误
     22                 E_USER_WARNING        =>    'User Warning',//非致命的用户生成警告
     23                 E_USER_NOTICE        =>    'User Notice',//用户生成的通知
     24                 E_STRICT            =>    'Runtime Notice'//Run-time通知,提高代码稳定可靠性
     25         );
     26  
     27  
     28     public function __construct()
     29     {
     30                 //获取嵌套的输出缓冲处理程序的级别
     31         $this->ob_level = ob_get_level();
     32     }
     33  
     34     // --------------------------------------------------------------------
     35  
     36     //记录错误日志
     37     function log_exception($severity, $message, $filepath, $line)
     38     {
     39         $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
     40  
     41         log_message('error', 'Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line, TRUE);
     42     }
     43  
     44     // --------------------------------------------------------------------
     45  
     46     /**
     47      * 可以看出show_404只是show_error的一种特殊情况
     48      */
     49     function show_404($page = '', $log_error = TRUE)
     50     {
     51         $heading = "404 Page Not Found";
     52         $message = "The page you requested was not found.";
     53  
     54         // 是否需要记录日志
     55         if ($log_error)
     56         {
     57             log_message('error', '404 Page Not Found --> '.$page);
     58         }
     59  
     60         echo $this->show_error($heading, $message, 'error_404', 404);
     61         exit;
     62     }
     63  
     64     // --------------------------------------------------------------------
     65  
     66     //有意识的触发错误,如找不到控制器等
     67     function show_error($heading, $message, $template = 'error_general', $status_code = 500)
     68     {
     69                 //响应一个http头
     70         set_status_header($status_code);
     71  
     72         $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
     73                 /**
     74                  * 缓冲机制是有嵌套级别的,
     75                  * 这个if判断是说发生错误的缓冲级别和Exception被加载【刚开始】的缓冲级别相差1以上
     76                  * 看core/Loader.php中的_ci_load() CI在加载view的时候先ob_start(),然后由output处理输出,
     77                  *  因此,如果是在视图文件发生错误,则就会出现缓冲级别相差1的情况,此时先把输出的内容给flush出来,然后再把错误信息输出。
     78                  */
     79         if (ob_get_level() > $this->ob_level + 1)
     80         {
     81             ob_end_flush();
     82         }
     83                 
     84                 //输出缓冲内容
     85         ob_start();
     86         include(APPPATH.'errors/'.$template.'.php');
     87         $buffer = ob_get_contents();
     88         ob_end_clean();
     89         return $buffer;
     90     }
     91  
     92     // --------------------------------------------------------------------
     93  
     94     //PHP代码错误
     95     function show_php_error($severity, $message, $filepath, $line)
     96     {
     97         $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
     98  
     99         $filepath = str_replace("\", "/", $filepath);
    100  
    101         // 为了安全起见,只显示最后两段路径
    102         if (FALSE !== strpos($filepath, '/'))
    103         {
    104             $x = explode('/', $filepath);
    105             $filepath = $x[count($x)-2].'/'.end($x);
    106         }
    107                 
    108         if (ob_get_level() > $this->ob_level + 1)
    109         {       //输出缓冲区内容并关闭缓冲
    110             ob_end_flush();
    111         }
    112         ob_start();
    113         include(APPPATH.'errors/error_php.php');
    114         $buffer = ob_get_contents();
    115         ob_end_clean();
    116         echo $buffer;
    117     }
    118 }
  • 相关阅读:
    Qt读取JSON和XML数据
    IOS设计模式学习(19)策略
    Android学习笔记(二)之异步加载图片
    ETL-Career RoadMap
    HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
    CodeForces 242E
    推荐:室内定位API
    基于单片机的电子密码锁的实现
    [nagios监控] NRPE: Unable to read output 的原因及排除
    (ubuntu)在andorid andk工程中使用ccache加速编译速度
  • 原文地址:https://www.cnblogs.com/hf8051/p/5160234.html
Copyright © 2011-2022 走看看