From b97dfe7d4ffe14951c4248b99bb7c6162bd6b323 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 25 Oct 2016 14:47:53 +0100 Subject: [PATCH 1/3] Enhance stack_trace() method. --- core.php | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/core.php b/core.php index f806436..bdebd72 100644 --- a/core.php +++ b/core.php @@ -457,22 +457,58 @@ function system_extension_mime_type($ext) { /** * Generates a stack trace. - * @param bool $log_trace Whether to send the stack trace to the error log in addition to returning it. - * @return string A string prepresentation of a stack trace. + * @param bool $log_trace Whether to send the stack trace to the error log. + * @param bool $full Whether to output a full description of all the variables involved. + * @return string A string prepresentation of a stack trace. */ -function stack_trace($log_trace = true) +function stack_trace($log_trace = true, $full = false) { $result = ""; $stackTrace = debug_backtrace(); $stackHeight = count($stackTrace); foreach ($stackTrace as $i => $stackEntry) { - $result .= "#" . ($stackHeight - $i) . " - " . $stackEntry["file"] . ":" . $stackEntry["line"] . " (" . $stackEntry["function"] . ":" . count($stackEntry["args"]) . ")\n"; + $result .= "#" . ($stackHeight - $i) . ": "; + $result .= (isset($stackEntry["file"]) ? $stackEntry["file"] : "") . ":" . (isset($stackEntry["line"]) ? $stackEntry["line"] : "") . " - "; + if(isset($stackEntry["function"])) + { + $result .= "(in " . $stackEntry["function"]; + if(isset($stackEntry["args"]) && count($stackEntry["args"])) + { + $result .= ": "; + $result .= implode(", ", array_map($full ? "var_dump_ret" : "var_dump_short", $stackEntry["args"])); + } + } + $result .= ")\n"; } - if($log_trace) error_log($result); - + return $result; +} +/** + * Calls var_dump() and returns the output. + * @param mixed $var The thing to pass to var_dump(). + * @return string The output captured from var_dump(). + */ +function var_dump_ret($var) +{ + ob_start(); + var_dump($var); + return ob_get_clean(); +} + +/** + * Calls var_dump(), shortening the output for various types. + * @param mixed $var The thing to pass to var_dump(). + * @return string A shortened version of the var_dump() output. + */ +function var_dump_short($var) +{ + $result = trim(var_dump_ret($var)); + if(substr($result, 0, 6) === "object" || substr($result, 0, 5) === "array") + { + $result = substr($result, 0, strpos($result, " ")) . " { ... }"; + } return $result; } From 7e662bc83579641e7229228fd84d11f330d59a91 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 25 Oct 2016 14:57:25 +0100 Subject: [PATCH 2/3] Add unknown file / line text --- core.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core.php b/core.php index bdebd72..b865cc6 100644 --- a/core.php +++ b/core.php @@ -469,7 +469,7 @@ function stack_trace($log_trace = true, $full = false) foreach ($stackTrace as $i => $stackEntry) { $result .= "#" . ($stackHeight - $i) . ": "; - $result .= (isset($stackEntry["file"]) ? $stackEntry["file"] : "") . ":" . (isset($stackEntry["line"]) ? $stackEntry["line"] : "") . " - "; + $result .= (isset($stackEntry["file"]) ? $stackEntry["file"] : "(unknown file)") . ":" . (isset($stackEntry["line"]) ? $stackEntry["line"] : "(unknown line)") . " - "; if(isset($stackEntry["function"])) { $result .= "(in " . $stackEntry["function"]; From 475ae9f805394296837c108e3eb5b122e49c579d Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 25 Oct 2016 15:39:18 +0100 Subject: [PATCH 3/3] [stack_trace] in -> calling --- core.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core.php b/core.php index b865cc6..c1722c8 100644 --- a/core.php +++ b/core.php @@ -472,7 +472,7 @@ function stack_trace($log_trace = true, $full = false) $result .= (isset($stackEntry["file"]) ? $stackEntry["file"] : "(unknown file)") . ":" . (isset($stackEntry["line"]) ? $stackEntry["line"] : "(unknown line)") . " - "; if(isset($stackEntry["function"])) { - $result .= "(in " . $stackEntry["function"]; + $result .= "(calling " . $stackEntry["function"]; if(isset($stackEntry["args"]) && count($stackEntry["args"])) { $result .= ": ";