[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: PearLogger.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/BuildListener.php'; 23 include_once 'phing/BuildEvent.php'; 24 require_once 'Log.php'; 25 26 /** 27 * Writes log messages to PEAR Log. 28 * 29 * By default it will log to file in current directory w/ name 'phing.log'. You can customize 30 * this behavior by setting properties: 31 * - pear.log.type 32 * - pear.log.name 33 * - pear.log.ident (note that this class changes ident to project name) 34 * - pear.log.conf (note that array values are currently unsupported in Phing property files) 35 * 36 * <code> 37 * phing -f build.xml -logger phing.listener.PearLogger -Dpear.log.type=file -Dpear.log.name=/path/to/log.log 38 * </code> 39 * 40 * @author Hans Lellelid <hans@xmpl.org> 41 * @version $Revision: 1.3 $ $Date: 2004/03/15 14:45:06 $ 42 * @see BuildEvent 43 * @package phing.listener 44 */ 45 class PearLogger implements BuildListener { 46 47 /** 48 * Size of the left column in output. The default char width is 12. 49 * @var int 50 */ 51 const LEFT_COLUMN_SIZE = 12; 52 53 /** 54 * The message output level that should be used. The default is 55 * <code>PROJECT_MSG_VERBOSE</code>. 56 * @var int 57 */ 58 protected $msgOutputLevel = PROJECT_MSG_ERR; 59 60 /** 61 * Time that the build started 62 * @var int 63 */ 64 protected $startTime; 65 66 /** 67 * Maps Phing PROJECT_MSG_* constants to PEAR_LOG_* constants. 68 * @var array 69 */ 70 protected static $levelMap = array( PROJECT_MSG_DEBUG => PEAR_LOG_DEBUG, 71 PROJECT_MSG_INFO => PEAR_LOG_INFO, 72 PROJECT_MSG_VERBOSE => PEAR_LOG_NOTICE, 73 PROJECT_MSG_WARN => PEAR_LOG_WARNING, 74 PROJECT_MSG_ERR => PEAR_LOG_ERR 75 ); 76 /** 77 * Whether logging has been configured. 78 * @var boolean 79 */ 80 protected $logConfigured = false; 81 82 /** 83 * Configure the logger. 84 */ 85 protected function configureLogging() { 86 87 $type = Phing::getDefinedProperty('pear.log.type'); 88 $name = Phing::getDefinedProperty('pear.log.name'); 89 $ident = Phing::getDefinedProperty('pear.log.ident'); 90 $conf = Phing::getDefinedProperty('pear.log.conf'); 91 92 if ($type === null) $type = 'file'; 93 if ($name === null) $name = 'phing.log'; 94 if ($ident === null) $ident = 'phing'; 95 if ($conf === null) $conf = array(); 96 97 $this->logger = Log::singleton($type, $name, $ident, $conf, self::$levelMap[$this->msgOutputLevel]); 98 } 99 100 /** 101 * Get the configured PEAR logger to use. 102 * This method just ensures that logging has been configured and returns the configured logger. 103 * @return Log 104 */ 105 protected function logger() { 106 if (!$this->logConfigured) { 107 $this->configureLogging(); 108 } 109 return $this->logger; 110 } 111 112 /** 113 * Set the msgOutputLevel this logger is to respond to. 114 * 115 * Only messages with a message level lower than or equal to the given 116 * level are output to the log. 117 * 118 * <p> Constants for the message levels are in Project.php. The order of 119 * the levels, from least to most verbose, is: 120 * 121 * <ul> 122 * <li>PROJECT_MSG_ERR</li> 123 * <li>PROJECT_MSG_WARN</li> 124 * <li>PROJECT_MSG_INFO</li> 125 * <li>PROJECT_MSG_VERBOSE</li> 126 * <li>PROJECT_MSG_DEBUG</li> 127 * </ul> 128 * 129 * The default message level for DefaultLogger is PROJECT_MSG_ERR. 130 * 131 * @param integer the logging level for the logger. 132 * @access public 133 */ 134 function setMessageOutputLevel($level) { 135 $this->msgOutputLevel = (int) $level; 136 } 137 138 /** 139 * Sets the start-time when the build started. Used for calculating 140 * the build-time. 141 * 142 * @param object The BuildEvent 143 * @access public 144 */ 145 146 function buildStarted(BuildEvent $event) { 147 $this->startTime = Phing::currentTimeMillis(); 148 $this->logger()->setIdent($event->getProject()->getName()); 149 $this->logger()->info("Starting build with buildfile: ". $event->getProject()->getProperty("phing.file")); 150 } 151 152 /** 153 * Prints whether the build succeeded or failed, and any errors that 154 * occured during the build. Also outputs the total build-time. 155 * 156 * @param object The BuildEvent 157 * @access public 158 * @see BuildEvent::getException() 159 */ 160 function buildFinished(BuildEvent $event) { 161 $error = $event->getException(); 162 if ($error === null) { 163 $msg = "Finished successful build."; 164 } else { 165 $msg = "Build failed. [reason: " . $error->getMessage() ."]"; 166 } 167 $this->logger()->log($msg . " Total time: " . $this->_formatTime(Phing::currentTimeMillis() - $this->startTime)); 168 } 169 170 /** 171 * Prints the current target name 172 * 173 * @param object The BuildEvent 174 * @access public 175 * @see BuildEvent::getTarget() 176 */ 177 function targetStarted(BuildEvent $event) {} 178 179 /** 180 * Fired when a target has finished. We don't need specific action on this 181 * event. So the methods are empty. 182 * 183 * @param object The BuildEvent 184 * @access public 185 * @see BuildEvent::getException() 186 */ 187 function targetFinished(BuildEvent $event) {} 188 189 /** 190 * Fired when a task is started. We don't need specific action on this 191 * event. So the methods are empty. 192 * 193 * @param object The BuildEvent 194 * @access public 195 * @see BuildEvent::getTask() 196 */ 197 function taskStarted(BuildEvent $event) {} 198 199 /** 200 * Fired when a task has finished. We don't need specific action on this 201 * event. So the methods are empty. 202 * 203 * @param object The BuildEvent 204 * @access public 205 * @see BuildEvent::getException() 206 */ 207 function taskFinished(BuildEvent $event) {} 208 209 /** 210 * Print a message to the stdout. 211 * 212 * @param object The BuildEvent 213 * @access public 214 * @see BuildEvent::getMessage() 215 */ 216 function messageLogged(BuildEvent $event) { 217 if ($event->getPriority() <= $this->msgOutputLevel) { 218 $msg = ""; 219 if ($event->getTask() !== null) { 220 $name = $event->getTask(); 221 $name = $name->getTaskName(); 222 $msg = str_pad("[$name] ", self::LEFT_COLUMN_SIZE, " ", STR_PAD_LEFT); 223 } 224 $msg .= $event->getMessage(); 225 $this->logger()->log($msg, self::$levelMap[$event->getPriority()]); 226 } 227 } 228 229 /** 230 * Formats a time micro integer to human readable format. 231 * 232 * @param integer The time stamp 233 * @access private 234 */ 235 function _formatTime($micros) { 236 $seconds = $micros; 237 $minutes = $seconds / 60; 238 if ($minutes > 1) { 239 return sprintf("%1.0f minute%s %0.2f second%s", 240 $minutes, ($minutes === 1 ? " " : "s "), 241 $seconds - floor($seconds/60) * 60, ($seconds%60 === 1 ? "" : "s")); 242 } else { 243 return sprintf("%0.4f second%s", $seconds, ($seconds%60 === 1 ? "" : "s")); 244 } 245 } 246 }
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 |