001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.ant;
010:
011: import org.apache.commons.logging.Log;
012:
013: import org.apache.tools.ant.Project;
014:
015: /**
016: * The Ant wrapper class for Logger.
017: *
018: * @author <a href="mailto:stephan@apache.org">Stephan Michels </a>
019: * @version CVS $Id: AntLog.java,v 1.3 2004/01/09 10:48:06 benedikta Exp $
020: */
021: public final class AntLog implements Log {
022: // underlying implementation to delegate to
023: private final Project project;
024: private boolean use_level = false;
025: private int antMsgLevel = Project.MSG_ERR;
026:
027: /**
028: * Create a logger that delegates to specified logger.
029: *
030: * @param project the Ant logger to delegate to
031: */
032: public AntLog(Project project) {
033: this .project = project;
034: }
035:
036: /**
037: * Create a logger that delegates to specified logger.
038: *
039: * @param project the Ant logger to delegate to
040: * @param antMsgLevel the massage level from ant
041: */
042: public AntLog(Project project, int antMsgLevel) {
043: this .project = project;
044: this .antMsgLevel = antMsgLevel;
045: use_level = true;
046: }
047:
048: /**
049: * <p>
050: * Log a message with trace log level.
051: * </p>
052: *
053: * @param message log this message
054: */
055: public void trace(Object message) {
056: if ((use_level) && (antMsgLevel == Project.MSG_DEBUG))
057: project.log(message.toString());
058:
059: project.log(message.toString(), Project.MSG_DEBUG);
060: }
061:
062: /**
063: * <p>
064: * Log an error with trace log level.
065: * </p>
066: *
067: * @param message log this message
068: * @param t log this cause
069: */
070: public void trace(Object message, Throwable t) {
071: if ((use_level) && (antMsgLevel == Project.MSG_DEBUG))
072: project.log(message + ":" + toString(t));
073:
074: project.log(message + ":" + toString(t), Project.MSG_DEBUG);
075: }
076:
077: /**
078: * <p>
079: * Is trace logging currently enabled?
080: * </p>
081: *
082: * <p>
083: * Call this method to prevent having to perform expensive operations (for example,
084: * <code>String</code> concatination) when the log level is more than trace.
085: * </p>
086: */
087: public boolean isTraceEnabled() {
088: if (use_level)
089: return antMsgLevel == Project.MSG_DEBUG;
090:
091: return true;
092: }
093:
094: /**
095: * <p>
096: * Log a message with debug log level.
097: * </p>
098: *
099: * @param message log this message
100: */
101: public void debug(Object message) {
102: if ((use_level) && (antMsgLevel == Project.MSG_DEBUG))
103: project.log(message.toString());
104:
105: project.log(message.toString(), Project.MSG_DEBUG);
106: }
107:
108: /**
109: * <p>
110: * Log an error with debug log level.
111: * </p>
112: *
113: * @param message log this message
114: * @param t log this cause
115: */
116: public void debug(Object message, Throwable t) {
117: if ((use_level) && (antMsgLevel == Project.MSG_DEBUG))
118: project.log(message + ":" + toString(t));
119:
120: project.log(message + ":" + toString(t), Project.MSG_DEBUG);
121: }
122:
123: /**
124: * <p>
125: * Is debug logging currently enabled?
126: * </p>
127: *
128: * <p>
129: * Call this method to prevent having to perform expensive operations (for example,
130: * <code>String</code> concatination) when the log level is more than debug.
131: * </p>
132: */
133: public boolean isDebugEnabled() {
134: if (use_level)
135: return antMsgLevel == Project.MSG_DEBUG;
136:
137: return true;
138: }
139:
140: /**
141: * <p>
142: * Log a message with info log level.
143: * </p>
144: *
145: * @param message log this message
146: */
147: public void info(Object message) {
148: if ((use_level) && (antMsgLevel == Project.MSG_INFO))
149: project.log(message.toString());
150:
151: project.log(message.toString(), Project.MSG_INFO);
152: }
153:
154: /**
155: * <p>
156: * Log an error with info log level.
157: * </p>
158: *
159: * @param message log this message
160: * @param t log this cause
161: */
162: public void info(Object message, Throwable t) {
163: if ((use_level) && (antMsgLevel == Project.MSG_INFO))
164: project.log(message + ":" + toString(t));
165:
166: project.log(message + ":" + toString(t), Project.MSG_INFO);
167: }
168:
169: /**
170: * <p>
171: * Is info logging currently enabled?
172: * </p>
173: *
174: * <p>
175: * Call this method to prevent having to perform expensive operations (for example,
176: * <code>String</code> concatination) when the log level is more than info.
177: * </p>
178: */
179: public boolean isInfoEnabled() {
180: if (use_level)
181: return antMsgLevel == Project.MSG_INFO;
182:
183: return true;
184: }
185:
186: /**
187: * <p>
188: * Log a message with warn log level.
189: * </p>
190: *
191: * @param message log this message
192: */
193: public void warn(Object message) {
194: if ((use_level) && (antMsgLevel == Project.MSG_WARN))
195: project.log(message.toString());
196:
197: project.log(message.toString(), Project.MSG_WARN);
198: }
199:
200: /**
201: * <p>
202: * Log an error with warn log level.
203: * </p>
204: *
205: * @param message log this message
206: * @param t log this cause
207: */
208: public void warn(Object message, Throwable t) {
209: if ((use_level) && (antMsgLevel == Project.MSG_WARN))
210: project.log(message + ":" + toString(t));
211:
212: project.log(message + ":" + toString(t), Project.MSG_WARN);
213: }
214:
215: /**
216: * <p>
217: * Is warning logging currently enabled?
218: * </p>
219: *
220: * <p>
221: * Call this method to prevent having to perform expensive operations (for example,
222: * <code>String</code> concatination) when the log level is more than warning.
223: * </p>
224: */
225: public boolean isWarnEnabled() {
226: if (use_level)
227: return antMsgLevel == Project.MSG_WARN;
228:
229: return true;
230: }
231:
232: /**
233: * <p>
234: * Log a message with error log level.
235: * </p>
236: *
237: * @param message log this message
238: */
239: public void error(Object message) {
240: if ((use_level) && (antMsgLevel == Project.MSG_ERR))
241: project.log(message.toString());
242:
243: project.log(message.toString(), Project.MSG_ERR);
244: }
245:
246: /**
247: * <p>
248: * Log an error with error log level.
249: * </p>
250: *
251: * @param message log this message
252: * @param t log this cause
253: */
254: public void error(Object message, Throwable t) {
255: if ((use_level) && (antMsgLevel == Project.MSG_ERR))
256: project.log(message + ":" + toString(t));
257:
258: project.log(message + ":" + toString(t), Project.MSG_ERR);
259: }
260:
261: /**
262: * <p>
263: * Is error logging currently enabled?
264: * </p>
265: *
266: * <p>
267: * Call this method to prevent having to perform expensive operations (for example,
268: * <code>String</code> concatination) when the log level is more than error.
269: * </p>
270: */
271: public boolean isErrorEnabled() {
272: if (use_level)
273: return antMsgLevel == Project.MSG_ERR;
274:
275: return true;
276: }
277:
278: /**
279: * <p>
280: * Log a message with fatal log level.
281: * </p>
282: *
283: * @param message log this message
284: */
285: public void fatal(Object message) {
286: if ((use_level) && (antMsgLevel == Project.MSG_ERR))
287: project.log(message.toString());
288:
289: project.log(message.toString(), Project.MSG_ERR);
290: }
291:
292: /**
293: * <p>
294: * Log an error with fatal log level.
295: * </p>
296: *
297: * @param message log this message
298: * @param t log this cause
299: */
300: public void fatal(Object message, Throwable t) {
301: if ((use_level) && (antMsgLevel == Project.MSG_ERR))
302: project.log(message + ":" + toString(t));
303:
304: project.log(message + ":" + toString(t), Project.MSG_ERR);
305: }
306:
307: /**
308: * <p>
309: * Is fatal logging currently enabled?
310: * </p>
311: *
312: * <p>
313: * Call this method to prevent having to perform expensive operations (for example,
314: * <code>String</code> concatination) when the log level is more than fatal.
315: * </p>
316: */
317: public boolean isFatalEnabled() {
318: if (use_level)
319: return antMsgLevel == Project.MSG_ERR;
320:
321: return true;
322: }
323:
324: private String toString(Throwable t) {
325: StringBuffer buffer = new StringBuffer();
326: if (t != null) {
327: buffer.append(" <");
328: buffer.append(t.toString());
329: buffer.append(">");
330:
331: java.io.StringWriter sw = new java.io.StringWriter(1024);
332: java.io.PrintWriter pw = new java.io.PrintWriter(sw);
333: t.printStackTrace(pw);
334: pw.close();
335: buffer.append(sw.toString());
336: }
337:
338: return buffer.toString();
339: }
340: }
|