<?php

class Debug {

  static private $debug_out = array();
  static private $colophons = array();
  static private $last_time = '';
  static private $start_time = 0;
  static private $enable;

  const ROWSEP   = '<br />';
  const DEBSTART = '<pre>';
  const DEBEND   = '</pre>';

  public function Start($enable = true) {
    self::$start_time = microtime(true);
    self::$last_time = microtime(true);
    self::$debug_out[] = 'Debug start - '.date('d.m.Y h:i:s');
    self::$enable = $enable;
  }

  public static function addPoint($message = '', $variable = '' ) {
    if (self::$enable == true) {
      $time = microtime(true);
      $point_time = number_format(($time - (self::$last_time))*1000, 3, ',', ' ');
      self::$last_time = $time;

      if ( !empty ($variable) )
      self::$debug_out[] = self::ROWSEP.'Message: '.$message.self::ROWSEP.'Time: '.$point_time.' ms'.self::ROWSEP.'Variable: '.print_r($variable, true);
      else
      self::$debug_out[] = self::ROWSEP.'Message: '.$message.self::ROWSEP.'Time: '.$point_time.' ms';
    }
  }

  public static function getDebug( $return = false ) {
    $ret = self::DEBSTART . implode( self::ROWSEP , self::$debug_out ). self::ROWSEP .
    		'Total: '. number_format((microtime(true) - (self::$start_time))*1000, 3, ',', ' ').
            ' ms'. self::ROWSEP . self::DEBEND;
    if ( $return == true ){
      return $ret;
    }
    else{
      if ( __DEBUG == true )
      echo $ret;
    }
  }

  public static function getVar() {
    if (self::$enable != true)
    return false;

    echo self::DEBSTART;

    foreach (func_get_args() AS $i => $v){

      if (is_array($v) OR is_object($v)) {
        echo htmlspecialchars(print_r($v,true));
      }
      else {
        if ( strtolower($v) === 'die' )
        die(self::DEBEND.self::ROWSEP.' běh scriptu ukončen (Debug::getVar(...,DIE)');
        else
        var_dump($v);
      }
    }

    echo self::DEBEND;

  }

  public static function addColophon($callback) {
    /** /fixCallback($callback);/**/
    if (!in_array($callback, self::$colophons, TRUE) && is_callable($callback)) {
      self::$colophons[] = $callback;
    }
  }

  public static function backtrace($clean = true) {

  	$darray = debug_backtrace();
  	if ($clean) {
    	foreach($darray as $key=>$item) {
  			$darray[$key]['object'] = 'clean'; 
  		}
  	}

		return $darray;
  }
  
}
?>