From 2efa13d843e3bef5e46c0130274abddea58e414c Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Mon, 13 Jul 2009 06:16:30 +0000
Subject: [PATCH] Error_Exception: Add functions to write the exception to the
 logs.

This patch adds several functions for writing the exception
to the logs with a full backtrace, including the causes for the exception.

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

diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php
index 0300b7e61..8820448e8 100644
--- a/lib/SimpleSAML/Error/Exception.php
+++ b/lib/SimpleSAML/Error/Exception.php
@@ -92,6 +92,96 @@ class SimpleSAML_Error_Exception extends Exception {
 	}
 
 
+	/**
+	 * Format this exception for logging.
+	 *
+	 * Create an array with lines for logging.
+	 *
+	 * @return array  Log lines which should be written out.
+	 */
+	public function format() {
+
+		$ret = array();
+
+		$e = $this;
+		do {
+			$err = get_class($e) . ': ' . $e->getMessage();
+			if ($e === $this) {
+				$ret[] = $err;
+			} else {
+				$ret[] = 'Caused by: ' . $err;
+			}
+
+			$ret[] = 'Backtrace:';
+
+			$depth = count($e->backtrace);
+			foreach ($e->backtrace as $i => $trace) {
+				$ret[] = ($depth - $i - 1) . ' ' . $trace;
+			}
+
+			$e = $e->cause;
+		} while ($e !== NULL);
+
+		return $ret;
+	}
+
+
+	/**
+	 * Print the exception to the log with log level error.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logError() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::error($line);
+		}
+	}
+
+
+	/**
+	 * Print the exception to the log with log level warning.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logWarning() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::warning($line);
+		}
+	}
+
+
+	/**
+	 * Print the exception to the log with log level info.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logInfo() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::debug($line);
+		}
+	}
+
+
+	/**
+	 * Print the exception to the log with log level debug.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logDebug() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::debug($line);
+		}
+	}
+
+
 	/**
 	 * Function for serialization.
 	 *
-- 
GitLab