Tweaked debug tools

This commit is contained in:
PMKuipers 2023-12-01 11:13:13 +01:00
parent c9d9703a04
commit 41eee1a7aa

View File

@ -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