<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
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 2
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 at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class errmsg {
protected $ident;
private $format, $color, $coltype;
private $colors;
public function __construct($ident, $conf){
if ( !isset($conf['format']) ){ $conf['format'] = '%msg%'; }
if ( !isset($conf['color']) ){ $conf['color'] = FALSE; }
if ( !isset($conf['coltype']) ){
$conf['coltype'] = '';
if ( $conf['color'] ){ $conf['coltype'] = 'html'; }
}
if ( isset($conf['colors']['lvls']) AND isset($conf['colors']['info']) ){
$this->colors = $conf['colors'];
} elseif ( $conf['color'] ){
//we use color, and no colors gived
$this->colors['lvls'] = Array( 'notice' => 'White',
'debug' => 'Green',
'warning' => 'Yellow',
'info' => 'Blue',
'err' => 'Maroon',
'alert' => 'Red',
'crit' => 'Red',
'emerg' => 'Red',
);
$this->colors['info'] = Array( 'pid' => 'Teal',
'file' => 'Teal',
'line' => 'Purple',
'context' => 'Yellow',
'function' => 'Purple',
'class' => 'Green',
'method' => 'Yellow',
'args' => 'Teal',
'msg' => '',
'ident' => 'Gray',
'time' => '',
);
}
$this->ident = $ident;
$this->format = $conf['format'];
$this->color = $conf['color'];
$this->coltype = $conf['coltype'];
}
public function buildmsg($err){
if ( !CORE::isError($err) ){ return FALSE; }
$ar['pid'] = $this->colorise( posix_getpid(), $this->get_colortags('pid') );
$ar['level'] = $this->colorise( errors::lvl_to_dsp($err->getlvl()), $this->get_leveltags($err->getlvl()) );
$msg = $err->getmsg();
if ( $this->coltype == 'html' ){ $msg = htmlentities($msg, ENT_QUOTES, CORE::getintcharset()); }
$ar['msg'] = $this->colorise( $msg, $this->get_leveltags($err->getlvl()) );
unset($msg);
$ar['context'] = $this->colorise( $err->getcontext(), $this->get_colortags('context') );
$ar['file'] = $this->colorise( $err->getfile(), $this->get_colortags('file') );
$ar['line'] = $this->colorise( $err->getline(), $this->get_colortags('line') );
$ar['ident'] = $this->colorise( $this->ident, $this->get_colortags('ident') );
$ar['hour'] = $this->colorise( @date('H', time()), $this->get_colortags('time') );
$ar['min'] = $this->colorise( @date('i', time()), $this->get_colortags('time') );
$buf = split('\.', chrono::get_microtime());
$ar['usec'] = $this->colorise( $buf[1], $this->get_colortags('time') );
$ar['sec'] = $this->colorise( @date('s', time()), $this->get_colortags('time') );
$ar['date'] = $this->colorise( @date('d-m-Y', time()), $this->get_colortags('time') );
$buf = $this->format;
foreach($ar as $var => $val){
$buf = str_replace('%'.$var.'%', $val, $buf);
}
if ( ereg('%backtrace%', $this->format) ){
$buf = str_replace('%backtrace%', $this->buildbacktrace($err->getbacktrace(), PHP_EOL), $buf);
}
unset($err);
return $buf;
}
public function disable_color(){ $this->color = FALSE; }
private function get_colortags($type){
if ( isset($this->colors['info'][$type]) ){
return $this->colortags($this->colors['info'][$type], $type);
}
return $this->colortags('', $type);
}
private function get_leveltags($level){
$level = errors::lvl_to_dsp($level);
if ( isset($this->colors['lvls'][$level]) ){
return $this->colortags($this->colors['lvls'][$level], $level);
}
return $this->colortags('', $level);
}
private function colortags($color=NULL, $type=NULL){
switch( $this->coltype ){
case 'html' : if ( empty($color) ){
$tags = '%s';
} else {
$tags = '<font color="'.$color.'">%s</font>';
}
break;
case 'css' : $tags = '<span class="logs_'.$type.'">%s</span>';
break;
case 'shell' : $tags['white'] = '%s';
/*
TODO Get shell color tags somewhere
*/
$tags = $tags[$color];
break;
case FALSE : $tags='%s';
break;
}
return $tags;
}
private function colorise($string, $tags){
if ( $this->color ){
if ( !is_array($string) AND !is_array($tags) AND !empty($string) ){
return sprintf($tags, $string);
}
}
return $string;
}
public function buildbacktrace( $backtrace, $eol ){
$tot = count($backtrace);
foreach( $backtrace as $k => $entry ){
$out[$tot]['file'] = $this->colorise( basename($entry['file']), $this->get_colortags('file') );
$out[$tot]['line'] = $this->colorise( $entry['line'], $this->get_colortags('line') );
$out[$tot]['class'] = $this->colorise( $entry['class'],
$this->get_colortags('class') );
$out[$tot]['function'] = $this->colorise( $entry['function'],
$this->get_colortags('function') );
$out[$tot]['type'] = $entry['type'];
$buf = Array();
foreach($entry['args'] as $arg){
if ( is_object($arg) ){
$buf[] = $this->colorise( get_class($arg), $this->get_colortags('class') );
} elseif ( is_array($arg) ) {
$buf[] = $this->colorise( 'Array', $this->get_colortags('class') );
} else {
if ( $this->coltype == 'html'){ $arg = htmlentities($arg); }
$buf[] = $this->colorise( $arg, $this->get_colortags('args') );
}
}
$out[$tot]['args'] = implode(', ', $buf);
unset($buf, $entry);
$tot--;
}
ksort($out);
unset($out[count($out)]);
unset($out[count($out)]);
unset($out[count($out)]);
foreach($out as $k => $i){
$bck[] = '|'.str_repeat('-', $k).'> '.$i['file'].':'.$i['line'].' => '.
$i['class'].$i['type'].$i['function'].'('.$i['args'].')';
}
if ( !isset($bck) ){ $bck = Array(); }
return implode($eol, $bck);
}
}