KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > listener > CommonsLoggingListener


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.listener;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogConfigurationException;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.apache.tools.ant.BuildListener;
26 import org.apache.tools.ant.BuildLogger;
27 import org.apache.tools.ant.BuildEvent;
28 import org.apache.tools.ant.Project;
29 import org.apache.tools.ant.Task;
30 import org.apache.tools.ant.UnknownElement;
31
32 import java.io.PrintStream JavaDoc;
33
34 /**
35  * Jakarta Commons Logging listener.
36  * Note: do not use the SimpleLog as your logger implementation as it
37  * causes an infinite loop since it writes to System.err, which Ant traps
38  * and reroutes to the logger/listener layer.
39  *
40  * The following names are used for the log:
41  * org.apache.tools.ant.Project.PROJECT_NAME - for project events
42  * org.apache.tools.ant.Target.TARGET_NAME - for target events
43  * TASK_CLASS_NAME.TARGET_NAME - for events in individual targets.
44  *
45  * In all target and project names we replace "." and " " with "-".
46  *
47  * TODO: we should use the advanced context logging features (and expose them
48  * in c-l first :-)
49  * TODO: this is _very_ inefficient. Switching the out and tracking the logs
50  * can be optimized a lot - but may require few more changes to the core.
51  *
52  * @since Ant 1.5
53  */

54 public class CommonsLoggingListener implements BuildListener, BuildLogger {
55
56     /** Indicates if the listener was initialized. */
57     private boolean initialized = false;
58
59     private LogFactory logFactory;
60
61     /**
62      * name of the category under which target events are logged
63      */

64     public static final String JavaDoc TARGET_LOG = "org.apache.tools.ant.Target";
65     /**
66      * name of the category under which project events are logged
67      */

68     public static final String JavaDoc PROJECT_LOG = "org.apache.tools.ant.Project";
69
70     /**
71      * Construct the listener and make sure that a LogFactory
72      * can be obtained.
73      */

74     public CommonsLoggingListener() {
75     }
76
77     private Log getLog(String JavaDoc cat, String JavaDoc suffix) {
78         if (suffix != null) {
79             suffix = suffix.replace('.', '-');
80             suffix = suffix.replace(' ', '-');
81             cat = cat + "." + suffix;
82         }
83         PrintStream JavaDoc tmpOut = System.out;
84         PrintStream JavaDoc tmpErr = System.err;
85         System.setOut(out);
86         System.setErr(err);
87
88         if (!initialized) {
89             try {
90                 logFactory = LogFactory.getFactory();
91             } catch (LogConfigurationException e) {
92                 e.printStackTrace(System.err);
93                 return null;
94             }
95         }
96
97         initialized = true;
98         Log log = logFactory.getInstance(cat);
99         System.setOut(tmpOut);
100         System.setErr(tmpErr);
101         return log;
102     }
103
104     /** {@inheritDoc}. */
105     public void buildStarted(BuildEvent event) {
106         String JavaDoc categoryString = PROJECT_LOG;
107         Log log = getLog(categoryString, null);
108
109         if (initialized) {
110             realLog(log, "Build started.", Project.MSG_INFO, null);
111         }
112     }
113
114     /** {@inheritDoc}. */
115     public void buildFinished(BuildEvent event) {
116         if (initialized) {
117             String JavaDoc categoryString = PROJECT_LOG;
118             Log log = getLog(categoryString, event.getProject().getName());
119
120             if (event.getException() == null) {
121                 realLog(log, "Build finished.", Project.MSG_INFO, null);
122             } else {
123                 realLog(log, "Build finished with error.", Project.MSG_ERR,
124                         event.getException());
125             }
126         }
127     }
128
129     /**
130      * @see BuildListener#targetStarted
131      */

132     /** {@inheritDoc}. */
133     public void targetStarted(BuildEvent event) {
134         if (initialized) {
135             Log log = getLog(TARGET_LOG,
136                     event.getTarget().getName());
137             // Since task log category includes target, we don't really
138
// need this message
139
realLog(log, "Start: " + event.getTarget().getName(),
140                     Project.MSG_VERBOSE, null);
141         }
142     }
143
144     /**
145      * @see BuildListener#targetFinished
146      */

147     /** {@inheritDoc}. */
148     public void targetFinished(BuildEvent event) {
149         if (initialized) {
150             String JavaDoc targetName = event.getTarget().getName();
151             Log log = getLog(TARGET_LOG,
152                     event.getTarget().getName());
153             if (event.getException() == null) {
154                 realLog(log, "Target end: " + targetName, Project.MSG_DEBUG, null);
155             } else {
156                 realLog(log, "Target \"" + targetName
157                         + "\" finished with error.", Project.MSG_ERR,
158                         event.getException());
159             }
160         }
161     }
162
163     /**
164      * @see BuildListener#taskStarted
165      */

166     /** {@inheritDoc}. */
167     public void taskStarted(BuildEvent event) {
168         if (initialized) {
169             Task task = event.getTask();
170             Object JavaDoc real = task;
171             if (task instanceof UnknownElement) {
172                 Object JavaDoc realObj = ((UnknownElement) task).getTask();
173                 if (realObj != null) {
174                     real = realObj;
175                 }
176             }
177             Log log = getLog(real.getClass().getName(), null);
178             if (log.isTraceEnabled()) {
179                 realLog(log, "Task \"" + task.getTaskName() + "\" started ",
180                         Project.MSG_VERBOSE, null);
181             }
182         }
183     }
184
185     /**
186      * @see BuildListener#taskFinished
187      */

188     /** {@inheritDoc}. */
189     public void taskFinished(BuildEvent event) {
190         if (initialized) {
191             Task task = event.getTask();
192             Object JavaDoc real = task;
193             if (task instanceof UnknownElement) {
194                 Object JavaDoc realObj = ((UnknownElement) task).getTask();
195                 if (realObj != null) {
196                     real = realObj;
197                 }
198             }
199             Log log = getLog(real.getClass().getName(), null);
200             if (event.getException() == null) {
201                 if (log.isTraceEnabled()) {
202                     realLog(log, "Task \"" + task.getTaskName() + "\" finished.",
203                             Project.MSG_VERBOSE, null);
204                 }
205             } else {
206                 realLog(log, "Task \"" + task.getTaskName()
207                         + "\" finished with error.", Project.MSG_ERR,
208                         event.getException());
209             }
210         }
211     }
212
213
214     /**
215      * @see BuildListener#messageLogged
216      */

217     /** {@inheritDoc}. */
218     public void messageLogged(BuildEvent event) {
219         if (initialized) {
220             Object JavaDoc categoryObject = event.getTask();
221             String JavaDoc categoryString = null;
222             String JavaDoc categoryDetail = null;
223
224             if (categoryObject == null) {
225                 categoryObject = event.getTarget();
226                 if (categoryObject == null) {
227                     categoryObject = event.getProject();
228                     categoryString = PROJECT_LOG;
229                     categoryDetail = event.getProject().getName();
230                 } else {
231                     categoryString = TARGET_LOG;
232                     categoryDetail = event.getTarget().getName();
233                 }
234             } else {
235                 // It's a task - append the target
236
if (event.getTarget() != null) {
237                     categoryString = categoryObject.getClass().getName();
238                     categoryDetail = event.getTarget().getName();
239                 } else {
240                     categoryString = categoryObject.getClass().getName();
241                 }
242
243             }
244
245             Log log = getLog(categoryString, categoryDetail);
246             int priority = event.getPriority();
247             String JavaDoc message = event.getMessage();
248             realLog(log, message, priority , null);
249         }
250     }
251
252     private void realLog(Log log, String JavaDoc message, int priority, Throwable JavaDoc t) {
253         PrintStream JavaDoc tmpOut = System.out;
254         PrintStream JavaDoc tmpErr = System.err;
255         System.setOut(out);
256         System.setErr(err);
257         switch (priority) {
258             case Project.MSG_ERR:
259                 if (t == null) {
260                     log.error(message);
261                 } else {
262                     log.error(message, t);
263                 }
264                 break;
265             case Project.MSG_WARN:
266                 if (t == null) {
267                     log.warn(message);
268                 } else {
269                     log.warn(message, t);
270                 }
271                 break;
272             case Project.MSG_INFO:
273                 if (t == null) {
274                     log.info(message);
275                 } else {
276                     log.info(message, t);
277                 }
278                 break;
279             case Project.MSG_VERBOSE:
280                 log.debug(message);
281                 break;
282             case Project.MSG_DEBUG:
283                 log.debug(message);
284                 break;
285             default:
286                 log.error(message);
287                 break;
288         }
289         System.setOut(tmpOut);
290         System.setErr(tmpErr);
291     }
292
293     // CheckStyle:VisibilityModifier OFF - bc
294
PrintStream JavaDoc out = System.out;
295     PrintStream JavaDoc err = System.err;
296     // CheckStyle:VisibilityModifier ON
297

298     /**
299      * Set the the output level.
300      * This is not used, the logger config is used instead.
301      * @param level ignored
302      */

303     public void setMessageOutputLevel(int level) {
304         // Use the logger config
305
}
306
307     /**
308      * Set the output print stream.
309      * @param output the output stream
310      */

311     public void setOutputPrintStream(PrintStream JavaDoc output) {
312         this.out = output;
313     }
314
315     /**
316      * Set emacs mode.
317      * This is ignored.
318      * @param emacsMode ignored
319      */

320     public void setEmacsMode(boolean emacsMode) {
321         // Doesn't make sense for c-l. Use the logger config
322
}
323
324     /**
325      * Set the error print stream.
326      * @param err the error stream
327      */

328     public void setErrorPrintStream(PrintStream JavaDoc err) {
329         this.err = err;
330     }
331
332 }
333
Popular Tags