moodle_local_treestudyplan/classes/local/helpers/debugger.php
2023-08-24 23:09:20 +02:00

69 lines
1.8 KiB
PHP

<?php
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
*
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan\local\helpers;
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);
}
public function dump($object, string $tag="") {
if (strlen($tag) > 0) {
$tag .= ":\n";
}
$o = json_encode($object);
$this->writeblock($tag.": ".$o);
}
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);
}
}
}