1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.core.loggers; |
2 |
> |
package uk.org.iscream.core.loggers; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.util.*; |
6 |
< |
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; |
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")); |
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 |
82 |
|
// show the window |
83 |
|
show(); |
84 |
|
write(toString(), Logger.SYSINIT, "started"); |
86 |
– |
write(toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
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); |
107 |
< |
_textArea.setRows(_maxMessages); |
108 |
< |
} |
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.ac.ukc.iscream.util.FormatName class |
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 |
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. |
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; |
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(); |
153 |
– |
|
154 |
– |
|
155 |
– |
/** |
156 |
– |
* The verbosity level of this instance |
157 |
– |
*/ |
158 |
– |
private int _verbosityLevel; |
145 |
|
|
146 |
|
/** |
147 |
|
* The maximum number of messages that can |