1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.org.iscream.cms.server.core.loggers; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.core.*; |
5 |
> |
import uk.org.iscream.cms.server.util.*; |
6 |
> |
import uk.org.iscream.cms.server.core.*; |
7 |
|
import java.util.Date; |
8 |
|
import java.text.DateFormat; |
9 |
|
import java.util.Locale; |
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.org.iscream.cms.server.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--- |
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 the text area. 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. |
96 |
< |
* |
97 |
< |
* @param source A string representation of the calling object. |
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); |
104 |
< |
_textArea.insert(line + "\n",0); |
99 |
< |
} |
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 line A line of formatted text to be logged |
100 |
> |
* @param verbosity the verbosity of this message |
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.cms.server.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. |
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; |
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--- |
142 |
|
* A text area to write log messages to |
143 |
|
*/ |
144 |
|
private JTextArea _textArea = new JTextArea(); |
145 |
< |
|
139 |
< |
|
145 |
> |
|
146 |
|
/** |
147 |
< |
* The verbosity level of this instance |
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 _verbosityLevel; |
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 |
|
|