[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * Base abstract class for all the loggers to be used in backup/restore 27 * 28 * Any message passed will be processed by all the loggers in the defined chain 29 * (note some implementations may be not strictly "loggers" but classes performing 30 * other sort of tasks (avoiding browser/php timeouts, painters...). One simple 1-way 31 * basic chain of commands/responsibility pattern. 32 * 33 * TODO: Finish phpdocs 34 */ 35 abstract class base_logger implements checksumable { 36 37 protected $level; // minimum level of logging this logger must handle (valid level from @backup class) 38 protected $showdate; // flag to decide if the logger must output the date (true) or no (false) 39 protected $showlevel; // flag to decide if the logger must output the level (true) or no (false) 40 protected $next; // next logger in the chain 41 42 public function __construct($level, $showdate = false, $showlevel = false) { 43 // TODO: check level is correct 44 $this->level = $level; 45 $this->showdate = $showdate; 46 $this->showlevel = $showlevel; 47 $this->next = null; 48 } 49 50 public final function set_next($next) { 51 // TODO: Check is a base logger 52 53 // TODO: Check next hasn't been set already 54 55 // TODO: Avoid circular dependencies 56 if ($this->is_circular_reference($next)) { 57 $a = new stdclass(); 58 $a->alreadyinchain = get_class($this); 59 $a->main = get_class($next); 60 throw new base_logger_exception('logger_circular_reference', $a); 61 } 62 63 $this->next = $next; 64 } 65 66 public function get_next() { 67 return $this->next; 68 } 69 70 public function get_level() { 71 return $this->level; 72 } 73 74 // checksumable interface methods 75 76 public function calculate_checksum() { 77 // Checksum is a simple md5 hash of classname, level and 78 // on each specialised logger, its own atrributes 79 // Not following the chain at all. 80 return md5(get_class($this) . '-' . $this->level); 81 } 82 83 public function is_checksum_correct($checksum) { 84 return $this->calculate_checksum() === $checksum; 85 } 86 87 // Protected API starts here 88 89 abstract protected function action($message, $level, $options = null); // To implement 90 91 public final function process($message, $level, $options = null) { 92 $result = true; 93 if ($this->level != backup::LOG_NONE && $this->level >= $level) { // Perform action conditionally 94 $result = $this->action($message, $level, $options); 95 } 96 if ($result === false) { // Something was wrong, stop the chain 97 return $result; 98 } 99 if ($this->next !== null) { // The chain continues being processed 100 $result = $this->next->process($message, $level, $options); 101 } 102 return $result; 103 } 104 105 protected function is_circular_reference($obj) { 106 // Get object all nexts recursively and check if $this is already there 107 $nexts = $obj->get_nexts(); 108 if (array_key_exists($this->calculate_checksum(), $nexts) || $obj == $this) { 109 return true; 110 } 111 return false; 112 } 113 114 protected function get_nexts() { 115 $nexts = array(); 116 if ($this->next !== null) { 117 $nexts[$this->next->calculate_checksum()] = $this->next->calculate_checksum(); 118 $nexts = array_merge($nexts, $this->next->get_nexts()); 119 } 120 return $nexts; 121 } 122 123 protected function get_datestr() { 124 return userdate(time(), '%c'); 125 } 126 127 protected function get_levelstr($level) { 128 $result = 'undefined'; 129 switch ($level) { 130 case backup::LOG_ERROR: 131 $result = 'error'; 132 break; 133 case backup::LOG_WARNING: 134 $result = 'warn'; 135 break; 136 case backup::LOG_INFO: 137 $result = 'info'; 138 break; 139 case backup::LOG_DEBUG: 140 $result = 'debug'; 141 break; 142 } 143 return $result; 144 } 145 146 protected function get_prefix($level, $options) { 147 $prefix = ''; 148 if ($this->showdate) { 149 $prefix .= '[' . $this->get_datestr() . '] '; 150 } 151 if ($this->showlevel) { 152 $prefix .= '[' . $this->get_levelstr($level) . '] '; 153 } 154 return $prefix; 155 } 156 } 157 158 /* 159 * Exception class used by all the @base_logger stuff 160 */ 161 class base_logger_exception extends backup_exception { 162 163 public function __construct($errorcode, $a=NULL, $debuginfo=null) { 164 parent::__construct($errorcode, $a, $debuginfo); 165 } 166 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |