phpDocumentor GloryLands
Common
[ class tree: GloryLands ] [ index: GloryLands ] [ all elements ]

Source for file errors.php

Documentation is available at errors.php

  1. <?php
  2. /**
  3.   * GloryLands Optimizing Core
  4.   *
  5.   * <pre>
  6.   * GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
  7.   * Copyright (C) 2008-09  John Haralampidis <jïhnys2[at]gmail.cïm>
  8.   *
  9.   * This program is free software: you can redistribute it and/or modify
  10.   * it under the terms of the GNU General Public License as published by
  11.   * the Free Software Foundation, either version 3 of the License, or
  12.   * (at your option) any later version.
  13.   *
  14.   * This program is distributed in the hope that it will be useful,
  15.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.   * GNU General Public License for more details.
  18.   *
  19.   * You should have received a copy of the GNU General Public License
  20.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.   *
  22.   * For any help/suggestions or troubleshooting you can see the
  23.   * project community website at <http://www.glorylands.gr>
  24.   * </pre>
  25.   *
  26.   * @license GNU/GPLv3 GNU General Public License version 3
  27.   * @package GloryLands
  28.   * @subpackage Common
  29.   * @author John Haralampidis <jïhnys2[at]gmail.cïm>
  30.   * @copyright Copyright (C) 2007-2009, John Haralampidis
  31.   * @version 1.0
  32.   */
  33.  
  34. /**
  35.   * Error management
  36.   *
  37.   * This class provides debugging information and error handling on the
  38.   * whole game system.
  39.   *
  40.   * @todo Implement the error stack
  41.   *
  42.   * @subpackage Common
  43.   * @version 1.0
  44.   */
  45. class GLError {
  46.  
  47.     const CRITICAL 'critical';
  48.     const ERROR 'error';
  49.     const WARNING 'warning';
  50.     const NOTICE 'notice';
  51.     const DEBUG 'critical';
  52.     
  53.     const FORMAT_HTML 1;
  54.     const FORMAT_TEXT 2;
  55.     const FORMAT_XML 4;
  56.     const FORMAT_JSON 8;
  57.     const FORMAT_DEBUG 128;
  58.     
  59.     static public $errors;
  60.     
  61.     static public function trigger($message$level=ERROR{
  62.         echo 'Error: '.$message;
  63.         if (!self::$errorsself::$errors=array();
  64.         self::$errors[array(
  65.             'message' => $message,
  66.             'level' => $level,
  67.             'trace' => debug_backtrace()
  68.         );
  69.     }
  70.     
  71.     static public function log($message$category$level=ERROR{
  72.         echo 'Error: '.$message;
  73.         if (!self::$errorsself::$errors=array();
  74.         self::$errors[array(
  75.             'message' => $message,
  76.             'level' => $level,
  77.             'trace' => debug_backtrace()
  78.         );
  79.     }
  80.  
  81.     static public function report($message$category$level=ERROR{
  82.         echo 'Error: '.$message;
  83.         if (!self::$errorsself::$errors=array();
  84.         self::$errors[array(
  85.             'message' => $message,
  86.             'level' => $level,
  87.             'trace' => debug_backtrace()
  88.         );
  89.     }
  90.     
  91.     /**
  92.       * Get a code snipped from the file
  93.       *
  94.       * This function is used to extract the erroreus code from the
  95.       */
  96.     static private function get_snippet($file$line$range=5$html=true{
  97.         $pre round($range/2);
  98.         $post $range-$pre;        
  99.         $pre $line-$pre;
  100.         $post $line+$post;
  101.         if ($pre<1$pre=1;
  102.         $max_len strlen((string)$post);        
  103.         $cline 0;
  104.         $buffer '';
  105.  
  106.         // Get the code lines
  107.         $f fopen($file,'r');                    
  108.         while (!feof($f)) {
  109.             $row fgets($f);
  110.             $cline++;
  111.             if (($cline $pre&& ($cline $post)) {
  112.                 $bufline str_pad($cline,$max_len,'0',STR_PAD_LEFT);
  113.                 if ($cline == $line{
  114.                     if ($html{
  115.                         $bufline '<div style="background-color: #FF0000; color: #FFFFFF">'.$bufline.'<b>&gt;</b>| '.$row.'</div>';
  116.                     else {
  117.                         $bufline $bufline.'>| '.$row;
  118.                     }
  119.                 else {
  120.                     $bufline.= ' | '.$row;
  121.                 }
  122.                 $buffer.=$bufline.($html?"<br />":"\n");
  123.             elseif (($cline == $pre|| ($cline == $post)) {
  124.                 $bufline str_pad($cline,$max_len,'0',STR_PAD_LEFT).' | ...';            
  125.                 $buffer.=$bufline.($html?"<br />":"\n");
  126.             }
  127.         }
  128.         
  129.         // Design the header
  130.         if ($html{
  131.             $buffer '<pre style="border: solid 1px #999999; background-color: #FFFFFF; padding: 4px; color: #999999;"><div style="padding: 2px; color: #FFFFFF; background-color: #666666; font-weight: bold;">'.$file.'</div>'.$buffer.'</pre>';
  132.         else {
  133.             $head '  --['.$file.']';
  134.             $head str_pad($head46'-'STR_PAD_RIGHT);
  135.             $buffer $head."\n".$buffer;
  136.         }
  137.         
  138.         // Return buffer 
  139.         return $buffer;
  140.     }
  141.     
  142.     static private function render_vars($vars$html=true{
  143.         if (!$htmlreturn print_r($vars,true);
  144.         $buffer '<span style="background-color:#E4CAFF;padding: 2px;" onmouseover="__expand(this)" onmouseout="__retract(this)"><span>(</span><ul style="display:none;">';
  145.         foreach ($vars as $var => $value{
  146.             $buffer.='<b>'.$var.'</b> = ';
  147.             if (is_object($value)) {
  148.                 $buffer.='Object['.get_class($value).']'.self::render_vars($value);
  149.             elseif (is_array($value)) {
  150.                 $buffer.='Array'.self::render_vars($value);
  151.             else {
  152.                 $buffer.=var_export($value,true);
  153.             }
  154.         }
  155.         $buffer.= '</ul><span>)</span></span>';
  156.         return $buffer;
  157.     }
  158.     
  159.     static private function render_trace($trace$html=true{    
  160.         $buffer '';
  161.         $pad strlen((string)sizeof($trace));
  162.         
  163.         if ($html$buffer '<pre style="background-color: #FFFFCC; color: #333333;">';
  164.         $i=0;
  165.         foreach ($trace as $hit{
  166.             $i++;
  167.             $buffer.=' | #'.str_pad($i$pad'0'STR_PAD_LEFT).' <b>';
  168.             if (!isset($hit['type'])) {
  169.                 $buffer.=$hit['function'];
  170.             else {
  171.                 if (isset($hit['class'])) {
  172.                     $class $hit['class'];
  173.                 else {
  174.                     $class get_class($hit['object']);
  175.                 }
  176.                 $buffer.=$class.$hit['type'].$hit['function'];
  177.                 $buffer.=self::render_vars($hit['args'],$html);
  178.             }
  179.             $buffer.='</b><br />  At: '.$hit['file'].' [line '.$hit['line'].']<br />';
  180.         }
  181.         if ($html$buffer.= '</pre>';
  182.         return $buffer;
  183.     }
  184.     
  185.     static private function render_error($error$html=true{        
  186.         
  187.         switch ($error['level']{
  188.             case CRITICAL:    
  189.                 $type_str='Critical error';
  190.                 $bcolor='#990000';
  191.                 $fcolor='#FFFFFF';
  192.                 $dcolor='#620000';
  193.                 $ascii='/!\\';
  194.             case ERROR:
  195.                 $type_str='Run-time Error';
  196.                 $bcolor='#FFDDDD';
  197.                 $fcolor='#990000';
  198.                 $dcolor='#990000';
  199.                 $ascii='[!]';
  200.             case WARNING:
  201.                 $type_str='Warning';
  202.                 $bcolor='#FFE1C4';
  203.                 $fcolor='#CC6600';
  204.                 $dcolor='#CC6600';
  205.                 $ascii='(!)';
  206.             case NOTICE:
  207.                 $type_str='Notice';
  208.                 $bcolor='#DFDFFF';
  209.                 $fcolor='#333366';
  210.                 $dcolor='#333366';
  211.                 $ascii='(i)';
  212.             case DEBUG:
  213.                 $type_str='Debug Message';
  214.                 $bcolor='#E9E9E9';
  215.                 $fcolor='#666666';
  216.                 $dcolor='#666666';
  217.                 $ascii='<*>';
  218.             default:
  219.                 $type_str='Undefined Error';
  220.                 $bcolor='#FFE1C4';
  221.                 $fcolor='#CC6600';
  222.                 $dcolor='#CC6600';
  223.                 $ascii='(?)';
  224.         }
  225.         
  226.         if ($html{
  227.             $buffer ='<div style="border: solid 1px '.$dcolor.'; background-color: '.$bcolor.'; color: '.$fcolor.'; font-family: Arial, Helvetica, sans-serif; font-size: 10px"><br />'."\n";
  228.             $buffer.='<h3>'.$type_str.' in </h3>';
  229.             $buffer.='</div>';
  230.         else {
  231.             $buffer='==['.$ascii.']==[ '.$type_str.' ]';
  232.             $buffer.=str_pad($buffer50'='STR_PAD_RIGHT);
  233.             $buffer.=' File: ';
  234.         }
  235.     }
  236.     
  237.     static private function get_code({
  238.         return "<script type=\"text/javascript\">
  239.             function __expand(elm) {
  240.                 var span=elm.getElementsByTagName('span')[0];
  241.                 var ul=elm.getElementsByTagName('ul')[0];
  242.                 //span.style.display = 'block';
  243.                 ul.style.display = 'inline';
  244.             }
  245.             function __retract(elm) {
  246.                 var span=elm.getElementsByTagName('span')[0];
  247.                 var ul=elm.getElementsByTagName('ul')[0];
  248.                 //span.style.display = 'inline';
  249.                 ul.style.display = 'none';
  250.             }
  251.             </script>";
  252.     }
  253.     
  254.     static public function render($format=FORMAT_HTML{
  255.         $buffer '';
  256.         if (!self::$errorsreturn '';
  257.         if ($format FORMAT_HTML$buffer.=self::get_code();
  258.         foreach (self::$errors as $error{
  259.             $buffer.=self::render_error($error($format FORMAT_HTML));
  260.         }
  261.         return $buffer;
  262.     }
  263.     
  264. }
  265.  
  266.  
  267. function debug_error_handler($errno$errstr$errfile$errline{
  268.     
  269.     $buf '<div style="font-family: Arial, Helvetica, sans-serif; border: dotted 2px #666666; color: #000000; margin: 2px; padding: 4px; font-size: 12px; text-align: left; background-color: ';
  270.  
  271.     switch ($errno{
  272.         case E_USER_ERROR:
  273.             $buf.= '#FF8888"><h3><b>Run-time Error</b>';
  274.             break;    
  275.         case E_USER_WARNING:
  276.             $buf.= '#FFC68C"><h3><b>Run-time Warning</b>';
  277.             break;
  278.     
  279.         case E_USER_NOTICE:
  280.             $buf.= '#A4A4FF"><h3><b>Run-time Notice</b>';
  281.             break;
  282.     
  283.         case 8:
  284.             return '';
  285.  
  286.         default:
  287.             $buf.= '#FF8888"><h3><b>Run-time Error #'.$errno.'</b>';
  288.             break;
  289.     }
  290.     
  291.     $buf .= "$errstr</h3>Position:<br />";
  292.     $buf .= '<pre style="background-color: #FFFFFF; padding: 4px; color: #999999;"><div style="padding: 2px; color: #FFFFFF; background-color: #666666; font-weight: bold;"> '.$errfile.'</div>'."[".($errline-3)."] ...\n";
  293.     
  294.     // Extract the faulty lines
  295.     $f fopen($errfile,"r");
  296.     $lines ""$line=0;
  297.     
  298.     $pad 1;
  299.     if ($errline>9$pad=2;
  300.     if ($errline>99$pad=3;
  301.     if ($errline>999$pad=4;
  302.     if ($errline>9999$pad=5;
  303.     if ($errline>99999$pad=6;                
  304.     if ($errline>999999$pad=7;
  305.     
  306.     while (!feof($f)) {
  307.         $s htmlspecialchars(trim(fgets($f)));
  308.         $line++;
  309.         if ($line $errline+2{
  310.             break;
  311.         elseif ($line == $errline{
  312.             $buf.= '<span style="color: #000000">['.str_pad($line,$pad,'0',STR_PAD_LEFT).'] '.$s.'</span><span style="color: red"> &crarr;</span>'."\n";
  313.         elseif ($line $errline-3{
  314.             $buf.='['.str_pad($line,$pad,'0',STR_PAD_LEFT).'] '.$s."\n";
  315.         }
  316.     }
  317.     fclose($f);
  318.     
  319.     $buf .= "[".($errline+3)."] ...".'</pre>Backtrace:<pre style="background-color: #FFFFCC; padding: 4px">';
  320.     
  321.     $b debug_backtrace();
  322.     array_shift($b);
  323.     $i 0;
  324.     $pad 1;
  325.     if (sizeof($b)>9$pad=2;
  326.     if (sizeof($b)>99$pad=3;
  327.     if (sizeof($b)>999$pad=4;    
  328.     foreach ($b as $t{
  329.     
  330.         $func $t['function'].'</b><span style="color: #999999">'.substr(preg_replace('/[\\n\\t]*|\\s{2,}/m',"",var_export($t['args'],true)),6).'</span>';
  331.         if (isset($t['object'])) {
  332.             $func get_class($t['object']).'->'.$func;            
  333.         elseif (isset($t['class'])) {
  334.             $func $t['class'].'::'.$func;
  335.         }
  336.     
  337.         $i++;
  338.         $buf.= "#".str_pad($i,$pad,'0',STR_PAD_LEFT).' <b>'.$func."\n ".str_repeat(' ',$pad)."   At: [".$t['line'].'] '.$t['file']."\n";
  339.     }
  340.     
  341.     $buf .= '</pre></div>';
  342.     echo $buf;
  343.  
  344. }
  345.  
  346. set_error_handler('debug_error_handler');
  347.  
  348. ?>

Documentation generated on Tue, 13 Oct 2009 23:48:53 +0300 by phpDocumentor 1.4.1