Voting

: four minus four?
(Example: nine)

The Note You're Voting On

ciprian dot stingu at gmail dot com
13 years ago
A function that i use for debug
I shortened variables name and i eliminated the spaces from second function in order fit in post :(

<?php
define
("LFP", './lt.log');
function
LogTrace($Argument, $lfn = LFP, $itw = ' ')
{
error_log("=====\r", 3, $lfn);
error_log("[BEGIN BACKTRACE]\r", 3, $lfn);
$it = '';
$Ts = array_reverse(debug_backtrace());
foreach(
$Ts as $T)
{
if(
$T['function'] != 'include' && $T['function'] != 'require' && $T['function'] != 'include_once' && $T['function'] != 'require_once')
{
$ft = $it . '<'. basename($T['file']) . '> on line ' . $T['line'];
if(
$T['function'] != 'LogTrace')
{
if(isset(
$T['class']))
$ft .= ' in method ' . $T['class'] . $T['type'];
else
$ft .= ' in function ';
$ft .= $Trace['function'] . '(';
}
else
$ft .= '(';
if(isset(
$T['args'][0]))
{
if(
$T['function'] != 'LogTrace')
{
$ct = '';
foreach(
$T['args'] as $A)
{
$ft .= $ct . LogVar($A, '', $it, $itw, 0);
$ct = $it . $itw . ',';
}
}
else
$ft .= LogVar($T['args'][0], '', $it, $itw, 0);
}
$ft .= $it . ")\r";
error_log($ft, 3, $lfn);
$it .= $itw;
}
}
error_log("[END BACKTRACE]\r", 3, $lfn);
}

function
LogVar(&$Var, $vn, $pit, $itw, $nlvl, $m = '')
{
if(
$nlvl>=16) return;
if(
$nlvl==0){$tv=serialize($Var);$tv=unserialize($tv);}
else
$tv=&$Var;
$it=$pit.$itw;
for(
$i=0; $i<$nlvl;$i++) $it.='.'.$itw;
$o='';$nl="\n";
if(
is_array($tv))
{
if(
strlen($vn)>0) $o.=$it.$m.'<array> $'.$vn.' = (';
else
$o.="\r".$it.$m.'<array> = (';
$o.= $nl;$AK=array_keys($tv);
foreach(
$AK as $AN) {$AV=&$tv[$AN];$o.=LogVar($AV,$AN,$pit,$itw,$nlvl+1);}
$o.=$it.')'.$nl;
}
else if(
is_string($tv))
{
if(
strlen($vn)>0)$o.=$it.$m.'<string> $'.$vn.' = ';
else
$o.=' '.$m.'<string> = ';
if(
$tv===null) $o.='NULL';
else
$o.='"'.$tv.'"';
$o.=$nl;
}
else if(
is_bool($tv))
{
if(
strlen($vn) > 0) $o.=$it.$m.'<boolean> $'.$vn.' = ';
else
$o.=' '.$m.'<boolean> = ';
if(
$tv===true) $o.='TRUE';
else
$o.='FALSE';
$o.=$nl;
}
else if(
is_object($tv))
{
if(
strlen($vn)>0)
{
$o.=$pit.$itw;
for(
$i=0;$i<$nlvl;$i++) $o.='.'.$itw;
$o.=$m.'<'.get_class($tv).'::$'.$vn.'> = {'.$nl;
}
else
$o.=' '.$m.'<'.get_class($tv).'::> = {'.$nl;
$R=new ReflectionClass($tv);
$o.=$it.'.'.$itw.'Class methods {'.$nl;
$CM=$R->getMethods();
foreach(
$CM as $MN => $MV)
{
$o.=$it.'.'.$itw.'.'.$itw.implode(' ',Reflection::getModifierNames($MV->getModifiers())).' '.$MV->getName().'(';
$MP=$MV->getParameters(); $ct='';
foreach(
$MP as $MPN => $MPV)
{
$o.=$ct; $o.=$MPV->isOptional()?'[':'';
if(
$MPV->isArray()) $o.='<array> ';
else if(
$MPV->getClass()!==null) $o.='<'.$MPV->getClass()->getName().'::> ';
$o.=$MPV->isPassedByReference()?'&':''; $o.='$'.$MPV->getName();
if(
$MPV->isDefaultValueAvailable())
{
if(
$MPV->getDefaultValue()===null) $o.=' = NULL';
else if(
$MPV->getDefaultValue()===true) $o.=' = TRUE';
else if(
$MPV->getDefaultValue()===false) $o.=' = FALSE';
else
$o.=' = '.$MPV->getDefaultValue();
}
$o.=$MPV->isOptional()?']':''; $ct=', ';
}
$o.=')'.$nl;
}
$o.=$it.'.'.$itw.'}'.$nl; $o.=$it.'.'.$itw.'Class properties {'.$nl;
$CV=$R->getProperties();
foreach(
$CV as $CN => $CV)
{
$M=implode(' ',Reflection::getModifierNames($CV->getModifiers())).' ';
$CV->setAccessible(true);
$o.=LogVar($CV->getValue($tv),$CV->getName(),$pit,$itw,$nlvl+2,$M);
}
$o.=$it.'.'.$itw.'}'.$nl; $o.=$it.'.'.$itw.'Object variables {'.$nl;
$OVs=get_object_vars($tv);
foreach(
$OVs as $ON => $OV) $o.=LogVar($OV,$ON,$pit,$itw,$nlvl+2);
$o.=$it.'.'.$itw.'}'.$nl; $o.=$pit.$itw;
for(
$i=0;$i<$nlvl;$i++) $o.='.'.$itw;
$o.='}'.$nl;
}
else
{
if(
strlen($vn)>0) $o.=$it.$m.'<'.gettype($tv).'> $'.$vn.' = '.$tv;
else
$o.=' '.$m.'<'.gettype($tv).'> = '.$tv;
$o.=$nl;
}
return
$o;
}
//test
date_default_timezone_set('Europe/Bucharest');
$date = new DateTime('2010-01-28');
LogTrace($date);
?>

<< Back to user notes page

To Top