Source code: com/eireneh/util/Logger.java
1
2 package com.eireneh.util;
3
4 import java.util.*;
5
6 import com.eireneh.util.event.*;
7
8 /**
9 * This package looks after Exceptions as they happen.
10 *
11 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
12 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
13 * Distribution Licence:<br />
14 * Project B is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General Public License,
16 * version 2 as published by the Free Software Foundation.<br />
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.<br />
21 * The License is available on the internet
22 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
23 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
25 * The copyright to this program is held by it's authors.
26 * </font></td></tr></table>
27 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
28 * @see docs.Licence
29 * @author Joe Walker
30 */
31 public class Logger
32 {
33 /**
34 * Simple ctor for use only with getLogger(String)
35 */
36 protected Logger(String source)
37 {
38 this.source = source;
39 }
40
41 /**
42 * Create a new Logger
43 */
44 public static Logger getLogger(String source)
45 {
46 Logger reply = (Logger) loggers.get(source);
47
48 if (reply == null)
49 {
50 reply = new Logger(source);
51 loggers.put(source, reply);
52 }
53
54 return reply;
55 }
56
57 /**
58 * Log a message to a specific log level
59 */
60 public void log(int level, String message)
61 {
62 fireCapture(log_list, new CaptureEvent(source, message, level));
63 }
64
65 /**
66 * Log an Exception but don't tell the user about it
67 * @param prob What went wrong
68 */
69 public void log(int level, String message, Throwable prob)
70 {
71 if (prob instanceof ThreadDeath)
72 throw (ThreadDeath) prob;
73
74 fireCapture(log_list, new CaptureEvent(source, prob, level));
75 }
76
77 /**
78 * Log a message
79 * @param message The text message
80 */
81 public void finest(String message)
82 {
83 fireCapture(log_list, new CaptureEvent(source, message, Level.FINEST));
84 }
85
86 /**
87 * Log a message
88 * @param message The text message
89 */
90 public void finer(String message)
91 {
92 fireCapture(log_list, new CaptureEvent(source, message, Level.FINER));
93 }
94
95 /**
96 * Log a message
97 * @param message The text message
98 */
99 public void fine(String message)
100 {
101 fireCapture(log_list, new CaptureEvent(source, message, Level.FINE));
102 }
103
104 /**
105 * Log a message
106 * @param message The text message
107 */
108 public void info(String message)
109 {
110 fireCapture(log_list, new CaptureEvent(source, message, Level.INFO));
111 }
112
113 /**
114 * Log a message
115 * @param message The text message
116 */
117 public void config(String message)
118 {
119 fireCapture(log_list, new CaptureEvent(source, message, Level.CONFIG));
120 }
121
122 /**
123 * Log a message
124 * @param message The text message
125 */
126 public void warning(String message)
127 {
128 fireCapture(log_list, new CaptureEvent(source, message, Level.WARNING));
129 }
130
131 /**
132 * Log a message
133 * @param message The text message
134 */
135 public void severe(String message)
136 {
137 fireCapture(log_list, new CaptureEvent(source, message, Level.SEVERE));
138 }
139
140 /**
141 * Log a message
142 * @param source Where the message comes from
143 * @param message The text message
144 */
145 protected static void fireCapture(EventListenerList list, CaptureEvent ev)
146 {
147 // Guaranteed to return a non-null array
148 Object[] listeners = list.getListenerList();
149
150 // Process the listeners last to first, notifying
151 // those that are interested in this event
152 for (int i=listeners.length-2; i>=0; i-=2)
153 {
154 if (listeners[i] == CaptureListener.class)
155 {
156 CaptureListener li = (CaptureListener) listeners[i+1];
157 try
158 {
159 if (ev.getException() != null)
160 li.captureException(ev);
161 else
162 li.captureMessage(ev);
163 }
164 catch (Throwable ex)
165 {
166 if (ex instanceof ThreadDeath)
167 throw (ThreadDeath) ex;
168
169 list.remove(CaptureListener.class, li);
170
171 Reporter.informUser(li, ex);
172 }
173 }
174 }
175 }
176
177 /**
178 * Gets a short HTML description of an Exception for display in a
179 * window
180 */
181 public static String getHTMLDescription(Throwable ex)
182 {
183 StringBuffer retcode = new StringBuffer();
184
185 // The message in the exception
186 String message = ex.getMessage();
187 if (message == null || message.equals(""))
188 message = "No description available";
189 message = StringUtil.swap(message, "\n", "<br>");
190
191 // The name of the exception
192 String classname = ex.getClass().getName();
193 int lastdot = classname.lastIndexOf('.');
194 if (lastdot != -1)
195 classname = classname.substring(lastdot+1);
196 if (classname.endsWith("Exception") && classname.length() > "Exception".length())
197 classname = classname.substring(0, classname.length() - "Exception".length());
198 if (classname.endsWith("Error") && classname.length() > "Error".length())
199 classname = classname.substring(0, classname.length() - "Error".length());
200 classname = StringUtil.createTitle(classname);
201 if (classname.equals("IO")) classname = "Input / Output";
202
203 retcode.append("<font size=\"-1\"><strong>");
204 retcode.append(classname);
205 retcode.append("</strong></font><br>");
206 retcode.append(message);
207
208 // If this is a LucidException with a nested Exception
209 if (ex instanceof LucidException)
210 {
211 Throwable nex = ((LucidException) ex).getException();
212 if (nex != null)
213 {
214 retcode.append("<p><br><font size=\"-1\">This was caused by: </font>");
215 retcode.append(getHTMLDescription(nex));
216 }
217 }
218
219 return retcode.toString();
220 }
221
222 /**
223 * Add an Exception listener to the list of things wanting
224 * to know whenever we capture an Exception
225 */
226 public static void addLogCaptureListener(CaptureListener li)
227 {
228 log_list.add(CaptureListener.class, li);
229 }
230
231 /**
232 * Remove an Exception listener from the list of things wanting
233 * to know whenever we capture an Exception
234 */
235 public static void removeLogCaptureListener(CaptureListener li)
236 {
237 log_list.remove(CaptureListener.class, li);
238 }
239
240 /** The originator of the message */
241 private String source;
242
243 /** The list of generated Loggers */
244 private static Hashtable loggers = new Hashtable();
245
246 /** The list of listeners */
247 protected static EventListenerList log_list = new EventListenerList();
248 }