Skip to content
Snippets Groups Projects
Commit 105c385a authored by Olav Morken's avatar Olav Morken
Browse files

Utilities: Split out buildBacktrace function from logBacktrace.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@791 44740490-163a-0410-bde0-09ae8108e29a
parent 41e10579
No related branches found
No related tags found
No related merge requests found
......@@ -271,63 +271,69 @@ class SimpleSAML_Utilities {
}
/* This function dumps a backtrace to the error log.
/**
* Build a backtrace.
*
* The log is in the following form:
* BT: (0) <filename>:<line> (<current function>)
* BT: (1) <filename>:<line> (<previous fucntion>)
* ...
* BT: (N) <filename>:<line> (N/A)
* This function takes in an exception and optionally a start depth, and
* builds a backtrace from that depth. The backtrace is returned as an
* array of strings, where each string represents one level in the stack.
*
* The log starts at the function which calls logBacktrace().
* @param Exception $exception The exception.
* @param int $startDepth The depth we should print the backtrace from.
* @return array The backtrace as an array of strings.
*/
public static function logBacktrace() {
public static function buildBacktrace(Exception $exception, $startDepth = 0) {
/* Get the backtrace. */
$bt = debug_backtrace();
assert('is_int($startDepth)');
/* Variable to hold the stack depth. */
$depth = 0;
$bt = array();
/* PHP stores the backtrace as a list of function calls.
* This means that $bt[0]['function'] contains the function
* which is called, while $bt[0]['line'] contains the line
* the function was called from.
*
* To get the form of bactrace we want, we are going to use
* $bt[i+1] to get the function and $bt[i] to get the file
* name and the line number.
*/
/* Position in the top function on the stack. */
$pos = $exception->getFile() . ':' . $exception->getLine();
for($i = 0; $i < count($bt); $i++) {
$file = $bt[$i]['file'];
$line = $bt[$i]['line'];
foreach($exception->getTrace() as $t) {
/* We can't get a function name or class for the source
* of the first call.
*/
if($i == count($bt) - 1) {
$function = 'N/A';
$class = NULL;
$function = $t['function'];
if(array_key_exists('class', $t)) {
$function = $t['class'] . '::' . $function;
}
$bt[] = $pos . ' (' . $function . ')';
if(array_key_exists('file', $t)) {
$pos = $t['file'] . ':' . $t['line'];
} else {
$function = $bt[$i+1]['function'];
$class = $bt[$i+1]['class'];
$pos = '[builtin]';
}
}
/* Attach the class name to the function name if
* we have a class name.
*/
if($class !== NULL) {
$function = $class . '::' . $function;
$bt[] = $pos . ' (N/A)';
/* Remove $startDepth elements from the top of the backtrace. */
$bt = array_slice($bt, $startDepth);
return $bt;
}
error_log('BT: (' . $depth . ') ' . $file . ':' .
$line . ' (' . $function . ')');
/**
* This function dumps a backtrace to the error log.
*
* The log is in the following form:
* BT: (0) <filename>:<line> (<current function>)
* BT: (1) <filename>:<line> (<previous fucntion>)
* ...
* BT: (N) <filename>:<line> (N/A)
*
* The log starts at the function which calls logBacktrace().
*/
public static function logBacktrace() {
$e = new Exception();
$depth++;
$bt = self::buildBackTrace($e, 1);
foreach($bt as $depth => $t) {
error_log('BT: (' . $depth . ') ' . $t);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment