From 41eee1a7aab6e39e18e01fd90163b0d143226289 Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Fri, 1 Dec 2023 11:13:13 +0100 Subject: [PATCH] Tweaked debug tools --- classes/debug.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/classes/debug.php b/classes/debug.php index d374a9e..957e525 100644 --- a/classes/debug.php +++ b/classes/debug.php @@ -21,6 +21,9 @@ */ namespace local_treestudyplan; + +use Exception; + defined('MOODLE_INTERNAL') || die(); class debug { @@ -31,11 +34,30 @@ class debug { */ public static function &dump(&$object,$filename="/tmp/debug.log") { $f = fopen($filename,"a+"); - fwrite($f,\json_encode($object,JSON_PRETTY_PRINT )."\n"); + try { + $json = \json_encode($object,JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR ); + fwrite($f,$json."\n"); + } catch (Exception $x) { + fwrite($f,"Error processing json: ". $x->getMessage()."\n"); + fwrite($f,"Print_r dump: \n".print_r($object,true)."\n"); + } fclose($f); return $object; } + /** + * @param $object Object to dump + * @param $filename File to write to + * @return any The object + */ + public static function &print_r(&$object,$filename="/tmp/debug.log") { + $f = fopen($filename,"a+"); + fwrite($f,"Print_r dump: \n".print_r($object,true)."\n"); + fclose($f); + return $object; + } + + /** * @param $object Object to dump * @param $filename File to write to