587
|
1 |
/*
|
|
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 |
*/
|
|
17 |
|
|
18 |
package com.nokia.ant;
|
|
19 |
|
|
20 |
import java.io.File;
|
|
21 |
import java.io.PrintStream;
|
|
22 |
import java.text.SimpleDateFormat;
|
|
23 |
import java.util.Calendar;
|
|
24 |
|
|
25 |
import org.apache.log4j.Logger;
|
|
26 |
import org.apache.tools.ant.BuildEvent;
|
|
27 |
import org.apache.tools.ant.DefaultLogger;
|
|
28 |
import org.apache.tools.ant.Project;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Logger class that can connect to Ant and log information regarding to build
|
|
32 |
* times, number of errors and such. Data is sent to Diamonds server, where it
|
|
33 |
* is processed further.
|
|
34 |
*
|
|
35 |
* This class is listening all build related events. It catches the build
|
|
36 |
* start-finish, target start-finish events of Ant and gather build start-end
|
|
37 |
* time, errors/warnings and store in BuildData class. Stored data will be
|
|
38 |
* exported to XML and uploaded to Diamonds server after each specific target.
|
|
39 |
* For example after target "create-bom" this class will upload all BOM data to
|
|
40 |
* Diamonds.
|
|
41 |
*
|
|
42 |
*
|
|
43 |
*/
|
|
44 |
public class HeliumLogger extends DefaultLogger {
|
|
45 |
|
|
46 |
private static boolean stopLogToConsole;
|
|
47 |
|
|
48 |
private Project project;
|
|
49 |
private Logger log = Logger.getLogger(this.getClass());
|
|
50 |
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Ant call this function when build start.
|
|
54 |
*/
|
|
55 |
public void buildStarted(BuildEvent event) {
|
|
56 |
project = event.getProject();
|
|
57 |
super.buildStarted(event);
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Triggered when a target starts.
|
|
62 |
*/
|
|
63 |
public void targetStarted(BuildEvent event) {
|
|
64 |
/** The "if" condition to test on execution. */
|
|
65 |
String ifCondition = "";
|
|
66 |
/** The "unless" condition to test on execution. */
|
|
67 |
String unlessCondition = "";
|
|
68 |
String targetName = event.getTarget().getName();
|
|
69 |
logTargetEvent(targetName, "start");
|
|
70 |
|
|
71 |
/**get the values needed from the event **/
|
|
72 |
ifCondition = event.getTarget().getIf();
|
|
73 |
unlessCondition = event.getTarget().getUnless();
|
|
74 |
project = event.getProject();
|
|
75 |
|
|
76 |
super.targetStarted(event);
|
|
77 |
|
|
78 |
/**if the target is not going to execute (due to 'if' or 'unless' conditions)
|
|
79 |
print a message telling the user why it is not going to execute**/
|
|
80 |
if (!testIfCondition(ifCondition) && ifCondition != null) {
|
|
81 |
project.log("Skipped because property '"
|
|
82 |
+ project.replaceProperties(ifCondition)
|
|
83 |
+ "' not set.", Project.MSG_INFO);
|
|
84 |
} else if (!testUnlessCondition(unlessCondition) && unlessCondition != null) {
|
|
85 |
project.log("Skipped because property '"
|
|
86 |
+ project.replaceProperties(unlessCondition)
|
|
87 |
+ "' set.", Project.MSG_INFO);
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Log the start or end of the build as a event.
|
|
93 |
*
|
|
94 |
* @param targetName
|
|
95 |
* The name of the current target.
|
|
96 |
* @param event
|
|
97 |
* A string description of the event.
|
|
98 |
*/
|
|
99 |
private void logTargetEvent(String targetName, String event) {
|
|
100 |
String logTargetProperty = project.getProperty("log.target");
|
|
101 |
if ((logTargetProperty != null) && (logTargetProperty.equals("yes"))) {
|
|
102 |
log.info("Target #### " + targetName + " ####: " + event);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Triggered when a target finishes.
|
|
108 |
*/
|
|
109 |
public void targetFinished(BuildEvent event) {
|
|
110 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
111 |
String time = sdf.format(Calendar.getInstance().getTime());
|
|
112 |
|
|
113 |
String targetName = time + "," + event.getTarget().getName();
|
|
114 |
|
|
115 |
logTargetEvent(targetName, "finish");
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Triggered when the build finishes.
|
|
120 |
*/
|
|
121 |
public void buildFinished(BuildEvent event) {
|
|
122 |
// re-enabling output of messages at the end of the build
|
|
123 |
stopLogToConsole = false;
|
|
124 |
cleanup();
|
|
125 |
super.buildFinished(event);
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* See if build needs a final cleanup target to be called.
|
|
130 |
*/
|
|
131 |
private void cleanup() {
|
|
132 |
String loggingoutputfile = project.getProperty("logging.output.file");
|
|
133 |
if (loggingoutputfile != null) {
|
|
134 |
File f = new File(loggingoutputfile);
|
|
135 |
if (f.exists()) {
|
|
136 |
f.delete();
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
if ((project.getProperty("call.cleanup") != null)
|
|
141 |
&& (project.getProperty("call.cleanup").equals("yes"))) {
|
|
142 |
project.executeTarget("cleanup-all");
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Get log to console status
|
|
148 |
*/
|
|
149 |
public static boolean getStopLogToConsole() {
|
|
150 |
return stopLogToConsole;
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Set log to console status
|
|
155 |
*/
|
|
156 |
public static void setStopLogToConsole(boolean stop) {
|
|
157 |
stopLogToConsole = stop;
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* {@inheritDoc}
|
|
162 |
*/
|
|
163 |
protected void printMessage(final String message, final PrintStream stream,
|
|
164 |
final int priority) {
|
|
165 |
if (!stopLogToConsole) {
|
|
166 |
stream.println(message);
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
/**
|
|
171 |
* Tests whether or not the "if" condition is satisfied.
|
|
172 |
*
|
|
173 |
* @return whether or not the "if" condition is satisfied. If no
|
|
174 |
* condition (or an empty condition) has been set,
|
|
175 |
* <code>true</code> is returned.
|
|
176 |
*/
|
|
177 |
private boolean testIfCondition(String ifCondition) {
|
|
178 |
if ("".equals(ifCondition)) {
|
|
179 |
return true;
|
|
180 |
}
|
|
181 |
|
|
182 |
String test = project.replaceProperties(ifCondition);
|
|
183 |
return project.getProperty(test) != null;
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Tests whether or not the "unless" condition is satisfied.
|
|
188 |
*
|
|
189 |
* @return whether or not the "unless" condition is satisfied. If no
|
|
190 |
* condition (or an empty condition) has been set,
|
|
191 |
* <code>true</code> is returned.
|
|
192 |
*/
|
|
193 |
private boolean testUnlessCondition(String unlessCondition) {
|
|
194 |
if ("".equals(unlessCondition)) {
|
|
195 |
return true;
|
|
196 |
}
|
|
197 |
String test = project.replaceProperties(unlessCondition);
|
|
198 |
return project.getProperty(test) == null;
|
|
199 |
}
|
|
200 |
}
|