1 | /*
|
---|
2 | * Copyright 2002-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 |
|
---|
18 | package org.apache.tools.ant.listener;
|
---|
19 |
|
---|
20 | import org.apache.commons.logging.Log;
|
---|
21 | import org.apache.commons.logging.LogConfigurationException;
|
---|
22 | import org.apache.commons.logging.LogFactory;
|
---|
23 |
|
---|
24 | import org.apache.tools.ant.BuildListener;
|
---|
25 | import org.apache.tools.ant.BuildLogger;
|
---|
26 | import org.apache.tools.ant.BuildEvent;
|
---|
27 | import org.apache.tools.ant.Project;
|
---|
28 | import org.apache.tools.ant.Task;
|
---|
29 | import org.apache.tools.ant.UnknownElement;
|
---|
30 |
|
---|
31 | import java.io.PrintStream;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Jakarta Commons Logging listener.
|
---|
35 | * Note: do not use the SimpleLog as your logger implementation as it
|
---|
36 | * causes an infinite loop since it writes to System.err, which Ant traps
|
---|
37 | * and reroutes to the logger/listener layer.
|
---|
38 | *
|
---|
39 | * The following names are used for the log:
|
---|
40 | * org.apache.tools.ant.Project.PROJECT_NAME - for project events
|
---|
41 | * org.apache.tools.ant.Target.TARGET_NAME - for target events
|
---|
42 | * TASK_CLASS_NAME.TARGET_NAME - for events in individual targets.
|
---|
43 | *
|
---|
44 | * In all target and project names we replace "." and " " with "-".
|
---|
45 | *
|
---|
46 | * TODO: we should use the advanced context logging features (and expose them
|
---|
47 | * in c-l first :-)
|
---|
48 | * TODO: this is _very_ inefficient. Switching the out and tracking the logs
|
---|
49 | * can be optimized a lot - but may require few more changes to the core.
|
---|
50 | *
|
---|
51 | * @since Ant 1.5
|
---|
52 | */
|
---|
53 | public class CommonsLoggingListener implements BuildListener, BuildLogger {
|
---|
54 |
|
---|
55 | /** Indicates if the listener was initialized. */
|
---|
56 | private boolean initialized = false;
|
---|
57 |
|
---|
58 | private LogFactory logFactory;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Construct the listener and make sure that a LogFactory
|
---|
62 | * can be obtained.
|
---|
63 | */
|
---|
64 | public CommonsLoggingListener() {
|
---|
65 | }
|
---|
66 |
|
---|
67 | private Log getLog(String cat, String suffix) {
|
---|
68 | if (suffix != null) {
|
---|
69 | suffix = suffix.replace('.', '-');
|
---|
70 | suffix = suffix.replace(' ', '-');
|
---|
71 | cat = cat + "." + suffix;
|
---|
72 | }
|
---|
73 | PrintStream tmpOut = System.out;
|
---|
74 | PrintStream tmpErr = System.err;
|
---|
75 | System.setOut(out);
|
---|
76 | System.setErr(err);
|
---|
77 |
|
---|
78 | if (!initialized) {
|
---|
79 | try {
|
---|
80 | logFactory = LogFactory.getFactory();
|
---|
81 | } catch (LogConfigurationException e) {
|
---|
82 | e.printStackTrace(System.err);
|
---|
83 | return null;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | initialized = true;
|
---|
88 | Log log = logFactory.getInstance(cat);
|
---|
89 | System.setOut(tmpOut);
|
---|
90 | System.setErr(tmpErr);
|
---|
91 | return log;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * @see BuildListener#buildStarted
|
---|
96 | */
|
---|
97 | public void buildStarted(BuildEvent event) {
|
---|
98 | String categoryString = "org.apache.tools.ant.Project";
|
---|
99 | Log log = getLog(categoryString, null);
|
---|
100 |
|
---|
101 | if (initialized) {
|
---|
102 | realLog(log, "Build started.", Project.MSG_INFO, null);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * @see BuildListener#buildFinished
|
---|
108 | */
|
---|
109 | public void buildFinished(BuildEvent event) {
|
---|
110 | if (initialized) {
|
---|
111 | String categoryString = "org.apache.tools.ant.Project";
|
---|
112 | Log log = getLog(categoryString, event.getProject().getName());
|
---|
113 |
|
---|
114 | if (event.getException() == null) {
|
---|
115 | realLog(log, "Build finished.", Project.MSG_INFO, null);
|
---|
116 | } else {
|
---|
117 | realLog(log, "Build finished with error.", Project.MSG_ERR,
|
---|
118 | event.getException());
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * @see BuildListener#targetStarted
|
---|
125 | */
|
---|
126 | public void targetStarted(BuildEvent event) {
|
---|
127 | if (initialized) {
|
---|
128 | Log log = getLog("org.apache.tools.ant.Target",
|
---|
129 | event.getTarget().getName());
|
---|
130 | // Since task log category includes target, we don't really
|
---|
131 | // need this message
|
---|
132 | realLog(log, "Start: " + event.getTarget().getName(),
|
---|
133 | Project.MSG_DEBUG, null);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @see BuildListener#targetFinished
|
---|
139 | */
|
---|
140 | public void targetFinished(BuildEvent event) {
|
---|
141 | if (initialized) {
|
---|
142 | String targetName = event.getTarget().getName();
|
---|
143 | Log log = getLog("org.apache.tools.ant.Target",
|
---|
144 | event.getTarget().getName());
|
---|
145 | if (event.getException() == null) {
|
---|
146 | realLog(log, "Target end: " + targetName, Project.MSG_DEBUG, null);
|
---|
147 | } else {
|
---|
148 | realLog(log, "Target \"" + targetName
|
---|
149 | + "\" finished with error.", Project.MSG_ERR,
|
---|
150 | event.getException());
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @see BuildListener#taskStarted
|
---|
157 | */
|
---|
158 | public void taskStarted(BuildEvent event) {
|
---|
159 | if (initialized) {
|
---|
160 | Task task = event.getTask();
|
---|
161 | Object real = task;
|
---|
162 | if (task instanceof UnknownElement) {
|
---|
163 | Object realObj = ((UnknownElement) task).getTask();
|
---|
164 | if (realObj != null) {
|
---|
165 | real = realObj;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | Log log = getLog(real.getClass().getName(), null);
|
---|
169 | if (log.isTraceEnabled()) {
|
---|
170 | realLog(log, "Task \"" + task.getTaskName() + "\" started ",
|
---|
171 | Project.MSG_VERBOSE, null);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * @see BuildListener#taskFinished
|
---|
178 | */
|
---|
179 | public void taskFinished(BuildEvent event) {
|
---|
180 | if (initialized) {
|
---|
181 | Task task = event.getTask();
|
---|
182 | Object real = task;
|
---|
183 | if (task instanceof UnknownElement) {
|
---|
184 | Object realObj = ((UnknownElement) task).getTask();
|
---|
185 | if (realObj != null) {
|
---|
186 | real = realObj;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | Log log = getLog(real.getClass().getName(), null);
|
---|
190 | if (event.getException() == null) {
|
---|
191 | if (log.isTraceEnabled()) {
|
---|
192 | realLog(log, "Task \"" + task.getTaskName() + "\" finished.",
|
---|
193 | Project.MSG_VERBOSE, null);
|
---|
194 | }
|
---|
195 | } else {
|
---|
196 | realLog(log, "Task \"" + task.getTaskName()
|
---|
197 | + "\" finished with error.", Project.MSG_ERR,
|
---|
198 | event.getException());
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * @see BuildListener#messageLogged
|
---|
206 | */
|
---|
207 | public void messageLogged(BuildEvent event) {
|
---|
208 | if (initialized) {
|
---|
209 | Object categoryObject = event.getTask();
|
---|
210 | String categoryString = null;
|
---|
211 | String categoryDetail = null;
|
---|
212 |
|
---|
213 | if (categoryObject == null) {
|
---|
214 | categoryObject = event.getTarget();
|
---|
215 | if (categoryObject == null) {
|
---|
216 | categoryObject = event.getProject();
|
---|
217 | categoryString = "org.apache.tools.ant.Project";
|
---|
218 | categoryDetail = event.getProject().getName();
|
---|
219 | } else {
|
---|
220 | categoryString = "org.apache.tools.ant.Target";
|
---|
221 | categoryDetail = event.getTarget().getName();
|
---|
222 | }
|
---|
223 | } else {
|
---|
224 | // It's a task - append the target
|
---|
225 | if (event.getTarget() != null) {
|
---|
226 | categoryString = categoryObject.getClass().getName();
|
---|
227 | categoryDetail = event.getTarget().getName();
|
---|
228 | } else {
|
---|
229 | categoryString = categoryObject.getClass().getName();
|
---|
230 | }
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | Log log = getLog(categoryString, categoryDetail);
|
---|
235 | int priority = event.getPriority();
|
---|
236 | String message = event.getMessage();
|
---|
237 | realLog(log, message, priority , null);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void realLog(Log log, String message, int priority, Throwable t) {
|
---|
242 | PrintStream tmpOut = System.out;
|
---|
243 | PrintStream tmpErr = System.err;
|
---|
244 | System.setOut(out);
|
---|
245 | System.setErr(err);
|
---|
246 | switch (priority) {
|
---|
247 | case Project.MSG_ERR:
|
---|
248 | if (t == null) {
|
---|
249 | log.error(message);
|
---|
250 | } else {
|
---|
251 | log.error(message, t);
|
---|
252 | }
|
---|
253 | break;
|
---|
254 | case Project.MSG_WARN:
|
---|
255 | if (t == null) {
|
---|
256 | log.warn(message);
|
---|
257 | } else {
|
---|
258 | log.warn(message, t);
|
---|
259 | }
|
---|
260 | break;
|
---|
261 | case Project.MSG_INFO:
|
---|
262 | if (t == null) {
|
---|
263 | log.info(message);
|
---|
264 | } else {
|
---|
265 | log.info(message, t);
|
---|
266 | }
|
---|
267 | break;
|
---|
268 | case Project.MSG_VERBOSE:
|
---|
269 | log.debug(message);
|
---|
270 | break;
|
---|
271 | case Project.MSG_DEBUG:
|
---|
272 | log.debug(message);
|
---|
273 | break;
|
---|
274 | default:
|
---|
275 | log.error(message);
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | System.setOut(tmpOut);
|
---|
279 | System.setErr(tmpErr);
|
---|
280 | }
|
---|
281 |
|
---|
282 | PrintStream out = System.out;
|
---|
283 | PrintStream err = System.err;
|
---|
284 |
|
---|
285 | public void setMessageOutputLevel(int level) {
|
---|
286 | // Use the logger config
|
---|
287 | }
|
---|
288 |
|
---|
289 | public void setOutputPrintStream(PrintStream output) {
|
---|
290 | this.out = output;
|
---|
291 | }
|
---|
292 |
|
---|
293 | public void setEmacsMode(boolean emacsMode) {
|
---|
294 | // Doesn't make sense for c-l. Use the logger config
|
---|
295 | }
|
---|
296 |
|
---|
297 | public void setErrorPrintStream(PrintStream err) {
|
---|
298 | this.err = err;
|
---|
299 | }
|
---|
300 |
|
---|
301 | }
|
---|