KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > ant > AntLog


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.ant;
10
11 import org.apache.commons.logging.Log;
12
13 import org.apache.tools.ant.Project;
14
15 /**
16  * The Ant wrapper class for Logger.
17  *
18  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
19  * @version CVS $Id: AntLog.java,v 1.3 2004/01/09 10:48:06 benedikta Exp $
20  */

21 public final class AntLog implements Log
22 {
23   // underlying implementation to delegate to
24
private final Project project;
25   private boolean use_level = false;
26   private int antMsgLevel = Project.MSG_ERR;
27
28   /**
29    * Create a logger that delegates to specified logger.
30    *
31    * @param project the Ant logger to delegate to
32    */

33   public AntLog(Project project)
34   {
35     this.project = project;
36   }
37
38   /**
39    * Create a logger that delegates to specified logger.
40    *
41    * @param project the Ant logger to delegate to
42    * @param antMsgLevel the massage level from ant
43    */

44   public AntLog(Project project, int antMsgLevel)
45   {
46     this.project = project;
47     this.antMsgLevel = antMsgLevel;
48     use_level = true;
49   }
50
51   /**
52    * <p>
53    * Log a message with trace log level.
54    * </p>
55    *
56    * @param message log this message
57    */

58   public void trace(Object JavaDoc message)
59   {
60     if ((use_level) && (antMsgLevel==Project.MSG_DEBUG))
61       project.log(message.toString());
62
63     project.log(message.toString(), Project.MSG_DEBUG);
64   }
65
66   /**
67    * <p>
68    * Log an error with trace log level.
69    * </p>
70    *
71    * @param message log this message
72    * @param t log this cause
73    */

74   public void trace(Object JavaDoc message, Throwable JavaDoc t)
75   {
76     if ((use_level) && (antMsgLevel==Project.MSG_DEBUG))
77       project.log(message+":"+toString(t));
78
79     project.log(message+":"+toString(t), Project.MSG_DEBUG);
80   }
81
82   /**
83    * <p>
84    * Is trace logging currently enabled?
85    * </p>
86    *
87    * <p>
88    * Call this method to prevent having to perform expensive operations (for example,
89    * <code>String</code> concatination) when the log level is more than trace.
90    * </p>
91    */

92   public boolean isTraceEnabled()
93   {
94     if (use_level)
95       return antMsgLevel==Project.MSG_DEBUG;
96
97     return true;
98   }
99
100   /**
101    * <p>
102    * Log a message with debug log level.
103    * </p>
104    *
105    * @param message log this message
106    */

107   public void debug(Object JavaDoc message)
108   {
109     if ((use_level) && (antMsgLevel==Project.MSG_DEBUG))
110       project.log(message.toString());
111
112     project.log(message.toString(), Project.MSG_DEBUG);
113   }
114
115   /**
116    * <p>
117    * Log an error with debug log level.
118    * </p>
119    *
120    * @param message log this message
121    * @param t log this cause
122    */

123   public void debug(Object JavaDoc message, Throwable JavaDoc t)
124   {
125     if ((use_level) && (antMsgLevel==Project.MSG_DEBUG))
126       project.log(message+":"+toString(t));
127
128     project.log(message+":"+toString(t), Project.MSG_DEBUG);
129   }
130
131   /**
132    * <p>
133    * Is debug logging currently enabled?
134    * </p>
135    *
136    * <p>
137    * Call this method to prevent having to perform expensive operations (for example,
138    * <code>String</code> concatination) when the log level is more than debug.
139    * </p>
140    */

141   public boolean isDebugEnabled()
142   {
143     if (use_level)
144       return antMsgLevel==Project.MSG_DEBUG;
145
146     return true;
147   }
148
149   /**
150    * <p>
151    * Log a message with info log level.
152    * </p>
153    *
154    * @param message log this message
155    */

156   public void info(Object JavaDoc message)
157   {
158     if ((use_level) && (antMsgLevel==Project.MSG_INFO))
159       project.log(message.toString());
160
161     project.log(message.toString(), Project.MSG_INFO);
162   }
163
164   /**
165    * <p>
166    * Log an error with info log level.
167    * </p>
168    *
169    * @param message log this message
170    * @param t log this cause
171    */

172   public void info(Object JavaDoc message, Throwable JavaDoc t)
173   {
174     if ((use_level) && (antMsgLevel==Project.MSG_INFO))
175       project.log(message+":"+toString(t));
176
177     project.log(message+":"+toString(t), Project.MSG_INFO);
178   }
179
180   /**
181    * <p>
182    * Is info logging currently enabled?
183    * </p>
184    *
185    * <p>
186    * Call this method to prevent having to perform expensive operations (for example,
187    * <code>String</code> concatination) when the log level is more than info.
188    * </p>
189    */

190   public boolean isInfoEnabled()
191   {
192     if (use_level)
193       return antMsgLevel==Project.MSG_INFO;
194
195     return true;
196   }
197
198   /**
199    * <p>
200    * Log a message with warn log level.
201    * </p>
202    *
203    * @param message log this message
204    */

205   public void warn(Object JavaDoc message)
206   {
207     if ((use_level) && (antMsgLevel==Project.MSG_WARN))
208       project.log(message.toString());
209
210     project.log(message.toString(), Project.MSG_WARN);
211   }
212
213   /**
214    * <p>
215    * Log an error with warn log level.
216    * </p>
217    *
218    * @param message log this message
219    * @param t log this cause
220    */

221   public void warn(Object JavaDoc message, Throwable JavaDoc t)
222   {
223     if ((use_level) && (antMsgLevel==Project.MSG_WARN))
224       project.log(message+":"+toString(t));
225
226     project.log(message+":"+toString(t), Project.MSG_WARN);
227   }
228
229   /**
230    * <p>
231    * Is warning logging currently enabled?
232    * </p>
233    *
234    * <p>
235    * Call this method to prevent having to perform expensive operations (for example,
236    * <code>String</code> concatination) when the log level is more than warning.
237    * </p>
238    */

239   public boolean isWarnEnabled()
240   {
241     if (use_level)
242       return antMsgLevel==Project.MSG_WARN;
243
244     return true;
245   }
246
247   /**
248    * <p>
249    * Log a message with error log level.
250    * </p>
251    *
252    * @param message log this message
253    */

254   public void error(Object JavaDoc message)
255   {
256     if ((use_level) && (antMsgLevel==Project.MSG_ERR))
257       project.log(message.toString());
258
259     project.log(message.toString(), Project.MSG_ERR);
260   }
261
262   /**
263    * <p>
264    * Log an error with error log level.
265    * </p>
266    *
267    * @param message log this message
268    * @param t log this cause
269    */

270   public void error(Object JavaDoc message, Throwable JavaDoc t)
271   {
272     if ((use_level) && (antMsgLevel==Project.MSG_ERR))
273       project.log(message+":"+toString(t));
274
275     project.log(message+":"+toString(t), Project.MSG_ERR);
276   }
277
278   /**
279    * <p>
280    * Is error logging currently enabled?
281    * </p>
282    *
283    * <p>
284    * Call this method to prevent having to perform expensive operations (for example,
285    * <code>String</code> concatination) when the log level is more than error.
286    * </p>
287    */

288   public boolean isErrorEnabled()
289   {
290     if (use_level)
291       return antMsgLevel==Project.MSG_ERR;
292
293     return true;
294   }
295
296   /**
297    * <p>
298    * Log a message with fatal log level.
299    * </p>
300    *
301    * @param message log this message
302    */

303   public void fatal(Object JavaDoc message)
304   {
305     if ((use_level) && (antMsgLevel==Project.MSG_ERR))
306       project.log(message.toString());
307
308     project.log(message.toString(), Project.MSG_ERR);
309   }
310
311   /**
312    * <p>
313    * Log an error with fatal log level.
314    * </p>
315    *
316    * @param message log this message
317    * @param t log this cause
318    */

319   public void fatal(Object JavaDoc message, Throwable JavaDoc t)
320   {
321     if ((use_level) && (antMsgLevel==Project.MSG_ERR))
322       project.log(message+":"+toString(t));
323
324     project.log(message+":"+toString(t), Project.MSG_ERR);
325   }
326
327   /**
328    * <p>
329    * Is fatal logging currently enabled?
330    * </p>
331    *
332    * <p>
333    * Call this method to prevent having to perform expensive operations (for example,
334    * <code>String</code> concatination) when the log level is more than fatal.
335    * </p>
336    */

337   public boolean isFatalEnabled()
338   {
339     if (use_level)
340       return antMsgLevel==Project.MSG_ERR;
341
342     return true;
343   }
344
345   private String JavaDoc toString(Throwable JavaDoc t)
346   {
347     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
348     if (t!=null)
349     {
350       buffer.append(" <");
351       buffer.append(t.toString());
352       buffer.append(">");
353
354       java.io.StringWriter JavaDoc sw = new java.io.StringWriter JavaDoc(1024);
355       java.io.PrintWriter JavaDoc pw = new java.io.PrintWriter JavaDoc(sw);
356       t.printStackTrace(pw);
357       pw.close();
358       buffer.append(sw.toString());
359     }
360
361     return buffer.toString();
362   }
363 }
364
Popular Tags