1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
//---PACKAGE DECLARATION--- |
22 |
+ |
package uk.org.iscream.cms.server.core.loggers; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
< |
import uk.ac.ukc.iscream.core.*; |
25 |
> |
import uk.org.iscream.cms.util.*; |
26 |
> |
import uk.org.iscream.cms.server.core.*; |
27 |
|
import java.util.Date; |
28 |
|
import java.text.DateFormat; |
29 |
|
import java.util.Locale; |
32 |
|
import java.awt.Color; |
33 |
|
|
34 |
|
/** |
35 |
< |
* The SimpleGUILogger is an implementation of the LoggerImpl defined |
35 |
> |
* The SimpleSwingLogger is an implementation of the LoggerImpl defined |
36 |
|
* in the associated interface. It's only purpose is to simply print |
37 |
|
* all the logging information it receives to a window frame. |
38 |
|
* |
39 |
|
* All very simple really... |
40 |
|
* |
41 |
+ |
* This class does have the problem that if too many messages are |
42 |
+ |
* entered into the text area, then it will fill the memory of |
43 |
+ |
* the JVM. This MAY have been fixed by adding the setRows(int) |
44 |
+ |
* in the write(string) method, however the Java API does not |
45 |
+ |
* specify if this enforces the max rows, but I THINK it does. |
46 |
+ |
* |
47 |
|
* @author $Author$ |
48 |
|
* @version $Id$ |
49 |
|
*/ |
50 |
< |
class SimpleSwingLogger extends JFrame implements LoggerImpl { |
50 |
> |
public class SimpleSwingLogger extends JFrame implements LoggerImpl { |
51 |
|
|
52 |
|
//---FINAL ATTRIBUTES--- |
53 |
|
|
67 |
|
* Creates a new Simple Swing Logger. |
68 |
|
*/ |
69 |
|
public SimpleSwingLogger() { |
70 |
< |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
70 |
> |
_maxMessages = Integer.parseInt(System.getProperty("uk.org.iscream.cms.server.LoggerClass.SimpleSwingLogger.maxMessages")); |
71 |
|
System.out.println(this.toString() + ": opening window"); |
72 |
|
|
73 |
|
// set up the Frame |
78 |
|
Box box = Box.createVerticalBox(); |
79 |
|
|
80 |
|
// create the i-scream logo at the top |
81 |
< |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
82 |
< |
JLabel comment = new JLabel(" I-Scream System Logging Console"); |
81 |
> |
JLabel iscream = new JLabel(new ImageIcon(getClass().getResource("/uk/org/iscream/cms/server/core/loggers/i-scream.gif"))); |
82 |
> |
JLabel comment = new JLabel(" i-scream System Logging Console"); |
83 |
|
comment.setForeground( new Color(0, 0, 102)); |
84 |
|
iscream.setBackground(Color.white); |
85 |
|
comment.setBackground(Color.white); |
87 |
|
header.add(comment); |
88 |
|
header.add(Box.createHorizontalGlue()); |
89 |
|
header.add(iscream); |
62 |
– |
//header.add(Box.createHorizontalGlue()); |
90 |
|
|
91 |
|
// set up the text area |
92 |
|
_textArea.setEditable(false); |
101 |
|
|
102 |
|
// show the window |
103 |
|
show(); |
104 |
< |
write(this.toString(), Logger.SYSINIT, "started"); |
78 |
< |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
104 |
> |
write(toString(), Logger.SYSINIT, "started"); |
105 |
|
} |
106 |
|
|
107 |
|
//---PUBLIC METHODS--- |
108 |
|
|
109 |
|
/** |
110 |
< |
* The write() method takes a message, formats it using the |
111 |
< |
* formatLogLine() method, and then outputs it to the screen |
112 |
< |
* using the text area. The source is usually the |
113 |
< |
* calling object, referenced by `this'. The method has been |
114 |
< |
* made synchronized to avoid it being called by two different |
115 |
< |
* objects and the output ending up merged on the screen. |
116 |
< |
* |
117 |
< |
* @param source A string representation of the calling object. |
118 |
< |
* @param verbosity the verbosity of this message |
119 |
< |
* @param message The text to be logged. |
120 |
< |
*/ |
121 |
< |
public synchronized void write(String source, int verbosity, String message) { |
122 |
< |
if (verbosity <= _verbosityLevel) { |
123 |
< |
String line = formatLogLine(source, message); |
124 |
< |
_textArea.insert(line + "\n",0); |
99 |
< |
} |
110 |
> |
* The write() method takes a line of text, pre-formatted |
111 |
> |
* and outputs it using a method defined by the actual |
112 |
> |
* implementation. The verbosity is given in case the |
113 |
> |
* implementation wishes to utilise it in the layout - |
114 |
> |
* eg. a different colour or font. |
115 |
> |
* |
116 |
> |
* This instance writes the line to a Swing based GUI |
117 |
> |
* logger. |
118 |
> |
* |
119 |
> |
* @param line A line of formatted text to be logged |
120 |
> |
* @param verbosity the verbosity of this message |
121 |
> |
*/ |
122 |
> |
public synchronized void write(String line, int verbosity) { |
123 |
> |
_textArea.insert(line + "\n",0); |
124 |
> |
_textArea.setRows(_maxMessages); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
129 |
|
* method to provide clean logging (every class should have this). |
130 |
|
* |
131 |
+ |
* This uses the uk.org.iscream.cms.util.FormatName class |
132 |
+ |
* to format the toString() |
133 |
+ |
* |
134 |
|
* @return the name of this class and its CVS revision |
135 |
|
*/ |
136 |
|
public String toString() { |
137 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
138 |
< |
//return "ScreenLogger" + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
137 |
> |
return FormatName.getName( |
138 |
> |
_name, |
139 |
> |
getClass().getName(), |
140 |
> |
REVISION); |
141 |
|
} |
142 |
+ |
|
143 |
|
//---PRIVATE METHODS--- |
144 |
|
|
145 |
|
/** |
146 |
< |
* This method generates a nicely formatted line for the log, |
147 |
< |
* including the date/time and the source of the message. The date |
148 |
< |
* and time are formatted using the DateFormat class, and the source |
149 |
< |
* class is formatted using the toString() method found in every |
150 |
< |
* source file. This is then prepended to the message and returned. |
151 |
< |
* |
152 |
< |
* @param source A string representation of the calling object. |
153 |
< |
* @param message The message to be logged. |
154 |
< |
* @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; |
146 |
> |
* This method is provided if this class wishes to log |
147 |
> |
* a message itself. |
148 |
> |
* |
149 |
> |
* @param source A String representation of the source |
150 |
> |
* @param verbosity the verbosity of this message |
151 |
> |
* @param message The message to log |
152 |
> |
*/ |
153 |
> |
private void write(String source, int verbosity, String message) { |
154 |
> |
write(FormatName.formatLogLine(source, verbosity, message), verbosity); |
155 |
|
} |
156 |
|
|
157 |
|
//---ACCESSOR/MUTATOR METHODS--- |
162 |
|
* A text area to write log messages to |
163 |
|
*/ |
164 |
|
private JTextArea _textArea = new JTextArea(); |
165 |
< |
|
139 |
< |
|
165 |
> |
|
166 |
|
/** |
167 |
< |
* The verbosity level of this instance |
167 |
> |
* The maximum number of messages that can |
168 |
> |
* be displayed before bottom items are removed. |
169 |
> |
* This is needed to fix the memory overload problem |
170 |
> |
* that was seen when the GUI got too full! |
171 |
|
*/ |
172 |
< |
private int _verbosityLevel; |
172 |
> |
private int _maxMessages; |
173 |
> |
|
174 |
> |
/** |
175 |
> |
* This is the friendly identifier of the |
176 |
> |
* component this class is running in. |
177 |
> |
* eg, a Filter may be called "filter1", |
178 |
> |
* If this class does not have an owning |
179 |
> |
* component, a name from the configuration |
180 |
> |
* can be placed here. This name could also |
181 |
> |
* be changed to null for utility classes. |
182 |
> |
*/ |
183 |
> |
private String _name = Core.NAME; |
184 |
|
|
185 |
|
//---STATIC ATTRIBUTES--- |
186 |
|
|
187 |
< |
} |
187 |
> |
} |