From 78e9dffd198c11c5bc3e15443734ca4e4f5a619c Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 28 Nov 2007 14:11:18 +0000
Subject: [PATCH] New utility method: logBacktrace

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@78 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Utilities.php | 58 ++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 91a1ef640..c9c40a423 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -116,6 +116,64 @@ class SimpleSAML_Utilities {
 		return $uniqueid;
 	}
 	
+
+	/* 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() {
+
+		/* Get the backtrace. */
+		$bt = debug_backtrace();
+
+		/* Variable to hold the stack depth. */
+		$depth = 0;
+
+		/* 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.
+		 */
+
+		for($i = 0; $i < count($bt); $i++) {
+			$file = $bt[$i]['file'];
+			$line = $bt[$i]['line'];
+
+			/* 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;
+			} else {
+				$function = $bt[$i+1]['function'];
+				$class = $bt[$i+1]['class'];
+			}
+
+			/* Attach the class name to the function name if
+			 * we have a class name.
+			 */
+			if($class !== NULL) {
+				$function = $class  . '::' . $function;
+			}
+
+
+			error_log('BT: (' . $depth . ') ' . $file . ':' .
+			          $line . ' (' . $function . ')');
+
+			$depth++;
+		}
+	}
 }
 
 ?>
\ No newline at end of file
-- 
GitLab