KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Log


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.PrintStream JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 /**
30  * The <code>Log</code> class provides a means to send some
31  * informational, debugging or error messages like
32  * <code>System.out.println</code> but traces can be enabled or
33  * disabled at runtime.
34  *
35  * <p>JAC supports a -V launching option that allows the user to
36  * enable a given trace category (for instance, by using <code>-V
37  * jac</code>, the user can see what happens in the JAC core system.
38  *
39  * @author <a HREF="mailto:laurent@aopsys.com">Laurent Martelli</a> */

40
41 public final class Log{
42
43     // enabled traces
44
static Hashtable JavaDoc levels = new Hashtable JavaDoc();
45
46     static PrintStream JavaDoc out = System.out;
47
48     static String JavaDoc logHeader = "";
49
50     static Date JavaDoc date = new Date JavaDoc();
51     static DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss.SSS ");
52
53     /**
54      * Sets the logging file.
55      *
56      * @param name the file name if null=>log to the standard output
57      * @param header a string to prepend to each line logged.
58      */

59     public static void setFileName(String JavaDoc name, String JavaDoc header) {
60         try {
61             logHeader = header;
62             out = new PrintStream JavaDoc(new FileOutputStream JavaDoc(name));
63         } catch(Exception JavaDoc e) {
64             out = System.out;
65             e.printStackTrace();
66         }
67     }
68
69     /**
70      * Traces with a level lower than <code>levels[category]</code> are
71      * discarded.
72      *
73      * @param category a string representing the category to trace
74      * @param level the level of the trace (1 is always printed)
75      * @param message the message to print out */

76
77     public static void trace(String JavaDoc category, int level, String JavaDoc message) {
78         if (!(category!=null && levels.containsKey(category))) {
79             levels.put(category, new Integer JavaDoc(0));
80         }
81         if (level<=((Integer JavaDoc)levels.get(category)).intValue()) {
82             date.setTime(System.currentTimeMillis());
83             out.print(dateFormat.format(date));
84             out.print(logHeader);
85             out.print(category);
86             out.print(": ");
87             out.println(message);
88         }
89     }
90
91     /**
92      * Print the stack trace of an exception with a level lower than
93      * <code>levels[category]</code> are discarded.
94      *
95      * @param category a string representing the category to trace
96      * @param level the level of the trace (1 is always printed)
97      * @param exception the exception */

98
99     public static void trace(String JavaDoc category, int level, Throwable JavaDoc exception) {
100         if (category!=null && levels.containsKey(category)) {
101             if (level<=((Integer JavaDoc)levels.get(category)).intValue()) {
102                 out.println(logHeader+category+": StackTrace");
103                 exception.printStackTrace(out);
104             }
105         } else {
106             levels.put(category, new Integer JavaDoc(0));
107         }
108     }
109
110
111     /**
112      * Prints a stack trace.
113      *
114      * @param category a string representing the category to trace
115      * @param level the level of the trace (1 is always printed)
116      */

117     public static void stack(String JavaDoc category, int level) {
118         if (category!=null && levels.containsKey(category)) {
119             if (level<=((Integer JavaDoc)levels.get(category)).intValue()) {
120                 out.println(logHeader+category+": StackTrace");
121                 new Exception JavaDoc().printStackTrace(out);
122             }
123         } else {
124             levels.put(category, new Integer JavaDoc(0));
125         }
126     }
127
128     public static void stack(String JavaDoc category) {
129         stack(category,1);
130     }
131
132     /**
133      * Traces with a level equals to 1.
134      *
135      * @param category a string representing the category to trace
136      * @param message the message to print out
137      * @see #trace(String,int,String) */

138
139     public static void trace(String JavaDoc category, String JavaDoc message) {
140         Log.trace(category, 1, message);
141     }
142
143     /**
144      * Print a stack trace with a level equals to 1.
145      *
146      * @param category a string representing the category to trace
147      * @param exception the message to print out
148      * @see #trace(String,int,String) */

149
150     public static void trace(String JavaDoc category, Throwable JavaDoc exception) {
151         Log.trace(category, 1, exception);
152     }
153
154     /**
155      * Traces an error into the <code>System.err</code> stream.
156      *
157      * @param message the error message to print out */

158
159     public static void error(String JavaDoc message) {
160         out.println(logHeader+"ERROR: "+message);
161     }
162
163     /**
164      * Traces a warning into the <code>System.err</code> stream.
165      *
166      * @param message the warning message to print out
167      * @param level warning level (0=important, 1=normal, 2=low, ...)
168      */

169     public static void warning(String JavaDoc message, int level) {
170         if (level<=1) {
171             out.println(logHeader+"WARNING: "+message);
172         }
173     }
174
175     /**
176      * Traces a warning into the <code>System.err</code> stream.
177      *
178      * @param message the warning message to print out
179      */

180     public static void warning(String JavaDoc message) {
181         warning(message, 1);
182     }
183
184     /**
185      * Traces a warning into the <code>System.err</code> stream only if
186      * the given category is enable to trace.
187      *
188      * @param message the warning message to print out
189      */

190     public static void warning(String JavaDoc category, String JavaDoc message) {
191         warning(category+": "+message, 1);
192     }
193
194     /**
195      * Traces a warning into the <code>System.err</code> stream only if
196      * the given category is enable to trace.
197      *
198      * @param message the warning message to print out
199      */

200     public static void warning(String JavaDoc category, int level, String JavaDoc message) {
201         warning(category+": "+message, level);
202     }
203
204     /**
205      * Sets the verbose level of a given category.
206      *
207      * <p>The higher, the more traces are printed out.
208      *
209      * @param category the category to set the level of
210      * @param level the category verbose level */

211
212     public static void setLevel(String JavaDoc category, int level) {
213         levels.put(category, new Integer JavaDoc(level));
214     }
215
216     /**
217      * Returns a Map category -> enabled saying which traces are enables
218      */

219     public static Map JavaDoc getLevels() {
220         return levels;
221     }
222
223     public static Set JavaDoc getCategories(Object JavaDoc substance) {
224         return levels.keySet();
225     }
226
227     public static String JavaDoc dump() {
228         return Strings.hex(levels)+" : "+levels;
229     }
230 }
231
Popular Tags