1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.org.iscream.core.loggers; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.core.*; |
5 |
> |
import uk.org.iscream.util.*; |
6 |
> |
import uk.org.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 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 implements LoggerImpl { |
30 |
> |
public class SimpleSwingLogger extends JFrame implements LoggerImpl { |
31 |
|
|
32 |
|
//---FINAL ATTRIBUTES--- |
33 |
|
|
36 |
|
*/ |
37 |
|
public final String REVISION = "$Revision$"; |
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 ScreenLoggerServant. |
47 |
> |
* Creates a new Simple Swing Logger. |
48 |
|
*/ |
49 |
< |
public SimpleGUILogger() { |
50 |
< |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
51 |
< |
System.out.println("Starting the logger"); |
52 |
< |
_frame.getContentPane().add(new JTextField("I-Scream System Logger")); |
53 |
< |
_frame.getContentPane().add(new JScrollPane(_textArea)); |
54 |
< |
|
55 |
< |
_frame.setSize(500,500); |
56 |
< |
_frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
57 |
< |
_frame.show(); |
58 |
< |
System.out.println("Should be showing now"); |
59 |
< |
write(this.toString(), Logger.SYSINIT, "started"); |
60 |
< |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
49 |
> |
public SimpleSwingLogger() { |
50 |
> |
_maxMessages = Integer.parseInt(System.getProperty("uk.org.iscream.LoggerClass.SimpleSwingLogger.maxMessages")); |
51 |
> |
System.out.println(this.toString() + ": opening window"); |
52 |
> |
|
53 |
> |
// set up the Frame |
54 |
> |
setTitle("I-Scream Logger"); |
55 |
> |
setSize(width, height); |
56 |
> |
getContentPane().setBackground(Color.white); |
57 |
> |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
58 |
> |
Box box = Box.createVerticalBox(); |
59 |
> |
|
60 |
> |
// create the i-scream logo at the top |
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); |
65 |
> |
comment.setBackground(Color.white); |
66 |
> |
Box header = Box.createHorizontalBox(); |
67 |
> |
header.add(comment); |
68 |
> |
header.add(Box.createHorizontalGlue()); |
69 |
> |
header.add(iscream); |
70 |
> |
|
71 |
> |
// set up the text area |
72 |
> |
_textArea.setEditable(false); |
73 |
> |
JScrollPane text = new JScrollPane(_textArea); |
74 |
> |
text.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
75 |
> |
text.setBackground(Color.white); |
76 |
> |
|
77 |
> |
// build the frame |
78 |
> |
box.add(header); |
79 |
> |
box.add(text); |
80 |
> |
getContentPane().add(box); |
81 |
> |
|
82 |
> |
// show the window |
83 |
> |
show(); |
84 |
> |
write(toString(), Logger.SYSINIT, "started"); |
85 |
|
} |
86 |
|
|
87 |
|
//---PUBLIC METHODS--- |
88 |
|
|
89 |
|
/** |
90 |
< |
* The write() method takes a message, formats it using the |
91 |
< |
* formatLogLine() method, and then outputs it to the screen |
92 |
< |
* using System.out.println(). The source is usually the |
93 |
< |
* calling object, referenced by `this'. The method has been |
94 |
< |
* made synchronized to avoid it being called by two different |
95 |
< |
* objects and the output ending up merged on the screen. |
90 |
> |
* The write() method takes a line of text, pre-formatted |
91 |
> |
* and outputs it using a method defined by the actual |
92 |
> |
* implementation. The verbosity is given in case the |
93 |
> |
* implementation wishes to utilise it in the layout - |
94 |
> |
* eg. a different colour or font. |
95 |
> |
* |
96 |
> |
* This instance writes the line to a Swing based GUI |
97 |
> |
* logger. |
98 |
|
* |
99 |
< |
* @param source A string representation of the calling object. |
99 |
> |
* @param line A line of formatted text to be logged |
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) { |
66 |
< |
String line = formatLogLine(source, message); |
67 |
< |
_textArea.insert(line + "\n", 0); |
68 |
< |
} |
101 |
> |
*/ |
102 |
> |
public synchronized void write(String line, int verbosity) { |
103 |
> |
_textArea.insert(line + "\n",0); |
104 |
> |
_textArea.setRows(_maxMessages); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
109 |
|
* method to provide clean logging (every class should have this). |
110 |
|
* |
111 |
+ |
* This uses the uk.org.iscream.util.FormatName class |
112 |
+ |
* to format the toString() |
113 |
+ |
* |
114 |
|
* @return the name of this class and its CVS revision |
115 |
|
*/ |
116 |
|
public String toString() { |
117 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
118 |
< |
//return "ScreenLogger" + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
117 |
> |
return FormatName.getName( |
118 |
> |
_name, |
119 |
> |
getClass().getName(), |
120 |
> |
REVISION); |
121 |
|
} |
122 |
+ |
|
123 |
|
//---PRIVATE METHODS--- |
124 |
|
|
125 |
< |
/** |
126 |
< |
* This method generates a nicely formatted line for the log, |
127 |
< |
* including the date/time and the source of the message. The date |
128 |
< |
* and time are formatted using the DateFormat class, and the source |
129 |
< |
* class is formatted using the toString() method found in every |
130 |
< |
* source file. This is then prepended to the message and returned. |
131 |
< |
* |
132 |
< |
* @param source A string representation of the calling object. |
133 |
< |
* @param message The message to be logged. |
134 |
< |
* @return The string to be written to the log. |
93 |
< |
*/ |
94 |
< |
private String formatLogLine(String source, String message){ |
95 |
< |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
96 |
< |
return "[" + date + "] " + source + ": " + message; |
125 |
> |
/** |
126 |
> |
* This method is provided if this class wishes to log |
127 |
> |
* a message itself. |
128 |
> |
* |
129 |
> |
* @param source A String representation of the source |
130 |
> |
* @param verbosity the verbosity of this message |
131 |
> |
* @param message The message to log |
132 |
> |
*/ |
133 |
> |
private void write(String source, int verbosity, String message) { |
134 |
> |
write(FormatName.formatLogLine(source, verbosity, message), verbosity); |
135 |
|
} |
136 |
|
|
137 |
|
//---ACCESSOR/MUTATOR METHODS--- |
138 |
|
|
101 |
– |
// class attributes: |
102 |
– |
// a frame |
103 |
– |
private JFrame _frame = new JFrame("I-SCREAM CORE LOGGER"); |
104 |
– |
|
105 |
– |
// a text area |
106 |
– |
private JTextArea _textArea = new JTextArea(); |
107 |
– |
|
139 |
|
//---ATTRIBUTES--- |
140 |
|
|
141 |
< |
/** |
142 |
< |
* The verbosity level of this instance |
143 |
< |
*/ |
144 |
< |
private int _verbosityLevel; |
141 |
> |
/** |
142 |
> |
* A text area to write log messages to |
143 |
> |
*/ |
144 |
> |
private JTextArea _textArea = new JTextArea(); |
145 |
> |
|
146 |
> |
/** |
147 |
> |
* The maximum number of messages that can |
148 |
> |
* be displayed before bottom items are removed. |
149 |
> |
* This is needed to fix the memory overload problem |
150 |
> |
* that was seen when the GUI got too full! |
151 |
> |
*/ |
152 |
> |
private int _maxMessages; |
153 |
> |
|
154 |
> |
/** |
155 |
> |
* This is the friendly identifier of the |
156 |
> |
* component this class is running in. |
157 |
> |
* eg, a Filter may be called "filter1", |
158 |
> |
* If this class does not have an owning |
159 |
> |
* component, a name from the configuration |
160 |
> |
* can be placed here. This name could also |
161 |
> |
* be changed to null for utility classes. |
162 |
> |
*/ |
163 |
> |
private String _name = Core.NAME; |
164 |
|
|
165 |
|
//---STATIC ATTRIBUTES--- |
166 |
|
|