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; |
12 |
|
import java.awt.Color; |
13 |
|
|
14 |
|
/** |
15 |
< |
* The SimpleGUILogger is an implementation of the LoggerImpl defined |
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$ |
28 |
|
* @version $Id$ |
29 |
|
*/ |
30 |
< |
class SimpleSwingLogger extends JFrame implements LoggerImpl { |
30 |
> |
public class SimpleSwingLogger extends JFrame implements LoggerImpl { |
31 |
|
|
32 |
|
//---FINAL ATTRIBUTES--- |
33 |
|
|
47 |
|
* Creates a new Simple Swing Logger. |
48 |
|
*/ |
49 |
|
public SimpleSwingLogger() { |
50 |
< |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
50 |
> |
_maxMessages = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.LoggerClass.SimpleSwingLogger.maxMessages")); |
51 |
|
System.out.println(this.toString() + ": opening window"); |
52 |
|
|
53 |
|
// set up the Frame |
58 |
|
Box box = Box.createVerticalBox(); |
59 |
|
|
60 |
|
// create the i-scream logo at the top |
61 |
< |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
61 |
> |
JLabel iscream = new JLabel(new ImageIcon("./uk/ac/ukc/iscream/core/loggers/i-scream.gif")); |
62 |
|
JLabel comment = new JLabel(" I-Scream System Logging Console"); |
63 |
|
comment.setForeground( new Color(0, 0, 102)); |
64 |
|
iscream.setBackground(Color.white); |
67 |
|
header.add(comment); |
68 |
|
header.add(Box.createHorizontalGlue()); |
69 |
|
header.add(iscream); |
62 |
– |
//header.add(Box.createHorizontalGlue()); |
70 |
|
|
71 |
|
// set up the text area |
72 |
|
_textArea.setEditable(false); |
81 |
|
|
82 |
|
// show the window |
83 |
|
show(); |
84 |
< |
write(this.toString(), Logger.SYSINIT, "started"); |
78 |
< |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
84 |
> |
write(toString(), Logger.SYSINIT, "started"); |
85 |
|
} |
86 |
|
|
87 |
|
//---PUBLIC METHODS--- |
98 |
|
* @param verbosity the verbosity of this message |
99 |
|
* @param message The text to be logged. |
100 |
|
*/ |
101 |
< |
public synchronized void write(String source, int verbosity, String message) { |
102 |
< |
if (verbosity <= _verbosityLevel) { |
103 |
< |
String line = formatLogLine(source, message); |
98 |
< |
_textArea.insert(line + "\n",0); |
99 |
< |
} |
101 |
> |
public synchronized void write(String line, int verbosity) { |
102 |
> |
_textArea.insert(line + "\n",0); |
103 |
> |
_textArea.setRows(_maxMessages); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
108 |
|
* method to provide clean logging (every class should have this). |
109 |
|
* |
110 |
+ |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
111 |
+ |
* to format the toString() |
112 |
+ |
* |
113 |
|
* @return the name of this class and its CVS revision |
114 |
|
*/ |
115 |
|
public String toString() { |
116 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
117 |
< |
//return "ScreenLogger" + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
116 |
> |
return FormatName.getName( |
117 |
> |
_name, |
118 |
> |
getClass().getName(), |
119 |
> |
REVISION); |
120 |
|
} |
121 |
+ |
|
122 |
|
//---PRIVATE METHODS--- |
123 |
|
|
124 |
< |
/** |
125 |
< |
* This method generates a nicely formatted line for the log, |
116 |
< |
* including the date/time and the source of the message. The date |
117 |
< |
* and time are formatted using the DateFormat class, and the source |
118 |
< |
* class is formatted using the toString() method found in every |
119 |
< |
* source file. This is then prepended to the message and returned. |
120 |
< |
* |
121 |
< |
* @param source A string representation of the calling object. |
122 |
< |
* @param message The message to be logged. |
123 |
< |
* @return The string to be written to the log. |
124 |
< |
*/ |
125 |
< |
private String formatLogLine(String source, String message){ |
126 |
< |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
127 |
< |
return "[" + date + "] " + source + ": " + message; |
124 |
> |
private void write(String source, int verbosity, String message) { |
125 |
> |
write(FormatName.formatLogLine(source, verbosity, message), verbosity); |
126 |
|
} |
127 |
|
|
128 |
|
//---ACCESSOR/MUTATOR METHODS--- |
133 |
|
* A text area to write log messages to |
134 |
|
*/ |
135 |
|
private JTextArea _textArea = new JTextArea(); |
136 |
< |
|
139 |
< |
|
136 |
> |
|
137 |
|
/** |
138 |
< |
* The verbosity level of this instance |
138 |
> |
* The maximum number of messages that can |
139 |
> |
* be displayed before bottom items are removed. |
140 |
> |
* This is needed to fix the memory overload problem |
141 |
> |
* that was seen when the GUI got too full! |
142 |
|
*/ |
143 |
< |
private int _verbosityLevel; |
143 |
> |
private int _maxMessages; |
144 |
> |
|
145 |
> |
/** |
146 |
> |
* This is the friendly identifier of the |
147 |
> |
* component this class is running in. |
148 |
> |
* eg, a Filter may be called "filter1", |
149 |
> |
* If this class does not have an owning |
150 |
> |
* component, a name from the configuration |
151 |
> |
* can be placed here. This name could also |
152 |
> |
* be changed to null for utility classes. |
153 |
> |
*/ |
154 |
> |
private String _name = Core.NAME; |
155 |
|
|
156 |
|
//---STATIC ATTRIBUTES--- |
157 |
|
|