1<?php 2 3// This file is part of Moodle - http://moodle.org/ 4// 5// Moodle is free software: you can redistribute it and/or modify 6// it under the terms of the GNU General Public License as published by 7// the Free Software Foundation, either version 3 of the License, or 8// (at your option) any later version. 9// 10// Moodle is distributed in the hope that it will be useful, 11// but WITHOUT ANY WARRANTY; without even the implied warranty of 12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13// GNU General Public License for more details. 14// 15// You should have received a copy of the GNU General Public License 16// along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18/** 19 * @package moodlecore 20 * @subpackage backup-logger 21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25/** 26 * Logger implementation that sends indented messages (depth option) to one file 27 * 28 * TODO: Finish phpdocs 29 */ 30class file_logger extends base_logger { 31 32 protected $fullpath; // Full path to OS file where contents will be stored 33 protected $fhandle; // File handle where all write operations happen 34 35 public function __construct($level, $showdate = false, $showlevel = false, $fullpath = null) { 36 if (empty($fullpath)) { 37 throw new base_logger_exception('missing_fullpath_parameter', $fullpath); 38 } 39 if (!is_writable(dirname($fullpath))) { 40 throw new base_logger_exception('file_not_writable', $fullpath); 41 } 42 // Open the OS file for writing (append) 43 $this->fullpath = $fullpath; 44 if ($level > backup::LOG_NONE) { // Only create the file if we are going to log something 45 if (! $this->fhandle = fopen($this->fullpath, 'a')) { 46 throw new base_logger_exception('error_opening_file', $fullpath); 47 } 48 } 49 parent::__construct($level, $showdate, $showlevel); 50 } 51 52 public function __destruct() { 53 if (is_resource($this->fhandle)) { 54 // Blindy close the file handler (no exceptions in destruct). 55 @fclose($this->fhandle); 56 } 57 } 58 59 public function __sleep() { 60 if (is_resource($this->fhandle)) { 61 // Blindy close the file handler before serialization. 62 @fclose($this->fhandle); 63 $this->fhandle = null; 64 } 65 return array('level', 'showdate', 'showlevel', 'next', 'fullpath'); 66 } 67 68 public function __wakeup() { 69 if ($this->level > backup::LOG_NONE) { // Only create the file if we are going to log something 70 if (! $this->fhandle = fopen($this->fullpath, 'a')) { 71 throw new base_logger_exception('error_opening_file', $this->fullpath); 72 } 73 } 74 } 75 76 /** 77 * Close the logger resources (file handle) if still open. 78 * 79 * @since Moodle 3.1 80 */ 81 public function close() { 82 // Close the file handle if hasn't been closed already. 83 if (is_resource($this->fhandle)) { 84 fclose($this->fhandle); 85 $this->fhandle = null; 86 } 87 } 88 89// Protected API starts here 90 91 protected function action($message, $level, $options = null) { 92 $prefix = $this->get_prefix($level, $options); 93 $depth = isset($options['depth']) ? $options['depth'] : 0; 94 // Depending of the type (extension of the file), format differently 95 if (substr($this->fullpath, -5) !== '.html') { 96 $content = $prefix . str_repeat(' ', $depth) . $message . PHP_EOL; 97 } else { 98 $content = $prefix . str_repeat(' ', $depth) . htmlentities($message, ENT_QUOTES, 'UTF-8') . '<br/>' . PHP_EOL; 99 } 100 if (!is_resource($this->fhandle) || (false === fwrite($this->fhandle, $content))) { 101 throw new base_logger_exception('error_writing_file', $this->fullpath); 102 } 103 return true; 104 } 105} 106