Source for file errors.php
Documentation is available at errors.php
* GloryLands Optimizing Core
* GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
* Copyright (C) 2008-09 John Haralampidis <jïhnys2[at]gmail.cïm>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For any help/suggestions or troubleshooting you can see the
* project community website at <http://www.glorylands.gr>
* @license GNU/GPLv3 GNU General Public License version 3
* @author John Haralampidis <jïhnys2[at]gmail.cïm>
* @copyright Copyright (C) 2007-2009, John Haralampidis
* This class provides debugging information and error handling on the
* @todo Implement the error stack
const CRITICAL = 'critical';
const WARNING = 'warning';
const DEBUG = 'critical';
const FORMAT_DEBUG = 128;
static public function trigger($message, $level= ERROR) {
if (!self::$errors) self::$errors= array();
'trace' => debug_backtrace()
static public function log($message, $category, $level= ERROR) {
if (!self::$errors) self::$errors= array();
'trace' => debug_backtrace()
static public function report($message, $category, $level= ERROR) {
if (!self::$errors) self::$errors= array();
'trace' => debug_backtrace()
* Get a code snipped from the file
* This function is used to extract the erroreus code from the
static private function get_snippet($file, $line, $range= 5, $html= true) {
$max_len = strlen((string) $post);
if (($cline > $pre) && ($cline < $post)) {
$bufline = str_pad($cline,$max_len,'0',STR_PAD_LEFT);
$bufline = '<div style="background-color: #FF0000; color: #FFFFFF">'. $bufline. '<b>></b>| '. $row. '</div>';
$bufline = $bufline. '>| '. $row;
$buffer.= $bufline. ($html? "<br />": "\n");
} elseif (($cline == $pre) || ($cline == $post)) {
$bufline = str_pad($cline,$max_len,'0',STR_PAD_LEFT). ' | ...';
$buffer.= $bufline. ($html? "<br />": "\n");
$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>';
$head = ' --['. $file. ']';
$head = str_pad($head, 46, '-', STR_PAD_RIGHT);
$buffer = $head. "\n". $buffer;
static private function render_vars($vars, $html= true) {
if (!$html) return print_r($vars,true);
$buffer = '<span style="background-color:#E4CAFF;padding: 2px;" onmouseover="__expand(this)" onmouseout="__retract(this)"><span>(</span><ul style="display:none;">';
foreach ($vars as $var => $value) {
$buffer.= '<b>'. $var. '</b> = ';
$buffer.= 'Object['. get_class($value). ']'. self::render_vars($value);
$buffer.= 'Array'. self::render_vars($value);
$buffer.= '</ul><span>)</span></span>';
static private function render_trace($trace, $html= true) {
if ($html) $buffer = '<pre style="background-color: #FFFFCC; color: #333333;">';
foreach ($trace as $hit) {
$buffer.= ' | #'. str_pad($i, $pad, '0', STR_PAD_LEFT). ' <b>';
if (!isset ($hit['type'])) {
$buffer.= $hit['function'];
if (isset ($hit['class'])) {
$buffer.= $class. $hit['type']. $hit['function'];
$buffer.= self::render_vars($hit['args'],$html);
$buffer.= '</b><br /> At: '. $hit['file']. ' [line '. $hit['line']. ']<br />';
if ($html) $buffer.= '</pre>';
static private function render_error($error, $html= true) {
switch ($error['level']) {
$type_str= 'Critical error';
$type_str= 'Run-time Error';
$type_str= 'Debug Message';
$type_str= 'Undefined Error';
$buffer = '<div style="border: solid 1px '. $dcolor. '; background-color: '. $bcolor. '; color: '. $fcolor. '; font-family: Arial, Helvetica, sans-serif; font-size: 10px"><br />'. "\n";
$buffer.= '<h3>'. $type_str. ' in </h3>';
$buffer= '==['. $ascii. ']==[ '. $type_str. ' ]';
$buffer.= str_pad($buffer, 50, '=', STR_PAD_RIGHT);
static private function get_code() {
return "<script type=\"text/javascript\">
var span=elm.getElementsByTagName('span')[0];
var ul=elm.getElementsByTagName('ul')[0];
//span.style.display = 'block';
ul.style.display = 'inline';
function __retract(elm) {
var span=elm.getElementsByTagName('span')[0];
var ul=elm.getElementsByTagName('ul')[0];
//span.style.display = 'inline';
ul.style.display = 'none';
static public function render($format= FORMAT_HTML) {
if (!self::$errors) return '';
if ($format & FORMAT_HTML) $buffer.= self::get_code();
foreach (self::$errors as $error) {
$buffer.= self::render_error($error, ($format & FORMAT_HTML));
$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: ';
$buf.= '#FF8888"><h3><b>Run-time Error</b>';
$buf.= '#FFC68C"><h3><b>Run-time Warning</b>';
$buf.= '#A4A4FF"><h3><b>Run-time Notice</b>';
$buf.= '#FF8888"><h3><b>Run-time Error #'. $errno. '</b>';
$buf .= ": $errstr</h3>Position:<br />";
$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";
// Extract the faulty lines
$f = fopen($errfile,"r");
if ($errline> 999) $pad= 4;
if ($errline> 9999) $pad= 5;
if ($errline> 99999) $pad= 6;
if ($errline> 999999) $pad= 7;
if ($line > $errline+ 2) {
} elseif ($line == $errline) {
$buf.= '<span style="color: #000000">['. str_pad($line,$pad,'0',STR_PAD_LEFT). '] '. $s. '</span><span style="color: red"> ↵</span>'. "\n";
} elseif ($line > $errline- 3) {
$buf.= '['. str_pad($line,$pad,'0',STR_PAD_LEFT). '] '. $s. "\n";
$buf .= "[". ($errline+ 3). "] ...". '</pre>Backtrace:<pre style="background-color: #FFFFCC; padding: 4px">';
$func = $t['function']. '</b><span style="color: #999999">'. substr(preg_replace('/[\\n\\t]*|\\s{2,}/m',"",var_export($t['args'],true)),6). '</span>';
if (isset ($t['object'])) {
} elseif (isset ($t['class'])) {
$func = $t['class']. '::'. $func;
$buf.= "#". str_pad($i,$pad,'0',STR_PAD_LEFT). ' <b>'. $func. "\n ". str_repeat(' ',$pad). " At: [". $t['line']. '] '. $t['file']. "\n";
|