Skip to content

Instantly share code, notes, and snippets.

@grom358 grom358/trace.php
Created Jun 4, 2014

Embed
What would you like to do?
<?php
function argToString($arg, $maxLength = false) {
if (is_null($arg)) {
return 'null';
} elseif (is_array($arg)) {
return 'Array';
} elseif (is_object($arg)) {
return 'Object(' . get_class($arg) . ')';
} elseif (is_bool($arg)) {
return $arg ? 'true' : 'false';
} elseif (is_int($arg) || is_double($arg)) {
return $arg;
} else {
$arg = (string) $arg;
if ($maxLength && strlen($arg) > $maxLength ) {
$arg = substr($arg, 0, $maxLength) . '...';
}
return "'" . $arg . "'";
}
}
function traceToString($trace) {
$trace = array_reverse($trace); // Show in reverse order
$len = count($trace);
$result = '';
foreach($trace as $i => $t) {
$result .= '#' . ($len - $i - 1) . ' ';
if (!empty($t['class'])) {
$result .= $t['class'] . $t['type'];
}
$result .= $t['function'];
$args = array();
if (!empty($t['args'])) {
foreach ($t['args'] as $arg) {
$args[] = argToString($arg);
}
}
$result .= '(' . implode(', ', $args) . ') at '
. (isset($t['file']) ? $t['file'] : 'Unknown')
. ':' . (isset($t['line']) ? $t['line'] : 'Unknown')
. "\n";
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.