<?php
/*
 * Profile functions.
 */

function get_microtime ( ) {
    list($usec, $sec)=explode(' ', microtime());
    return ((float)$usec + (float)$sec);
}

define('APP_START', get_microtime());

function profile ( $dump = false) {
    static $lastTime;
    static $profile;
    static $lastCrc;
    
    $start = get_microtime();
    
    if(!$dump){
        $debug = debug_backtrace();
        $myCrc = crc32(serialize($debug));
        if($lastCrc != $myCrc){
            array_shift($debug);
            $lastCrc = $myCrc;
            $tabs    = 0;
            $data    = '';
            $tabStr  = '    ';
            foreach($debug as $arr){
                $data .= str_repeat($tabStr, $tabs).'&raquo; ';
                if(isset($arr['class'])){
                    $data .= $arr['class'].'.';
                }
                $args=array();
                if(isset($arr['args'])){
                    foreach($arr['args'] as $v){
                        if(is_null($v)){
                            $args[] = 'null';
                        }elseif(is_array($v)){
                            $args[] = 'Array['.count($v).']';
                        }elseif(is_object($v)){
                            $args[] = 'Object:'.get_class($v);
                        }elseif(is_bool($v)){
                            $args[] = $v ? 'true' : 'false';
                        }else{
                            if(strlen($v) > 64){
                                $v = substr($v, 0, 64).'...';
                            }
                            $args[]=htmlentities('"'.str_replace("\n", '\\n', preg_replace('/\s+/', ' ', $v)).'"');
                        }
                    }
                }
                $data .= sprintf("%s (%s)\n%s&raquo; %s :: %d\n"
                , $arr['function']
                , implode(', ', $args)
                , str_repeat($tabStr, $tabs)
                , isset($arr['file']) ? str_replace(dirname(__FILE__), '', $arr['file']) : 'Unknown'
                , isset($arr['line']) ? $arr['line'] : '-1'
                );
                $tabs ++;
            }
            $lastTime  += (get_microtime() - $start);        
            $data       = array(
            'totalTime' => (get_microtime() - $lastTime) - APP_START,
            'trace'     => $data
            );
            if(count($profile)>0){
                $data['diffTime'] = $data['totalTime'] - $profile[count($profile)-1]['totalTime'];
            }else{
                $data['diffTime'] = 0.0;
            }
            $profile[] = $data;
            $lastTime = (get_microtime() - $start);
        }else{
            $lastTime += (get_microtime() - $start);        
        }
    }else{
        $output='
        <div style="background-color: #C0C0C0; color: #000000; font-size: 11px; font-family: arial; position: absolute; top: 2px; left: 900px; width: 300px; padding: 2px 10px 2px 10px;">
        Tick @
        ';
        foreach($profile as $key=>$data){
            $output.=sprintf('
            <br><b>Time:</b> %0.4f (+%0.4f)
            <br><b>Trace:</b>
            <div style="padding-left: 10px; border-left: 4px solid #808080; margin-left: 10px;">
            <pre style="padding: 0px; margin: 0px;">%s</pre>
            </div>
            ', $data['totalTime']
             , $data['diffTime']
             , $data['trace']
            );
        }
        $output.='</div>';
        $lastTime = null;
        $profile  = null;
        $lastCrc  = null;
        return $output;
    }
}
 
 
?>