587
|
1 |
/*
|
628
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
587
|
17 |
package com.nokia.helium.logger.ant.listener;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.FileOutputStream;
|
|
21 |
import java.io.IOException;
|
628
|
22 |
import java.io.PrintStream;
|
587
|
23 |
import java.util.Vector;
|
|
24 |
import java.util.regex.Matcher;
|
|
25 |
import java.util.regex.Pattern;
|
|
26 |
|
628
|
27 |
import org.apache.log4j.Logger;
|
|
28 |
import org.apache.tools.ant.BuildEvent;
|
|
29 |
import org.apache.tools.ant.BuildException;
|
|
30 |
import org.apache.tools.ant.DefaultLogger;
|
|
31 |
import org.apache.tools.ant.Project;
|
|
32 |
import org.apache.tools.ant.util.StringUtils;
|
|
33 |
|
587
|
34 |
/**
|
628
|
35 |
* This is a class that represents a recorder. This is the listener to the build process.
|
|
36 |
*
|
587
|
37 |
* @since Ant 1.4
|
|
38 |
*/
|
628
|
39 |
public class RecorderEntry implements BuildEventHandler, TargetEventHandler, TaskEventHandler,
|
|
40 |
MessageEventHandler {
|
587
|
41 |
|
628
|
42 |
/** The name of the file associated with this recorder entry. */
|
587
|
43 |
private File filename;
|
628
|
44 |
/** The state of the recorder (recorder on or off). */
|
587
|
45 |
private boolean record = true;
|
628
|
46 |
/** The current verbosity level to record at. */
|
587
|
47 |
private int loglevel = Project.MSG_INFO;
|
628
|
48 |
/** The output PrintStream to record to. */
|
587
|
49 |
private PrintStream out;
|
628
|
50 |
/** The start time of the last know target. */
|
587
|
51 |
private long targetStartTime;
|
628
|
52 |
/** Strip task banners if true. */
|
587
|
53 |
private boolean emacsMode;
|
628
|
54 |
|
587
|
55 |
private Pattern pattern;
|
628
|
56 |
|
587
|
57 |
private Vector<String> logRegExps = new Vector<String>();
|
|
58 |
private Logger log = Logger.getLogger(getClass());
|
|
59 |
|
|
60 |
/**
|
|
61 |
* @param name The name of this recorder (used as the filename).
|
|
62 |
*/
|
|
63 |
public RecorderEntry(File name) {
|
|
64 |
targetStartTime = System.currentTimeMillis();
|
|
65 |
filename = name;
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* @return the name of the file the output is sent to.
|
|
70 |
*/
|
|
71 |
public File getFilename() {
|
|
72 |
return filename;
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Turns off or on this recorder.
|
628
|
77 |
*
|
587
|
78 |
* @param state true for on, false for off, null for no change.
|
|
79 |
*/
|
|
80 |
public void setRecordState(boolean state) {
|
628
|
81 |
flush();
|
|
82 |
record = state;
|
587
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Get the current state of the recorder
|
628
|
87 |
*
|
587
|
88 |
* @param state
|
|
89 |
*/
|
|
90 |
public boolean getRecordState() {
|
|
91 |
return record;
|
|
92 |
}
|
628
|
93 |
|
587
|
94 |
/**
|
|
95 |
* To set the regexp to filter the logging.
|
628
|
96 |
*
|
587
|
97 |
* @param regexp
|
|
98 |
*/
|
|
99 |
public void addRegexp(String regexp) {
|
|
100 |
logRegExps.add(regexp);
|
|
101 |
}
|
628
|
102 |
|
587
|
103 |
/**
|
|
104 |
* To clear all regexp set.
|
|
105 |
*/
|
628
|
106 |
public void resetRegExp() {
|
587
|
107 |
logRegExps.clear();
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* @see org.apache.tools.ant.BuildListener#buildStarted(BuildEvent)
|
|
112 |
*/
|
|
113 |
/** {@inheritDoc}. */
|
|
114 |
public void handleBuildStarted(BuildEvent event) {
|
|
115 |
log("> BUILD STARTED", Project.MSG_DEBUG);
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* @see org.apache.tools.ant.BuildListener#buildFinished(BuildEvent)
|
|
120 |
*/
|
|
121 |
/** {@inheritDoc}. */
|
|
122 |
public void handleBuildFinished(BuildEvent event) {
|
|
123 |
log.debug("< BUILD FINISHED");
|
|
124 |
|
|
125 |
if (record && out != null) {
|
|
126 |
Throwable error = event.getException();
|
|
127 |
|
|
128 |
if (error == null) {
|
|
129 |
out.println(StringUtils.LINE_SEP + "BUILD SUCCESSFUL");
|
628
|
130 |
}
|
|
131 |
else {
|
|
132 |
out.println(StringUtils.LINE_SEP + "BUILD FAILED" + StringUtils.LINE_SEP);
|
587
|
133 |
error.printStackTrace(out);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
cleanup();
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
628
|
140 |
* Cleans up any resources held by this recorder entry at the end of a subbuild if it has been
|
|
141 |
* created for the subbuild's project instance.
|
|
142 |
*
|
587
|
143 |
* @param event the buildFinished event
|
628
|
144 |
*
|
587
|
145 |
* @since Ant 1.6.2
|
|
146 |
*/
|
|
147 |
public void handleSubBuildFinished(BuildEvent event) {
|
|
148 |
log("< SUBBUILD FINISHED", Project.MSG_DEBUG);
|
|
149 |
// let's keep the logging ongoing, even if sub-build finishes.
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Empty implementation to satisfy the BuildListener interface.
|
628
|
154 |
*
|
587
|
155 |
* @param event the buildStarted event
|
628
|
156 |
*
|
587
|
157 |
* @since Ant 1.6.2
|
|
158 |
*/
|
|
159 |
public void handleSubBuildStarted(BuildEvent event) {
|
|
160 |
log("< SUBBUILD STARTED", Project.MSG_DEBUG);
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* {@inheritDoc}
|
|
165 |
*/
|
|
166 |
public void handleTargetStarted(BuildEvent event) {
|
|
167 |
log(">> TARGET STARTED -- " + event.getTarget(), Project.MSG_DEBUG);
|
628
|
168 |
log(StringUtils.LINE_SEP + event.getTarget().getName() + ":", Project.MSG_INFO);
|
587
|
169 |
targetStartTime = System.currentTimeMillis();
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* {@inheritDoc}
|
|
174 |
*/
|
|
175 |
public void handleTargetFinished(BuildEvent event) {
|
|
176 |
log("<< TARGET FINISHED -- " + event.getTarget(), Project.MSG_DEBUG);
|
|
177 |
|
|
178 |
String time = formatTime(System.currentTimeMillis() - targetStartTime);
|
|
179 |
|
|
180 |
log(event.getTarget() + ": duration " + time, Project.MSG_VERBOSE);
|
|
181 |
flush();
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* @see org.apache.tools.ant.BuildListener#taskStarted(BuildEvent)
|
|
186 |
*/
|
|
187 |
/** {@inheritDoc}. */
|
|
188 |
public void handleTaskStarted(BuildEvent event) {
|
|
189 |
log(">>> TASK STARTED -- " + event.getTask(), Project.MSG_DEBUG);
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* @see org.apache.tools.ant.BuildListener#taskFinished(BuildEvent)
|
|
194 |
*/
|
|
195 |
/** {@inheritDoc}. */
|
|
196 |
public void handleTaskFinished(BuildEvent event) {
|
|
197 |
log("<<< TASK FINISHED -- " + event.getTask(), Project.MSG_DEBUG);
|
|
198 |
flush();
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* @see org.apache.tools.ant.BuildListener#messageLogged(BuildEvent)
|
|
203 |
*/
|
|
204 |
/** {@inheritDoc}. */
|
|
205 |
public void handleMessageLogged(BuildEvent event) {
|
|
206 |
log("--- MESSAGE LOGGED", Project.MSG_DEBUG);
|
|
207 |
|
|
208 |
StringBuffer buf = new StringBuffer();
|
|
209 |
|
|
210 |
if (event.getTask() != null) {
|
|
211 |
String name = event.getTask().getTaskName();
|
|
212 |
|
|
213 |
if (!emacsMode) {
|
|
214 |
String label = "[" + name + "] ";
|
|
215 |
int size = DefaultLogger.LEFT_COLUMN_SIZE - label.length();
|
|
216 |
|
|
217 |
for (int i = 0; i < size; i++) {
|
|
218 |
buf.append(" ");
|
|
219 |
}
|
|
220 |
buf.append(label);
|
|
221 |
}
|
|
222 |
}
|
|
223 |
String messgeToUpdate = filterMessage(event.getMessage());
|
|
224 |
buf.append(messgeToUpdate);
|
|
225 |
log(buf.toString(), event.getPriority());
|
|
226 |
}
|
628
|
227 |
|
587
|
228 |
/**
|
|
229 |
* To replace regExp matching with ****.
|
628
|
230 |
*
|
587
|
231 |
* @param message
|
|
232 |
* @return
|
|
233 |
*/
|
|
234 |
private String filterMessage(String message) {
|
|
235 |
for (String regExp : logRegExps) {
|
|
236 |
pattern = Pattern.compile(regExp);
|
|
237 |
if (pattern != null) {
|
|
238 |
Matcher match = pattern.matcher(message);
|
|
239 |
message = match.replaceAll("********");
|
|
240 |
}
|
|
241 |
}
|
|
242 |
return message;
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* The thing that actually sends the information to the output.
|
628
|
247 |
*
|
587
|
248 |
* @param mesg The message to log.
|
|
249 |
* @param level The verbosity level of the message.
|
|
250 |
*/
|
|
251 |
private void log(String msg, int level) {
|
|
252 |
if (record && (level <= loglevel) && out != null) {
|
|
253 |
out.println(msg);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
private void flush() {
|
|
258 |
if (record && out != null) {
|
|
259 |
out.flush();
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
|
264 |
* @see BuildLogger#setMessageOutputLevel(int)
|
|
265 |
*/
|
|
266 |
/** {@inheritDoc}. */
|
|
267 |
public void setMessageOutputLevel(int level) {
|
|
268 |
if (level >= Project.MSG_ERR && level <= Project.MSG_DEBUG) {
|
|
269 |
loglevel = level;
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* @see BuildLogger#setOutputPrintStream(PrintStream)
|
|
275 |
*/
|
|
276 |
/** {@inheritDoc}. */
|
|
277 |
public void setOutputPrintStream(PrintStream output) {
|
|
278 |
closeFile();
|
|
279 |
out = output;
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* @see BuildLogger#setEmacsMode(boolean)
|
|
284 |
*/
|
|
285 |
/** {@inheritDoc}. */
|
|
286 |
public void setEmacsMode(boolean emacsMode) {
|
|
287 |
this.emacsMode = emacsMode;
|
|
288 |
}
|
|
289 |
|
|
290 |
/**
|
|
291 |
* @see BuildLogger#setErrorPrintStream(PrintStream)
|
|
292 |
*/
|
|
293 |
/** {@inheritDoc}. */
|
|
294 |
public void setErrorPrintStream(PrintStream err) {
|
|
295 |
setOutputPrintStream(err);
|
|
296 |
}
|
|
297 |
|
|
298 |
private static String formatTime(long millis) {
|
|
299 |
// CheckStyle:MagicNumber OFF
|
|
300 |
long seconds = millis / 1000;
|
|
301 |
long minutes = seconds / 60;
|
|
302 |
|
|
303 |
if (minutes > 0) {
|
628
|
304 |
return Long.toString(minutes) + " minute" + (minutes == 1 ? " " : "s ")
|
|
305 |
+ Long.toString(seconds % 60) + " second" + (seconds % 60 == 1 ? "" : "s");
|
|
306 |
}
|
|
307 |
else {
|
|
308 |
return Long.toString(seconds) + " second" + (seconds % 60 == 1 ? "" : "s");
|
587
|
309 |
}
|
|
310 |
// CheckStyle:MagicNumber ON
|
|
311 |
}
|
628
|
312 |
|
587
|
313 |
/**
|
|
314 |
* Registering ourselves to the StatusAndLogListener.
|
|
315 |
*/
|
|
316 |
public void register() {
|
588
|
317 |
StatusAndLogListener listener = StatusAndLogListener.getStatusAndLogListener();
|
|
318 |
if (listener != null) {
|
587
|
319 |
this.log.debug("register");
|
588
|
320 |
synchronized (listener) {
|
628
|
321 |
listener.register((BuildEventHandler) this);
|
|
322 |
listener.register((TargetEventHandler) this);
|
|
323 |
listener.register((TaskEventHandler) this);
|
|
324 |
listener.register((MessageEventHandler) this);
|
587
|
325 |
}
|
|
326 |
}
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
|
330 |
* Unregistering ourselves from the StatusAndLogListener.
|
|
331 |
*/
|
|
332 |
public void unregister() {
|
588
|
333 |
StatusAndLogListener listener = StatusAndLogListener.getStatusAndLogListener();
|
|
334 |
if (listener != null) {
|
587
|
335 |
this.log.debug("unregister");
|
588
|
336 |
synchronized (listener) {
|
628
|
337 |
listener.remove((MessageEventHandler) this);
|
|
338 |
listener.remove((TaskEventHandler) this);
|
|
339 |
listener.remove((TargetEventHandler) this);
|
|
340 |
listener.remove((BuildEventHandler) this);
|
587
|
341 |
}
|
|
342 |
}
|
|
343 |
}
|
|
344 |
|
|
345 |
/**
|
|
346 |
* @since 1.6.2
|
|
347 |
*/
|
|
348 |
public void cleanup() {
|
|
349 |
closeFile();
|
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
628
|
353 |
* Closes the file associated with this recorder. Used by Recorder.
|
|
354 |
*
|
587
|
355 |
* @since 1.6.3
|
|
356 |
*/
|
|
357 |
public void closeFile() {
|
628
|
358 |
this.log.debug("closeFile.");
|
587
|
359 |
if (out != null) {
|
|
360 |
out.close();
|
|
361 |
out = null;
|
|
362 |
unregister();
|
|
363 |
}
|
|
364 |
}
|
628
|
365 |
|
587
|
366 |
/**
|
628
|
367 |
* Initially opens the file associated with this recorder. Used by Recorder.
|
|
368 |
*
|
|
369 |
* @param append Indicates if output must be appended to the logfile or that the logfile should
|
|
370 |
* be overwritten.
|
587
|
371 |
* @throws BuildException
|
|
372 |
* @since 1.6.3
|
|
373 |
*/
|
|
374 |
public void openFile(boolean append) {
|
|
375 |
openFileImpl(append);
|
|
376 |
}
|
628
|
377 |
|
587
|
378 |
/**
|
628
|
379 |
* Re-opens the file associated with this recorder. Used by Recorder.
|
|
380 |
*
|
587
|
381 |
* @throws BuildException
|
|
382 |
* @since 1.6.3
|
|
383 |
*/
|
|
384 |
public void reopenFile() {
|
|
385 |
openFileImpl(true);
|
|
386 |
}
|
628
|
387 |
|
587
|
388 |
private void openFileImpl(boolean append) {
|
|
389 |
if (out == null) {
|
|
390 |
this.log.debug("openFileImpl: " + filename);
|
|
391 |
try {
|
|
392 |
out = new PrintStream(new FileOutputStream(filename, append));
|
|
393 |
register();
|
628
|
394 |
}
|
|
395 |
catch (IOException ioe) {
|
|
396 |
throw new BuildException("Problems opening file using a " + "recorder entry: "
|
|
397 |
+ ioe.getMessage(), ioe);
|
587
|
398 |
}
|
|
399 |
}
|
|
400 |
}
|
628
|
401 |
|
587
|
402 |
/**
|
|
403 |
* To add user message into log file.
|
628
|
404 |
*
|
587
|
405 |
* @param message
|
|
406 |
*/
|
|
407 |
public void addLogMessage(String message) {
|
|
408 |
out.println(StringUtils.LINE_SEP + message);
|
628
|
409 |
|
587
|
410 |
}
|
628
|
411 |
|
587
|
412 |
}
|