1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.PrintStream;
  21. import java.io.StringReader;
  22. import org.apache.tools.ant.util.DateUtils;
  23. import org.apache.tools.ant.util.StringUtils;
  24. /**
  25. * Writes build events to a PrintStream. Currently, it
  26. * only writes which targets are being executed, and
  27. * any messages that get logged.
  28. *
  29. */
  30. public class DefaultLogger implements BuildLogger {
  31. /**
  32. * Size of left-hand column for right-justified task name.
  33. * @see #messageLogged(BuildEvent)
  34. */
  35. public static final int LEFT_COLUMN_SIZE = 12;
  36. /** PrintStream to write non-error messages to */
  37. protected PrintStream out;
  38. /** PrintStream to write error messages to */
  39. protected PrintStream err;
  40. /** Lowest level of message to write out */
  41. protected int msgOutputLevel = Project.MSG_ERR;
  42. /** Time of the start of the build */
  43. private long startTime = System.currentTimeMillis();
  44. /** Line separator */
  45. protected static final String lSep = StringUtils.LINE_SEP;
  46. /** Whether or not to use emacs-style output */
  47. protected boolean emacsMode = false;
  48. /**
  49. * Sole constructor.
  50. */
  51. public DefaultLogger() {
  52. }
  53. /**
  54. * Sets the highest level of message this logger should respond to.
  55. *
  56. * Only messages with a message level lower than or equal to the
  57. * given level should be written to the log.
  58. * <P>
  59. * Constants for the message levels are in the
  60. * {@link Project Project} class. The order of the levels, from least
  61. * to most verbose, is <code>MSG_ERR</code>, <code>MSG_WARN</code>,
  62. * <code>MSG_INFO</code>, <code>MSG_VERBOSE</code>,
  63. * <code>MSG_DEBUG</code>.
  64. * <P>
  65. * The default message level for DefaultLogger is Project.MSG_ERR.
  66. *
  67. * @param level the logging level for the logger.
  68. */
  69. public void setMessageOutputLevel(int level) {
  70. this.msgOutputLevel = level;
  71. }
  72. /**
  73. * Sets the output stream to which this logger is to send its output.
  74. *
  75. * @param output The output stream for the logger.
  76. * Must not be <code>null</code>.
  77. */
  78. public void setOutputPrintStream(PrintStream output) {
  79. this.out = new PrintStream(output, true);
  80. }
  81. /**
  82. * Sets the output stream to which this logger is to send error messages.
  83. *
  84. * @param err The error stream for the logger.
  85. * Must not be <code>null</code>.
  86. */
  87. public void setErrorPrintStream(PrintStream err) {
  88. this.err = new PrintStream(err, true);
  89. }
  90. /**
  91. * Sets this logger to produce emacs (and other editor) friendly output.
  92. *
  93. * @param emacsMode <code>true</code> if output is to be unadorned so that
  94. * emacs and other editors can parse files names, etc.
  95. */
  96. public void setEmacsMode(boolean emacsMode) {
  97. this.emacsMode = emacsMode;
  98. }
  99. /**
  100. * Responds to a build being started by just remembering the current time.
  101. *
  102. * @param event Ignored.
  103. */
  104. public void buildStarted(BuildEvent event) {
  105. startTime = System.currentTimeMillis();
  106. }
  107. /**
  108. * Prints whether the build succeeded or failed,
  109. * any errors the occurred during the build, and
  110. * how long the build took.
  111. *
  112. * @param event An event with any relevant extra information.
  113. * Must not be <code>null</code>.
  114. */
  115. public void buildFinished(BuildEvent event) {
  116. Throwable error = event.getException();
  117. StringBuffer message = new StringBuffer();
  118. if (error == null) {
  119. message.append(StringUtils.LINE_SEP);
  120. message.append("BUILD SUCCESSFUL");
  121. } else {
  122. message.append(StringUtils.LINE_SEP);
  123. message.append("BUILD FAILED");
  124. message.append(StringUtils.LINE_SEP);
  125. if (Project.MSG_VERBOSE <= msgOutputLevel
  126. || !(error instanceof BuildException)) {
  127. message.append(StringUtils.getStackTrace(error));
  128. } else {
  129. if (error instanceof BuildException) {
  130. message.append(error.toString()).append(lSep);
  131. } else {
  132. message.append(error.getMessage()).append(lSep);
  133. }
  134. }
  135. }
  136. message.append(StringUtils.LINE_SEP);
  137. message.append("Total time: ");
  138. message.append(formatTime(System.currentTimeMillis() - startTime));
  139. String msg = message.toString();
  140. if (error == null) {
  141. printMessage(msg, out, Project.MSG_VERBOSE);
  142. } else {
  143. printMessage(msg, err, Project.MSG_ERR);
  144. }
  145. log(msg);
  146. }
  147. /**
  148. * Logs a message to say that the target has started if this
  149. * logger allows information-level messages.
  150. *
  151. * @param event An event with any relevant extra information.
  152. * Must not be <code>null</code>.
  153. */
  154. public void targetStarted(BuildEvent event) {
  155. if (Project.MSG_INFO <= msgOutputLevel
  156. && !event.getTarget().getName().equals("")) {
  157. String msg = StringUtils.LINE_SEP
  158. + event.getTarget().getName() + ":";
  159. printMessage(msg, out, event.getPriority());
  160. log(msg);
  161. }
  162. }
  163. /**
  164. * No-op implementation.
  165. *
  166. * @param event Ignored.
  167. */
  168. public void targetFinished(BuildEvent event) {
  169. }
  170. /**
  171. * No-op implementation.
  172. *
  173. * @param event Ignored.
  174. */
  175. public void taskStarted(BuildEvent event) {
  176. }
  177. /**
  178. * No-op implementation.
  179. *
  180. * @param event Ignored.
  181. */
  182. public void taskFinished(BuildEvent event) {
  183. }
  184. /**
  185. * Logs a message, if the priority is suitable.
  186. * In non-emacs mode, task level messages are prefixed by the
  187. * task name which is right-justified.
  188. *
  189. * @param event A BuildEvent containing message information.
  190. * Must not be <code>null</code>.
  191. */
  192. public void messageLogged(BuildEvent event) {
  193. int priority = event.getPriority();
  194. // Filter out messages based on priority
  195. if (priority <= msgOutputLevel) {
  196. StringBuffer message = new StringBuffer();
  197. if (event.getTask() != null && !emacsMode) {
  198. // Print out the name of the task if we're in one
  199. String name = event.getTask().getTaskName();
  200. String label = "[" + name + "] ";
  201. int size = LEFT_COLUMN_SIZE - label.length();
  202. StringBuffer tmp = new StringBuffer();
  203. for (int i = 0; i < size; i++) {
  204. tmp.append(" ");
  205. }
  206. tmp.append(label);
  207. label = tmp.toString();
  208. try {
  209. BufferedReader r =
  210. new BufferedReader(
  211. new StringReader(event.getMessage()));
  212. String line = r.readLine();
  213. boolean first = true;
  214. while (line != null) {
  215. if (!first) {
  216. message.append(StringUtils.LINE_SEP);
  217. }
  218. first = false;
  219. message.append(label).append(line);
  220. line = r.readLine();
  221. }
  222. } catch (IOException e) {
  223. // shouldn't be possible
  224. message.append(label).append(event.getMessage());
  225. }
  226. } else {
  227. message.append(event.getMessage());
  228. }
  229. String msg = message.toString();
  230. if (priority != Project.MSG_ERR) {
  231. printMessage(msg, out, priority);
  232. } else {
  233. printMessage(msg, err, priority);
  234. }
  235. log(msg);
  236. }
  237. }
  238. /**
  239. * Convenience method to format a specified length of time.
  240. *
  241. * @param millis Length of time to format, in milliseconds.
  242. *
  243. * @return the time as a formatted string.
  244. *
  245. * @see DateUtils#formatElapsedTime(long)
  246. */
  247. protected static String formatTime(final long millis) {
  248. return DateUtils.formatElapsedTime(millis);
  249. }
  250. /**
  251. * Prints a message to a PrintStream.
  252. *
  253. * @param message The message to print.
  254. * Should not be <code>null</code>.
  255. * @param stream A PrintStream to print the message to.
  256. * Must not be <code>null</code>.
  257. * @param priority The priority of the message.
  258. * (Ignored in this implementation.)
  259. */
  260. protected void printMessage(final String message,
  261. final PrintStream stream,
  262. final int priority) {
  263. stream.println(message);
  264. }
  265. /**
  266. * Empty implementation which allows subclasses to receive the
  267. * same output that is generated here.
  268. *
  269. * @param message Message being logged. Should not be <code>null</code>.
  270. */
  271. protected void log(String message) {
  272. }
  273. }