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

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

initial import of LiRK3

File size: 9.0 KB
Line 
1/*
2 * Copyright 2001-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 */
17package org.apache.tools.ant.taskdefs;
18
19import java.io.FileOutputStream;
20import java.io.IOException;
21import java.io.PrintStream;
22import org.apache.tools.ant.BuildEvent;
23import org.apache.tools.ant.BuildException;
24import org.apache.tools.ant.BuildLogger;
25import org.apache.tools.ant.DefaultLogger;
26import org.apache.tools.ant.Project;
27import org.apache.tools.ant.SubBuildListener;
28import org.apache.tools.ant.util.StringUtils;
29
30/**
31 * This is a class that represents a recorder. This is the listener to the
32 * build process.
33 *
34 * @since Ant 1.4
35 */
36public class RecorderEntry implements BuildLogger, SubBuildListener {
37
38 //////////////////////////////////////////////////////////////////////
39 // ATTRIBUTES
40
41 /** The name of the file associated with this recorder entry. */
42 private String filename = null;
43 /** The state of the recorder (recorder on or off). */
44 private boolean record = true;
45 /** The current verbosity level to record at. */
46 private int loglevel = Project.MSG_INFO;
47 /** The output PrintStream to record to. */
48 private PrintStream out = null;
49 /** The start time of the last know target. */
50 private long targetStartTime = 0L;
51 /** Strip task banners if true. */
52 private boolean emacsMode = false;
53 /** project instance the recorder is associated with */
54 private Project project;
55
56 //////////////////////////////////////////////////////////////////////
57 // CONSTRUCTORS / INITIALIZERS
58
59 /**
60 * @param name The name of this recorder (used as the filename).
61 */
62 protected RecorderEntry(String name) {
63 targetStartTime = System.currentTimeMillis();
64 filename = name;
65 }
66
67 //////////////////////////////////////////////////////////////////////
68 // ACCESSOR METHODS
69
70 /**
71 * @return the name of the file the output is sent to.
72 */
73 public String getFilename() {
74 return filename;
75 }
76
77
78 /**
79 * Turns off or on this recorder.
80 *
81 * @param state true for on, false for off, null for no change.
82 */
83 public void setRecordState(Boolean state) {
84 if (state != null) {
85 flush();
86 record = state.booleanValue();
87 }
88 }
89
90
91 public void buildStarted(BuildEvent event) {
92 log("> BUILD STARTED", Project.MSG_DEBUG);
93 }
94
95
96 public void buildFinished(BuildEvent event) {
97 log("< BUILD FINISHED", Project.MSG_DEBUG);
98
99 if (record && out != null) {
100 Throwable error = event.getException();
101
102 if (error == null) {
103 out.println(StringUtils.LINE_SEP + "BUILD SUCCESSFUL");
104 } else {
105 out.println(StringUtils.LINE_SEP + "BUILD FAILED"
106 + StringUtils.LINE_SEP);
107 error.printStackTrace(out);
108 }
109 }
110 cleanup();
111 }
112
113 /**
114 * Cleans up any resources held by this recorder entry at the end
115 * of a subbuild if it has been created for the subbuild's project
116 * instance.
117 *
118 * @param event the buildFinished event
119 *
120 * @since Ant 1.6.2
121 */
122 public void subBuildFinished(BuildEvent event) {
123 if (event.getProject() == project) {
124 cleanup();
125 }
126 }
127
128 /**
129 * Empty implementation to satisfy the BuildListener interface.
130 *
131 * @param event the buildStarted event
132 *
133 * @since Ant 1.6.2
134 */
135 public void subBuildStarted(BuildEvent event) {
136 }
137
138
139 public void targetStarted(BuildEvent event) {
140 log(">> TARGET STARTED -- " + event.getTarget(), Project.MSG_DEBUG);
141 log(StringUtils.LINE_SEP + event.getTarget().getName() + ":",
142 Project.MSG_INFO);
143 targetStartTime = System.currentTimeMillis();
144 }
145
146
147 public void targetFinished(BuildEvent event) {
148 log("<< TARGET FINISHED -- " + event.getTarget(), Project.MSG_DEBUG);
149
150 String time = formatTime(System.currentTimeMillis() - targetStartTime);
151
152 log(event.getTarget() + ": duration " + time, Project.MSG_VERBOSE);
153 flush();
154 }
155
156
157 public void taskStarted(BuildEvent event) {
158 log(">>> TASK STARTED -- " + event.getTask(), Project.MSG_DEBUG);
159 }
160
161
162 public void taskFinished(BuildEvent event) {
163 log("<<< TASK FINISHED -- " + event.getTask(), Project.MSG_DEBUG);
164 flush();
165 }
166
167
168 public void messageLogged(BuildEvent event) {
169 log("--- MESSAGE LOGGED", Project.MSG_DEBUG);
170
171 StringBuffer buf = new StringBuffer();
172
173 if (event.getTask() != null) {
174 String name = event.getTask().getTaskName();
175
176 if (!emacsMode) {
177 String label = "[" + name + "] ";
178 int size = DefaultLogger.LEFT_COLUMN_SIZE - label.length();
179
180 for (int i = 0; i < size; i++) {
181 buf.append(" ");
182 }
183 buf.append(label);
184 }
185 }
186 buf.append(event.getMessage());
187
188 log(buf.toString(), event.getPriority());
189 }
190
191
192 /**
193 * The thing that actually sends the information to the output.
194 *
195 * @param mesg The message to log.
196 * @param level The verbosity level of the message.
197 */
198 private void log(String mesg, int level) {
199 if (record && (level <= loglevel) && out != null) {
200 out.println(mesg);
201 }
202 }
203
204 private void flush() {
205 if (record && out != null) {
206 out.flush();
207 }
208 }
209
210
211 public void setMessageOutputLevel(int level) {
212 if (level >= Project.MSG_ERR && level <= Project.MSG_DEBUG) {
213 loglevel = level;
214 }
215 }
216
217
218 public void setOutputPrintStream(PrintStream output) {
219 closeFile();
220 out = output;
221 }
222
223
224 public void setEmacsMode(boolean emacsMode) {
225 this.emacsMode = emacsMode;
226 }
227
228
229 public void setErrorPrintStream(PrintStream err) {
230 setOutputPrintStream(err);
231 }
232
233
234 private static String formatTime(long millis) {
235 long seconds = millis / 1000;
236 long minutes = seconds / 60;
237
238
239 if (minutes > 0) {
240 return Long.toString(minutes) + " minute"
241 + (minutes == 1 ? " " : "s ")
242 + Long.toString(seconds % 60) + " second"
243 + (seconds % 60 == 1 ? "" : "s");
244 } else {
245 return Long.toString(seconds) + " second"
246 + (seconds % 60 == 1 ? "" : "s");
247 }
248
249 }
250
251 /**
252 * Set the project associated with this recorder entry.
253 *
254 * @param project the project instance
255 *
256 * @since 1.6.2
257 */
258 public void setProject(Project project) {
259 this.project = project;
260 if (project != null) {
261 project.addBuildListener(this);
262 }
263 }
264
265 /**
266 * @since 1.6.2
267 */
268 public void cleanup() {
269 closeFile();
270 if (project != null) {
271 project.removeBuildListener(this);
272 }
273 project = null;
274 }
275
276 /**
277 * Initially opens the file associated with this recorder.
278 * Used by Recorder.
279 * @param append Indicates if output must be appended to the logfile or that
280 * the logfile should be overwritten.
281 * @throws BuildException
282 * @since 1.6.3
283 */
284 void openFile(boolean append) throws BuildException {
285 openFileImpl(append);
286 }
287
288 /**
289 * Closes the file associated with this recorder.
290 * Used by Recorder.
291 * @since 1.6.3
292 */
293 void closeFile() {
294 if (out != null) {
295 out.close();
296 out = null;
297 }
298 }
299
300 /**
301 * Re-opens the file associated with this recorder.
302 * Used by Recorder.
303 * @throws BuildException
304 * @since 1.6.3
305 */
306 void reopenFile() throws BuildException {
307 openFileImpl(true);
308 }
309
310 private void openFileImpl(boolean append) throws BuildException {
311 if (out == null) {
312 try {
313 out = new PrintStream(new FileOutputStream(filename, append));
314 } catch (IOException ioe) {
315 throw new BuildException("Problems opening file using a " +
316 "recorder entry", ioe);
317 }
318 }
319 }
320
321}
322
Note: See TracBrowser for help on using the repository browser.