moodle_local_treestudyplan/classes/local/helpers/debugger.php

69 lines
1.8 KiB
PHP
Raw Normal View History

2023-06-26 12:59:15 +02:00
<?php
2023-08-24 23:02:41 +02:00
// 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
*/
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 {
2023-08-24 23:02:41 +02:00
2023-06-26 12:59:15 +02:00
private $fname;
private $tag;
private $enabled = false;
2023-08-24 23:02:41 +02:00
public function __construct($filename, $tag) {
2023-06-26 12:59:15 +02:00
global $CFG;
$this->fname = $filename;
$this->tag = $tag;
2023-08-25 09:44:34 +02:00
// Assume debug environment if cachejs is false.
2023-06-26 12:59:15 +02:00
$this->enabled = (isset($CFG->cachejs) && $CFG->cachejs == false);
}
2023-08-24 23:02:41 +02:00
public function dump($object, string $tag="") {
if (strlen($tag) > 0) {
2023-06-26 21:44:31 +02:00
$tag .= ":\n";
}
2023-08-24 23:09:20 +02:00
$o = json_encode($object);
$this->writeblock($tag.": ".$o);
2023-06-26 12:59:15 +02:00
}
2023-08-24 23:02:41 +02:00
public function write($str) {
2023-06-26 12:59:15 +02:00
$this->writeblock($str);
}
2023-08-24 23:02:41 +02:00
private function writeblock($str) {
if ($this->enabled) {
2023-06-26 12:59:15 +02:00
$now = new DateTime();
$tagline = "[ {$this->tag} - ".$now->format("c")." ]";
2023-08-24 23:02:41 +02:00
$fp = fopen($this->fname, "a");
fwrite($fp, $tagline . ":\n".$str."\n");
2023-06-26 12:59:15 +02:00
fclose($fp);
}
}
2023-08-24 23:09:20 +02:00
}