KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > taskman > DefaultTaskOutput


1 package org.sapia.taskman;
2
3 /**
4  * A <code>TaskOutput</code> that logs to System.out. This class adds "debug
5  * level" functionality.
6  *
7  * @author Yanick Duchesne 15-Apr-2003
8  * <dl>
9  * <dt><b>Copyright: </b>
10  * <dd>Copyright &#169; 2002-2004 <a
11  * HREF="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
12  * Rights Reserved.</dd>
13  * </dt>
14  * <dt><b>License: </b>
15  * <dd>Read the license.txt file of the jar or visit the <a
16  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the
17  * Sapia OSS web site</dd>
18  * </dt>
19  * </dl>
20  */

21 public class DefaultTaskOutput implements TaskOutput {
22   /**
23    * Identifies the "debug" level - the <code>debug()</code> method has been
24    * called to log a message.
25    */

26   public static int DEBUG = 0;
27
28   /**
29    * Identifies the "info" level - the <code>info()</code> method has been
30    * called to log a message.
31    */

32   public static int INFO = 1;
33
34   /**
35    * Identifies the "warning" level - the <code>warning()</code> method has
36    * been called to log a message.
37    */

38   public static int WARNING = 2;
39
40   /**
41    * Identifies the "error" level - the <code>error()</code> method has been
42    * called to log a message.
43    */

44   public static int ERROR = 3;
45
46   /**
47    * An array string constant that holds the name of the logging priority
48    * defined by the constants in this class; in fact, each constant corresponds
49    * to an index in this array.
50    */

51   public static final String JavaDoc[] LEVEL = new String JavaDoc[] { "debug", "info",
52       "warning", "error" };
53   private int _lvl;
54   protected String JavaDoc _taskName;
55
56   /**
57    * Constructor for DefaultTaskLog.
58    */

59   public DefaultTaskOutput(String JavaDoc taskName, int level) {
60     _lvl = level;
61     _taskName = taskName;
62   }
63
64   /**
65    * @see org.sapia.taskman.TaskOutput#setTaskName(String)
66    */

67   public void setTaskName(String JavaDoc name) {
68     _taskName = name;
69   }
70
71   /**
72    * @see org.sapia.taskman.TaskOutput#debug(Object)
73    */

74   public TaskOutput debug(Object JavaDoc message) {
75     if(DEBUG >= _lvl) {
76       doOutput(DEBUG, message, null);
77     }
78
79     return this;
80   }
81
82   /**
83    * @see org.sapia.taskman.TaskOutput#error(Object, Throwable)
84    */

85   public TaskOutput error(Object JavaDoc message, Throwable JavaDoc t) {
86     if(ERROR >= _lvl) {
87       doOutput(ERROR, message, t);
88     }
89
90     return this;
91   }
92
93   /**
94    * @see org.sapia.taskman.TaskOutput#error(Object)
95    */

96   public TaskOutput error(Object JavaDoc message) {
97     if(ERROR >= _lvl) {
98       doOutput(ERROR, message, null);
99     }
100
101     return this;
102   }
103
104   /**
105    * @see org.sapia.taskman.TaskOutput#error(Throwable)
106    */

107   public TaskOutput error(Throwable JavaDoc t) {
108     if(ERROR >= _lvl) {
109       doOutput(t);
110     }
111
112     return this;
113   }
114
115   /**
116    * @see org.sapia.taskman.TaskOutput#info(Object)
117    */

118   public TaskOutput info(Object JavaDoc message) {
119     if(INFO >= _lvl) {
120       doOutput(INFO, message, null);
121     }
122
123     return this;
124   }
125
126   /**
127    * @see org.sapia.taskman.TaskOutput#warning(Object)
128    */

129   public TaskOutput warning(Object JavaDoc message) {
130     if(WARNING >= _lvl) {
131       doOutput(WARNING, message, null);
132     }
133
134     return this;
135   }
136
137   /**
138    * This method has an empty implementation (closing System.out would not be a
139    * good idea).
140    *
141    * @see org.sapia.taskman.TaskOutput#close()
142    */

143   public void close() {
144   }
145
146   /**
147    * Returns this instance's name.
148    *
149    * @return a name.
150    */

151   protected String JavaDoc getTaskName() {
152     return _taskName;
153   }
154
155   /**
156    * Returns the name of the passed in priority.
157    *
158    * @return a name.
159    */

160   protected String JavaDoc getLevelNameFor(int priority) {
161     return LEVEL[priority];
162   }
163
164   /**
165    * Template method that can be overriden to log to another place than
166    * System.out.
167    *
168    * @param lvl
169    * the priority at which the given message is logged; this
170    * parameter's value corresponds to one of the constants of this
171    * class.
172    *
173    * @param message
174    * the message that was logged.
175    * @param t
176    * the <code>Throwable</code> that was logged, or <code>null</code>
177    * if no <code>Throwable</code> was logged.
178    *
179    * @see #DEBUG
180    * @see #INFO
181    * @see #WARNING
182    * @see #ERROR
183    */

184   protected void doOutput(int lvl, Object JavaDoc message, Throwable JavaDoc t) {
185     System.out.print("[");
186     System.out.print(_taskName);
187     System.out.print(" - ");
188     System.out.print(LEVEL[lvl]);
189     System.out.print("]");
190     System.out.println(message);
191
192     if(t != null) {
193       t.printStackTrace();
194     }
195   }
196
197   /**
198    * Template method that can be overriden to log to another place than
199    * System.out.
200    *
201    * @param t
202    * the <code>Throwable</code> that was logged.
203    */

204   protected void doOutput(Throwable JavaDoc t) {
205     System.out.print("[");
206     System.out.print(_taskName);
207     System.out.print(" - ");
208     System.out.print(LEVEL[ERROR]);
209     System.out.print("]");
210     System.out.println();
211     t.printStackTrace();
212   }
213 }
214
Popular Tags