|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 class file_logger extends base_logger { 00031 00032 protected $fullpath; // Full path to OS file where contents will be stored 00033 protected $fhandle; // File handle where all write operations happen 00034 00035 public function __construct($level, $showdate = false, $showlevel = false, $fullpath = null) { 00036 if (empty($fullpath)) { 00037 throw new base_logger_exception('missing_fullpath_parameter', $fullpath); 00038 } 00039 if (!is_writable(dirname($fullpath))) { 00040 throw new base_logger_exception('file_not_writable', $fullpath); 00041 } 00042 // Open the OS file for writing (append) 00043 $this->fullpath = $fullpath; 00044 if ($level > backup::LOG_NONE) { // Only create the file if we are going to log something 00045 if (! $this->fhandle = fopen($this->fullpath, 'a')) { 00046 throw new base_logger_exception('error_opening_file', $fullpath); 00047 } 00048 } 00049 parent::__construct($level, $showdate, $showlevel); 00050 } 00051 00052 public function __destruct() { 00053 @fclose($this->fhandle); // Blindy close the file handler (no exceptions in destruct) 00054 } 00055 00056 public function __sleep() { 00057 @fclose($this->fhandle); // Blindy close the file handler before serialization 00058 return array('level', 'showdate', 'showlevel', 'next', 'fullpath'); 00059 } 00060 00061 public function __wakeup() { 00062 if ($this->level > backup::LOG_NONE) { // Only create the file if we are going to log something 00063 if (! $this->fhandle = fopen($this->fullpath, 'a')) { 00064 throw new base_logger_exception('error_opening_file', $this->fullpath); 00065 } 00066 } 00067 } 00068 00069 // Protected API starts here 00070 00071 protected function action($message, $level, $options = null) { 00072 $prefix = $this->get_prefix($level, $options); 00073 $depth = isset($options['depth']) ? $options['depth'] : 0; 00074 // Depending of the type (extension of the file), format differently 00075 if (substr($this->fullpath, -5) !== '.html') { 00076 $content = $prefix . str_repeat(' ', $depth) . $message . PHP_EOL; 00077 } else { 00078 $content = $prefix . str_repeat(' ', $depth) . htmlentities($message, ENT_QUOTES) . '<br/>' . PHP_EOL; 00079 } 00080 if (false === fwrite($this->fhandle, $content)) { 00081 throw new base_logger_exception('error_writing_file', $this->fullpath); 00082 } 00083 return true; 00084 } 00085 }