Skip to content

Commit dd265a1

Browse files
committed
#23 - Add methods for formatting stack traces
1 parent 2ea2b35 commit dd265a1

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

src/TgUtils/FormatUtils.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,70 @@ public static function formatUnit($size, $unit, $precision = 1, $language = NULL
5858
$size = $rc != $unit ? number_format($size, $precision, I18N::_('decimal_point', $language), I18N::_('thousand_sep', $language)) : number_format($size, 0, I18N::_('decimal_point', $language), I18N::_('thousand_sep', $language));
5959
return $size.' '.$rc;
6060
}
61+
62+
/**
63+
* Provides the complete Exception message as string with newlines.
64+
* This method does not shorten any string as the getTraceAsString() method does.
65+
* @param Throwable $throwable the exception to trace
66+
* @return string the complete exception message and stack
67+
*/
68+
public static function getTraceAsString($throwable) {
69+
return implode("\n", self::getTraceLines($throwable));
70+
}
71+
72+
/**
73+
* Provides the complete Exception message as array of strings.
74+
* This method does not shorten any string as the getTraceAsString() method does.
75+
* @param Throwable $throwable the exception to trace
76+
* @return array the complete exception message and stack in separate strings
77+
*/
78+
public static function getTraceLines($throwable) {
79+
$rc = array(get_class($throwable).': '.$throwable->getMessage());
80+
$rc[] = 'at '.$throwable->getFile().'(line '.$throwable->getLine().'): ';
81+
82+
$trace = $throwable->getTrace();
83+
foreach ($trace AS $traceLine) {
84+
$rc[] = self::getTraceLine($traceLine);
85+
}
86+
$previous = $throwable->getPrevious();
87+
if ($previous != NULL) {
88+
$rc[] = 'Caused by:';
89+
$rc = array_merge($rc, self::getTraceLines($previous));
90+
}
91+
return $rc;
92+
}
93+
94+
/**
95+
* Provides the line of a stack trace as string (no shortening).
96+
* @param array $entry - the entry of the stack trace as given by getTrace()
97+
* @return string the entry as string
98+
*/
99+
protected static function getTraceLine($entry) {
100+
$rc = 'at ';
101+
if (isset($entry['file'])) $rc .= $entry['file'];
102+
if (isset($entry['line'])) $rc .= ' (line '.$entry['line'].')';
103+
if (isset($entry['class']) || isset($entry['type']) || isset($entry['function'])) {
104+
if (isset($entry['file'])) $rc .= ': ';
105+
if (isset($entry['class'])) $rc .= $entry['class'];
106+
if (isset($entry['type'])) $rc .= $entry['type'];
107+
if (isset($entry['function'])) {
108+
$rc .= $entry['function'].'(';
109+
if (isset($entry['args'])) {
110+
$first = TRUE;
111+
foreach ($entry['args'] AS $arg) {
112+
if ($first) $first = FALSE;
113+
else $rc .= ',';
114+
if (is_object($arg)) $rc .= get_class($arg);
115+
else if (is_array($arg)) $rc .= 'array';
116+
else if (is_string($arg)) $rc .= "'$arg'";
117+
else $rc .= $arg;
118+
}
119+
}
120+
$rc .= ')';
121+
}
122+
}
123+
return $rc;
124+
}
125+
61126
}
62127

0 commit comments

Comments
 (0)