source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/DefaultLogger.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

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