1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.core.loggers; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.util.*; |
6 |
import uk.ac.ukc.iscream.core.*; |
7 |
import java.util.Date; |
8 |
import java.text.DateFormat; |
9 |
import java.util.Locale; |
10 |
import javax.swing.*; |
11 |
import javax.swing.border.*; |
12 |
import java.awt.Color; |
13 |
|
14 |
/** |
15 |
* The SimpleSwingLogger is an implementation of the LoggerImpl defined |
16 |
* in the associated interface. It's only purpose is to simply print |
17 |
* all the logging information it receives to a window frame. |
18 |
* |
19 |
* All very simple really... |
20 |
* |
21 |
* This class does have the problem that if too many messages are |
22 |
* entered into the text area, then it will fill the memory of |
23 |
* the JVM. This MAY have been fixed by adding the setRows(int) |
24 |
* in the write(string) method, however the Java API does not |
25 |
* specify if this enforces the max rows, but I THINK it does. |
26 |
* |
27 |
* @author $Author: ajm4 $ |
28 |
* @version $Id: SimpleSwingLogger.java,v 1.4 2000/11/29 21:27:25 ajm4 Exp $ |
29 |
*/ |
30 |
public class SimpleSwingLogger extends JFrame implements LoggerImpl { |
31 |
|
32 |
//---FINAL ATTRIBUTES--- |
33 |
|
34 |
/** |
35 |
* The current CVS revision of this class |
36 |
*/ |
37 |
public final String REVISION = "$Revision: 1.4 $"; |
38 |
|
39 |
private final int width = 700; |
40 |
private final int height = 400; |
41 |
|
42 |
//---STATIC METHODS--- |
43 |
|
44 |
//---CONSTRUCTORS--- |
45 |
|
46 |
/** |
47 |
* Creates a new Simple Swing Logger. |
48 |
*/ |
49 |
public SimpleSwingLogger() { |
50 |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
51 |
_maxMessages = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.LoggerClass.SimpleSwingLogger.maxMessages")); |
52 |
System.out.println(this.toString() + ": opening window"); |
53 |
|
54 |
// set up the Frame |
55 |
setTitle("I-Scream Logger"); |
56 |
setSize(width, height); |
57 |
getContentPane().setBackground(Color.white); |
58 |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
59 |
Box box = Box.createVerticalBox(); |
60 |
|
61 |
// create the i-scream logo at the top |
62 |
JLabel iscream = new JLabel(new ImageIcon("./uk/ac/ukc/iscream/core/loggers/i-scream.gif")); |
63 |
JLabel comment = new JLabel(" I-Scream System Logging Console"); |
64 |
comment.setForeground( new Color(0, 0, 102)); |
65 |
iscream.setBackground(Color.white); |
66 |
comment.setBackground(Color.white); |
67 |
Box header = Box.createHorizontalBox(); |
68 |
header.add(comment); |
69 |
header.add(Box.createHorizontalGlue()); |
70 |
header.add(iscream); |
71 |
|
72 |
// set up the text area |
73 |
_textArea.setEditable(false); |
74 |
JScrollPane text = new JScrollPane(_textArea); |
75 |
text.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
76 |
text.setBackground(Color.white); |
77 |
|
78 |
// build the frame |
79 |
box.add(header); |
80 |
box.add(text); |
81 |
getContentPane().add(box); |
82 |
|
83 |
// show the window |
84 |
show(); |
85 |
write(toString(), Logger.SYSINIT, "started"); |
86 |
write(toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
87 |
} |
88 |
|
89 |
//---PUBLIC METHODS--- |
90 |
|
91 |
/** |
92 |
* The write() method takes a message, formats it using the |
93 |
* formatLogLine() method, and then outputs it to the screen |
94 |
* using the text area. The source is usually the |
95 |
* calling object, referenced by `this'. The method has been |
96 |
* made synchronized to avoid it being called by two different |
97 |
* objects and the output ending up merged on the screen. |
98 |
* |
99 |
* @param source A string representation of the calling object. |
100 |
* @param verbosity the verbosity of this message |
101 |
* @param message The text to be logged. |
102 |
*/ |
103 |
public synchronized void write(String source, int verbosity, String message) { |
104 |
if (verbosity <= _verbosityLevel) { |
105 |
String line = formatLogLine(source, message); |
106 |
_textArea.insert(line + "\n",0); |
107 |
_textArea.setRows(_maxMessages); |
108 |
} |
109 |
} |
110 |
|
111 |
/** |
112 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
113 |
* method to provide clean logging (every class should have this). |
114 |
* |
115 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
116 |
* to format the toString() |
117 |
* |
118 |
* @return the name of this class and its CVS revision |
119 |
*/ |
120 |
public String toString() { |
121 |
return FormatName.getName( |
122 |
_name, |
123 |
getClass().getName(), |
124 |
REVISION); |
125 |
} |
126 |
|
127 |
//---PRIVATE METHODS--- |
128 |
|
129 |
/** |
130 |
* This method generates a nicely formatted line for the log, |
131 |
* including the date/time and the source of the message. The date |
132 |
* and time are formatted using the DateFormat class, and the source |
133 |
* class is formatted using the toString() method found in every |
134 |
* source file. This is then prepended to the message and returned. |
135 |
* |
136 |
* @param source A string representation of the calling object. |
137 |
* @param message The message to be logged. |
138 |
* @return The string to be written to the log. |
139 |
*/ |
140 |
private String formatLogLine(String source, String message){ |
141 |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
142 |
return "[" + date + "] " + source + ": " + message; |
143 |
} |
144 |
|
145 |
//---ACCESSOR/MUTATOR METHODS--- |
146 |
|
147 |
//---ATTRIBUTES--- |
148 |
|
149 |
/** |
150 |
* A text area to write log messages to |
151 |
*/ |
152 |
private JTextArea _textArea = new JTextArea(); |
153 |
|
154 |
|
155 |
/** |
156 |
* The verbosity level of this instance |
157 |
*/ |
158 |
private int _verbosityLevel; |
159 |
|
160 |
/** |
161 |
* The maximum number of messages that can |
162 |
* be displayed before bottom items are removed. |
163 |
* This is needed to fix the memory overload problem |
164 |
* that was seen when the GUI got too full! |
165 |
*/ |
166 |
private int _maxMessages; |
167 |
|
168 |
/** |
169 |
* This is the friendly identifier of the |
170 |
* component this class is running in. |
171 |
* eg, a Filter may be called "filter1", |
172 |
* If this class does not have an owning |
173 |
* component, a name from the configuration |
174 |
* can be placed here. This name could also |
175 |
* be changed to null for utility classes. |
176 |
*/ |
177 |
private String _name = Core.NAME; |
178 |
|
179 |
//---STATIC ATTRIBUTES--- |
180 |
|
181 |
} |