Dump back traces

@param array $backtraces
@param string $format
505026
public static function dumpBacktrace(array $backtraces, string $format = "[%INDEX%]%CLASS%%TYPE%%FUNCTION%() in %FILE%(%LINE%)")
    {
        self::walkBacktrace($backtraces, function($index, $file, $line, $class, $type, $func) use($format){
            $line = strtr($format, [
                '%INDEX%' => $index + 1,
                '%FILE%' => $file,
                '%LINE%' => $line,
                '%CLASS%' => $class,
                '%TYPE%' => $type,
                '%FUNCTION%' => $func,
            ]);
            echo $line . PHP_EOL;
        });
    }
537860
public static function dump( $var, $format = null ) {

		if( !static::isDebug() )
			return;

		$dumpers = [
			static::DUMP_HTML     => '\\yolk\\debug\\HTMLDumper',
			static::DUMP_TERMINAL => '\\yolk\\debug\\TerminalDumper',
			static::DUMP_TEXT     => '\\yolk\\debug\\TextDumper',
		];

		// auto-detect format if unknown or none specified
		if( !isset($dumpers[$format]) )
			$format = static::isCLI() ? static::DUMP_TERMINAL : static::DUMP_HTML;

		$dumpers[$format]::dump($var);

	}
642085
protected function _dump($args) {
		// get function call position
		$backtrace = debug_backtrace();
		$backtrace = $backtrace[1];

		// get calling file
		if (isset($backtrace['file'])) {
			$file = file($backtrace['file']);
			$function = trim($file[$backtrace['line']-1]);
		}

		$output = '';
		foreach ($args as $arg) {
			// count errors to produce unique ids
			$this->errorcounter++;

			// create headline
			$output .= '<div class="dp_container">';
			$output .= '<h1 class="dump">'.htmlspecialchars($function).'</h1>';

			// highlight the file name
			$file = preg_replace('=[^/]+$=', '<strong>$0</strong>', $backtrace['file']);
			$output .= '<h3>'.$file.' (Line '.$backtrace['line'].')</h3>';

			// add var content
			$output .= $this->_dump_php($arg);
			$output .= '</div>';
			
			// add styles
			$output .= $this->_debug_styles();
		}
		
		// output to screen		
		if ($this->config['output']['screen'] == false) return '';

		echo $output;
	}
989321
private function backtrack(): bool
    {
        $found = false;

        do {
            $last = \array_pop($this->trace);

            if ($last instanceof Entry) {
                $found = $this->grammar->fetch($last->getRule()) instanceof Alternation;
            } elseif ($last instanceof Escape) {
                $found = $this->grammar->fetch($last->getRule()) instanceof Repetition;
            } elseif ($last instanceof Token) {
                if (! $this->stream->prev()) {
                    return false;
                }
            }
        } while (0 < \count($this->trace) && $found === false);

        if ($found === false) {
            return false;
        }

        $this->todo = $last->getTodo();
        $this->todo[] = new Entry($last->getRule(), $last->getData() + 1);

        return true;
    }
642282
static private function getTextBackTrace($backtrace) {
		$str = '';
	
		foreach ($backtrace as $step) {
			if ($step['function']!='getTextBackTrace' && $step['function']!='handle_error')
			{
				if (isset($step['file']) && isset($step['line'])) {
					$str .= "In ".$step['file'] . " at line ".$step['line'].": ";
				}
				if (isset($step['class']) && isset($step['type']) && isset($step['function'])) {
					$str .= $step['class'].$step['type'].$step['function'].'(';
				}
	
				if (is_array($step['args'])) {
					$drawn = false;
					$params = '';
					foreach ( $step['args'] as $param)
					{
						$params .= self::getPhpVariableAsText($param);
						//$params .= var_export($param, true);
						$params .= ', ';
						$drawn = true;
					}
					$str .= $params;
					if ($drawn == true)
					$str = substr($str, 0, strlen($str)-2);
				}
				$str .= ')';
				$str .= "\n";
			}
		}
	
		return $str;
	}
737672
protected function setStackTraces($stackTraces = null)
    {
        $traces = $this->parser->parseStackTrace($stackTraces);

        $output = [];

        foreach ($traces as $trace) {
            $output[] = new TraceEntry($this->parser, $trace);
        }

        $this->stack_traces = new Collection($output);
    }
136701
def format_backtrace(backtrace)
      backtrace ||= []
      backtrace[0..BACKTRACE_LINES].collect do |line|
        "#{line.gsub(/\n|\`|\'/, '')}".split(//).last(MAX_LINE_LENGTH).join
      end
    end
992103
public static function traceBegin()
    {
        $dbg = debug_backtrace();
        $str = '=> ';
        if (!empty ($dbg[1]['class'])) {
            $str .= $dbg[1]['class'] . '::';
        }
        $str .= $dbg[1]['function'] . '(';
        if (is_array($dbg[1]['args'])) {
            foreach ($dbg[1]['args'] as $index => $arg) {
                if ($index != 0) {
                    $str .= ', ';
                }
                if (is_object($arg)) {
                    $str .= get_class($arg);
                } else {
                    $str .= str_replace(array("\r\n", "\n", "\r"), "", var_export($arg, true));
                }
            }
        }
        if (isset($dbg[1]['file'])) {
            $file = basename($dbg[1]['file']);
        } else {
            $file = 'unknown_file';
        }
        if (isset($dbg[1]['line'])) {
            $line = $dbg[1]['line'];
        } else {
            $line = 'unknown_line';
        }
        $str .= ') [' . $file . ':' . $line . ']';
        phpCAS :: log($str);
        if (!isset(self::$_PHPCAS_DEBUG['indent'])) {
            self::$_PHPCAS_DEBUG['indent'] = 0;
        } else {
            self::$_PHPCAS_DEBUG['indent']++;
        }
    }
430247
public static function walkBacktrace($backtraces, callable $callback)
    {
        foreach($backtraces as $index => $item){
            $func  = $item['function'] ?? '';
            $class = $item['class'] ?? '';
            $type  = $item['type'] ?? '';
            $file  = $item['file'] ?? '';
            $line  = $item['line'] ?? '';
            ($callback)($index, $file, $line, $class, $type, $func);
        }
    }
23
public static function trace($trace = null, $addObject = false)
    {
        if (!self::isDebug()) {
            return false;
        }

        $_this = self::i();

        $trace = $trace ? $trace : debug_backtrace($addObject);
        unset($trace[0]);

        $result = $_this->convertTrace($trace, $addObject);

        return $_this->dump($result, '! backtrace !');
    }
1156207
public void setTraces(java.util.Collection<Trace> traces) {
        if (traces == null) {
            this.traces = null;
            return;
        }

        this.traces = new java.util.ArrayList<Trace>(traces);
    }
1193811
protected function getBacktrace() {
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

            if (empty($trace)) {
                // @codeCoverageIgnoreStart
                return [];
                // @codeCoverageIgnoreEnd
            } else {
                foreach ($trace as $k => $v) {
                    foreach (self::$ignoredFiles as $i) {
                        if (stripos(Alo::ifnull($v['file'], ''), $i) !== false) {
                            unset($trace[$k]);
                            break;
                        }
                    }
                }
            }

            return array_values($trace);
        }
428477
public static function debugBacktraceString($trace = null) {
		$result = [];
		if (!\Debug::$debug) return $result;
		$i = 1;
		// if trace is not provided
		if (empty($trace)) {
			$trace = debug_backtrace();
			unset($trace[0]);
		}
		foreach($trace as $v) {
			$stack = '#' . $i . ' ' . (isset($v['file']) ? $v['file'] : 'Unknown');
			if (isset($v['line'])) {
				$stack.= '(' . $v['line'] . ')';
			}
			// do not show error handler
			if (!(isset($v['class']) && $v['class'] == 'error' && $v['function'] == 'error_handler')) {
				$stack.= ': ';
				if(isset($v['class'])) {
					$stack.= $v['class'] . $v['type'];
				}
				$params = [];
				if (isset($v['args'])) {
					foreach ($v['args'] as $v2) {
						if (gettype($v2) == 'string') {
							$params[] = str_replace(["\n", "\r", "\t"], ' ', $v2);
						} else if (is_array($v2)) {
							$temp = var_export_condensed($v2, ['skip_objects' => true]);
							$params[] = substr($temp, 0, 100) . '...';
						} else {
							$params[] = var_export_condensed($v2, ['skip_objects' => true]);
						}
					}
				}
				$stack.= $v['function'] . '(' . implode(', ', $params) . ');';
			}
			$i++;
			$result[] = $stack;
		}
		return $result;
	}
82509
public static function getCLIBacktrace($backtrace=null) {
		if(!$backtrace)
			$backtrace = debug_backtrace();

		$r = '';
		$c = count($backtrace);
		for($i=0; $i<$c; $i++) {
			$trace = $backtrace[$i];

			if(isset($trace['file']))
				$r .= $trace['file'].':'.$trace['line']."\n";
		}

		return $r;
	}
700817
private function parse_backtrace_args( $args ) {
		return $this->array_map( $args, function ( $d ) {
			$type = gettype( $d );
			if ( 'array' === $type ) {
				return $this->parse_backtrace_args( $d );
			} elseif ( 'object' === $type ) {
				$type = get_class( $d );
			} elseif ( 'resource' !== $type && 'resource (closed)' !== $type && 'NULL' !== $type && 'unknown type' !== $type ) {
				if ( 'boolean' === $type ) {
					$d = var_export( $d, true );
				}
				$type .= ': ' . $d;
			}

			return $type;
		} );
	}
1011890
public static function showTrace(array $options = [])
    {
        $options = array_merge([
            'trace_offset' => 0,
            'reverse'      => false,
        ], $options);

        $backtrace = ($options['reverse'] ? array_reverse(debug_backtrace()) : debug_backtrace());

        $output     = '';
        $traceIndex = ($options['reverse'] ? 1 : count($backtrace));
        foreach ($backtrace as $trace) {
            $output .= $traceIndex . ': ';
            $output .= self::getCalledFromTrace($trace);
            $output .= "\n";

            $traceIndex += ($options['reverse'] ? 1 : -1);
        }

        self::output($output, $options);
    }
882118
public static function dump( $var, $format = 'html', $back = 0, $options = NULL, $return = FALSE, $max_depth = 6 )
    {
        list( $file, $line ) = self::caller( $back );

        if ( !$options ){
            $options = array();
        }
        $default_options = array(
                'title' => 'system dump',
                'font_size' => 11,
                'max_string_length' => self::DUMP_MAX_LENGTH,
                'type' => 'textarea',
            );
        $options = array_merge( $default_options, $options );

        $title             = $options['title'];
        $font_size         = $options['font_size'];
        $max_string_length = $options['max_string_length'];
        $type              = $options['type'];

        $lines = array();
        $recursion = array();
        self::_dump( '-', $var, 0, $max_string_length, $lines, $max_depth, $recursion );

        switch( $format )
        {
        case "html":
            switch( $type ){
            case 'div':
                $output  = "<div style=\"font-size:12px; margin: 2px\"> $title:" . implode('',$lines) . " @$file($line)</div>";
                break;
            case 'textarea':
            default:
                $output  = "<h3 style=\"font-size:12px; margin: 0; color:black; background-color:white; text-align: left\"> $title @$file($line)</h3>";
                $output .= "<textarea rows=14 style=\"width:100%; font-size:{$font_size}px; margin: 0; color:black; background-color:white; border: 1px solid silver;\">";
                $output .= implode(PHP_EOL,$lines);
                $output .= "</textarea>";
                break;
            }
            break;
        case "shell":
        default:
            $output  = "$title @$file($line)" . PHP_EOL;
            $output .= implode(PHP_EOL,$lines) . PHP_EOL;
            break;
        }

        if ( $return ){
            return $output;
        }
        else{
            echo $output;
            return null;
        }
    }