PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/build/ext/phing/classes/phing/listener/PearLogListener.php

http://github.com/alexgorbatchev/SyntaxHighlighter
PHP | 197 lines | 65 code | 19 blank | 113 comment | 11 complexity | 90a4424fef532c8a9524ba6d80ce6003 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: PearLogListener.php 227 2007-08-28 02:17:00Z hans $
  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. require_once 'phing/BuildListener.php';
  22. /**
  23. * Writes build messages to PEAR Log.
  24. *
  25. * By default it will log to file in current directory w/ name 'phing.log'. You can customize
  26. * this behavior by setting properties:
  27. * - pear.log.type
  28. * - pear.log.name
  29. * - pear.log.ident (note that this class changes ident to project name)
  30. * - pear.log.conf (note that array values are currently unsupported in Phing property files)
  31. *
  32. * <code>
  33. * phing -f build.xml -logger phing.listener.PearLogger -Dpear.log.type=file -Dpear.log.name=/path/to/log.log
  34. * </code>
  35. *
  36. * @author Hans Lellelid <hans@xmpl.org>
  37. * @version $Revision: 1.3 $ $Date: 2007-08-27 22:17:00 -0400 (Mon, 27 Aug 2007) $
  38. * @see BuildEvent
  39. * @package phing.listener
  40. */
  41. class PearLogListener implements BuildListener {
  42. /**
  43. * Size of the left column in output. The default char width is 12.
  44. * @var int
  45. */
  46. const LEFT_COLUMN_SIZE = 12;
  47. /**
  48. * Time that the build started
  49. * @var int
  50. */
  51. protected $startTime;
  52. /**
  53. * Maps Phing Project::MSG_* constants to PEAR_LOG_* constants.
  54. * @var array
  55. */
  56. protected static $levelMap = array( Project::MSG_DEBUG => PEAR_LOG_DEBUG,
  57. Project::MSG_INFO => PEAR_LOG_INFO,
  58. Project::MSG_VERBOSE => PEAR_LOG_NOTICE,
  59. Project::MSG_WARN => PEAR_LOG_WARNING,
  60. Project::MSG_ERR => PEAR_LOG_ERR
  61. );
  62. /**
  63. * Whether logging has been configured.
  64. * @var boolean
  65. */
  66. protected $logConfigured = false;
  67. /**
  68. * @var Log PEAR Log object.
  69. */
  70. protected $logger;
  71. /**
  72. * Configure the logger.
  73. */
  74. protected function configureLogging() {
  75. $type = Phing::getDefinedProperty('pear.log.type');
  76. $name = Phing::getDefinedProperty('pear.log.name');
  77. $ident = Phing::getDefinedProperty('pear.log.ident');
  78. $conf = Phing::getDefinedProperty('pear.log.conf');
  79. if ($type === null) $type = 'file';
  80. if ($name === null) $name = 'phing.log';
  81. if ($ident === null) $ident = 'phing';
  82. if ($conf === null) $conf = array();
  83. include_once 'Log.php';
  84. if (!class_exists('Log')) {
  85. throw new BuildException("Cannot find PEAR Log class for use by PearLogger.");
  86. }
  87. $this->logger = Log::singleton($type, $name, $ident, $conf, self::$levelMap[$this->msgOutputLevel]);
  88. }
  89. /**
  90. * Get the configured PEAR logger to use.
  91. * This method just ensures that logging has been configured and returns the configured logger.
  92. * @return Log
  93. */
  94. protected function logger() {
  95. if (!$this->logConfigured) {
  96. $this->configureLogging();
  97. }
  98. return $this->logger;
  99. }
  100. /**
  101. * Sets the start-time when the build started. Used for calculating
  102. * the build-time.
  103. *
  104. * @param BuildEvent The BuildEvent
  105. */
  106. public function buildStarted(BuildEvent $event) {
  107. $this->startTime = Phing::currentTimeMillis();
  108. $this->logger()->setIdent($event->getProject()->getName());
  109. $this->logger()->info("Starting build with buildfile: ". $event->getProject()->getProperty("phing.file"));
  110. }
  111. /**
  112. * Logs whether the build succeeded or failed, and any errors that
  113. * occured during the build. Also outputs the total build-time.
  114. *
  115. * @param BuildEvent The BuildEvent
  116. * @see BuildEvent::getException()
  117. */
  118. public function buildFinished(BuildEvent $event) {
  119. $error = $event->getException();
  120. if ($error === null) {
  121. $msg = "Finished successful build.";
  122. } else {
  123. $msg = "Build failed. [reason: " . $error->getMessage() ."]";
  124. }
  125. $this->logger()->log($msg . " Total time: " . DefaultLogger::formatTime(Phing::currentTimeMillis() - $this->startTime));
  126. }
  127. /**
  128. * Logs the current target name
  129. *
  130. * @param BuildEvent The BuildEvent
  131. * @see BuildEvent::getTarget()
  132. */
  133. public function targetStarted(BuildEvent $event) {}
  134. /**
  135. * Fired when a target has finished. We don't need specific action on this
  136. * event. So the methods are empty.
  137. *
  138. * @param BuildEvent The BuildEvent
  139. * @access public
  140. * @see BuildEvent::getException()
  141. */
  142. public function targetFinished(BuildEvent $event) {}
  143. /**
  144. * Fired when a task is started. We don't need specific action on this
  145. * event. So the methods are empty.
  146. *
  147. * @param BuildEvent The BuildEvent
  148. * @access public
  149. * @see BuildEvent::getTask()
  150. */
  151. public function taskStarted(BuildEvent $event) {}
  152. /**
  153. * Fired when a task has finished. We don't need specific action on this
  154. * event. So the methods are empty.
  155. *
  156. * @param BuildEvent The BuildEvent
  157. * @see BuildEvent::getException()
  158. */
  159. public function taskFinished(BuildEvent $event) {}
  160. /**
  161. * Logs a message to the configured PEAR logger.
  162. *
  163. * @param BuildEvent The BuildEvent
  164. * @see BuildEvent::getMessage()
  165. */
  166. public function messageLogged(BuildEvent $event) {
  167. if ($event->getPriority() <= $this->msgOutputLevel) {
  168. $msg = "";
  169. if ($event->getTask() !== null) {
  170. $name = $event->getTask();
  171. $name = $name->getTaskName();
  172. $msg = str_pad("[$name] ", self::LEFT_COLUMN_SIZE, " ", STR_PAD_LEFT);
  173. }
  174. $msg .= $event->getMessage();
  175. $this->logger()->log($msg, self::$levelMap[$event->getPriority()]);
  176. }
  177. }
  178. }