moodle_local_treestudyplan/classes/local/helpers/debugger.php

52 lines
1009 B
PHP
Raw Normal View History

2023-06-26 12:59:15 +02:00
<?php
2023-06-26 13:00:21 +02:00
namespace local_treestudyplan\local\helpers;
2023-06-26 12:59:15 +02:00
use DateTime;
class debugger {
private $fname;
private $tag;
private $enabled = false;
public function __construct($filename,$tag)
{
global $CFG;
$this->fname = $filename;
$this->tag = $tag;
// assume debug environment if cachejs is false
$this->enabled = (isset($CFG->cachejs) && $CFG->cachejs == false);
}
2023-06-26 21:44:31 +02:00
public function dump($object,string $tag="")
2023-06-26 12:59:15 +02:00
{
2023-06-26 21:44:31 +02:00
if(strlen($tag) > 0){
$tag .= ":\n";
}
$this->writeblock($tag.print_r($object,true));
2023-06-26 12:59:15 +02:00
}
public function write($str)
{
$this->writeblock($str);
}
private function writeblock($str){
if($this->enabled){
$now = new DateTime();
$tagline = "[ {$this->tag} - ".$now->format("c")." ]";
$fp = fopen($this->fname,"a");
fwrite($fp,$tagline . ":\n".$str."\n");
fclose($fp);
}
}
}