[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /** 3 * $Id: XmlLogger.php 3076 2006-12-18 08:52:12Z fabien $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://phing.info>. 20 */ 21 22 require_once 'phing/listener/BuildLogger.php'; 23 require_once 'phing/listener/DefaultLogger.php'; 24 require_once 'phing/system/util/Timer.php'; 25 /** 26 * Generates a file in the current directory with 27 * an XML description of what happened during a build. 28 * The default filename is "log.xml", but this can be overridden 29 * with the property <code>XmlLogger.file</code>. 30 * 31 * @author Michiel Rook <michiel.rook@gmail.com> 32 * @version $Id: XmlLogger.php 3076 2006-12-18 08:52:12Z fabien $ 33 * @package phing.listener 34 */ 35 class XmlLogger implements BuildLogger 36 { 37 /** XML element name for a build. */ 38 const BUILD_TAG = "build"; 39 /** XML element name for a target. */ 40 const TARGET_TAG = "target"; 41 /** XML element name for a task. */ 42 const TASK_TAG = "task"; 43 /** XML element name for a message. */ 44 const MESSAGE_TAG = "message"; 45 /** XML attribute name for a name. */ 46 const NAME_ATTR = "name"; 47 /** XML attribute name for a time. */ 48 const TIME_ATTR = "time"; 49 /** XML attribute name for a message priority. */ 50 const PRIORITY_ATTR = "priority"; 51 /** XML attribute name for a file location. */ 52 const LOCATION_ATTR = "location"; 53 /** XML attribute name for an error description. */ 54 const ERROR_ATTR = "error"; 55 /** XML element name for a stack trace. */ 56 const STACKTRACE_TAG = "stacktrace"; 57 58 private $doc = NULL; 59 60 private $buildStartTime = 0; 61 private $targetStartTime = 0; 62 private $taskStartTime = 0; 63 64 private $buildElement = NULL; 65 66 private $msgOutputLevel = PROJECT_MSG_DEBUG; 67 68 /** 69 * Constructs a new BuildListener that logs build events to an XML file. 70 */ 71 function __construct() 72 { 73 $this->doc = new DOMDocument(); 74 $this->doc->formatOutput = true; 75 76 $this->buildTimer = new Timer(); 77 $this->targetTimer = new Timer(); 78 $this->taskTimer = new Timer(); 79 } 80 81 /** 82 * Fired when the build starts, this builds the top-level element for the 83 * document and remembers the time of the start of the build. 84 * 85 * @param BuildEvent Ignored. 86 */ 87 function buildStarted(BuildEvent $event) 88 { 89 $this->buildTimerStart = Phing::currentTimeMillis(); 90 $this->buildElement = $this->doc->createElement(XmlLogger::BUILD_TAG); 91 } 92 93 /** 94 * Fired when the build finishes, this adds the time taken and any 95 * error stacktrace to the build element and writes the document to disk. 96 * 97 * @param BuildEvent An event with any relevant extra information. 98 * Will not be <code>null</code>. 99 */ 100 function buildFinished(BuildEvent $event) 101 { 102 $this->buildTimer->stop(); 103 104 $elapsedTime = Phing::currentTimeMillis() - $this->buildTimerStart; 105 106 $this->buildElement->setAttribute(XmlLogger::TIME_ATTR, DefaultLogger::_formatTime($elapsedTime)); 107 108 if ($event->getException() != null) 109 { 110 $this->buildElement->setAttribute(XmlLogger::ERROR_ATTR, $event->getException()->toString()); 111 112 $errText = $this->doc->createCDATASection($event->getException()->getTraceAsString()); 113 $stacktrace = $this->doc->createElement(XmlLogger::STACKTRACE_TAG); 114 $stacktrace->appendChild($errText); 115 $this->buildElement->appendChild($stacktrace); 116 } 117 118 $outFilename = $event->getProject()->getProperty("XmlLogger.file"); 119 120 if ($outFilename == "") 121 { 122 $outFilename = "log.xml"; 123 } 124 $writer = new FileWriter($outFilename); 125 126 $writer->write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 127 $writer->write($this->doc->saveXML($this->buildElement)); 128 $writer->close(); 129 } 130 /** 131 * Fired when a target starts building, remembers the current time and the name of the target. 132 * 133 * @param BuildEvent An event with any relevant extra information. 134 * Will not be <code>null</code>. 135 */ 136 function targetStarted(BuildEvent $event) 137 { 138 $target = $event->getTarget(); 139 140 $this->targetTimerStart = Phing::currentTimeMillis(); 141 142 $this->targetElement = $this->doc->createElement(XmlLogger::TARGET_TAG); 143 $this->targetElement->setAttribute(XmlLogger::NAME_ATTR, $target->getName()); 144 } 145 146 /** 147 * Fired when a target finishes building, this adds the time taken 148 * to the appropriate target element in the log. 149 * 150 * @param BuildEvent An event with any relevant extra information. 151 * Will not be <code>null</code>. 152 */ 153 function targetFinished(BuildEvent $event) 154 { 155 $target = $event->getTarget(); 156 157 $elapsedTime = Phing::currentTimeMillis() - $this->targetTimerStart; 158 159 $this->targetElement->setAttribute(XmlLogger::TIME_ATTR, DefaultLogger::_formatTime($elapsedTime)); 160 161 $this->buildElement->appendChild($this->targetElement); 162 } 163 164 /** 165 * Fired when a task starts building, remembers the current time and the name of the task. 166 * 167 * @param BuildEvent An event with any relevant extra information. 168 * Will not be <code>null</code>. 169 */ 170 function taskStarted(BuildEvent $event) 171 { 172 $task = $event->getTask(); 173 174 $this->taskTimerStart = Phing::currentTimeMillis(); 175 176 $this->taskElement = $this->doc->createElement(XmlLogger::TASK_TAG); 177 $this->taskElement->setAttribute(XmlLogger::NAME_ATTR, $task->getTaskName()); 178 $this->taskElement->setAttribute(XmlLogger::LOCATION_ATTR, $task->getLocation()->toString()); 179 } 180 /** 181 * Fired when a task finishes building, this adds the time taken 182 * to the appropriate task element in the log. 183 * 184 * @param BuildEvent An event with any relevant extra information. 185 * Will not be <code>null</code>. 186 */ 187 function taskFinished(BuildEvent $event) 188 { 189 $task = $event->getTask(); 190 191 $elapsedTime = Phing::currentTimeMillis() - $this->taskTimerStart; 192 $this->taskElement->setAttribute(XmlLogger::TIME_ATTR, DefaultLogger::_formatTime($elapsedTime)); 193 194 $this->targetElement->appendChild($this->taskElement); 195 } 196 197 /** 198 * Fired when a message is logged, this adds a message element to the 199 * most appropriate parent element (task, target or build) and records 200 * the priority and text of the message. 201 * 202 * @param BuildEvent An event with any relevant extra information. 203 * Will not be <code>null</code>. 204 */ 205 function messageLogged(BuildEvent $event) 206 { 207 $priority = $event->getPriority(); 208 209 if ($priority > $this->msgOutputLevel) 210 { 211 return; 212 } 213 214 $messageElement = $this->doc->createElement(XmlLogger::MESSAGE_TAG); 215 216 switch ($priority) 217 { 218 case PROJECT_MSG_ERR: 219 $name = "error"; 220 break; 221 222 case PROJECT_MSG_WARN: 223 $name = "warn"; 224 break; 225 226 case PROJECT_MSG_INFO: 227 $name = "info"; 228 break; 229 230 default: 231 $name = "debug"; 232 break; 233 } 234 235 $messageElement->setAttribute(XmlLogger::PRIORITY_ATTR, $name); 236 237 $messageText = $this->doc->createCDATASection($event->getMessage()); 238 239 $messageElement->appendChild($messageText); 240 241 if ($event->getTask() != null) 242 { 243 $this->taskElement->appendChild($messageElement); 244 } 245 else 246 if ($event->getTarget() != null) 247 { 248 $this->targetElement->appendChild($messageElement); 249 } 250 else 251 if ($this->buildElement != null) 252 { 253 $this->buildElement->appendChild($messageElement); 254 } 255 } 256 257 /** 258 * Set the logging level when using this as a Logger 259 */ 260 function setMessageOutputLevel($level) 261 { 262 $this->msgOutputLevel = $level; 263 } 264 }; 265 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |