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; |
20 |
|
* @author $Author$ |
21 |
|
* @version $Id$ |
22 |
|
*/ |
23 |
< |
class FileLogger implements LoggerImpl { |
23 |
> |
public class FileLogger implements LoggerImpl { |
24 |
|
|
25 |
|
//---FINAL ATTRIBUTES--- |
26 |
|
|
47 |
|
*/ |
48 |
|
public FileLogger() throws IOException{ |
49 |
|
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
50 |
< |
filename = System.getProperty("uk.ac.ukc.iscream.LoggerClassParam1"); |
50 |
> |
filename = System.getProperty("uk.ac.ukc.iscream.LoggerClass.FileLogger.filename"); |
51 |
|
// Perform file check to make sure writing is ok |
52 |
|
if(!fileCheck()){ |
53 |
|
// Have to system.out.println errors because logging mechanism failed ! |
61 |
|
open = true; |
62 |
|
} |
63 |
|
catch(IOException e){ |
64 |
< |
System.out.println(formatLogLine(this.toString(), "Attemp to setup writer failed: "+e.getMessage())); |
64 |
> |
System.out.println(formatLogLine(toString(), "Attempt to setup writer failed - " + e.getMessage())); |
65 |
|
} |
66 |
|
// Put an initial line into the log |
67 |
< |
write(this.toString(), Logger.SYSINIT, "started"); |
68 |
< |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
67 |
> |
write(toString(), Logger.SYSINIT, "started"); |
68 |
> |
write(toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
69 |
|
} |
70 |
|
|
71 |
|
//---PUBLIC METHODS--- |
100 |
|
} |
101 |
|
catch(IOException e){ |
102 |
|
// We'd best log the error |
103 |
< |
System.out.println(formatLogLine(this.toString(), "Writing to logfile failed: "+e.getMessage())); |
103 |
> |
System.out.println(formatLogLine(this.toString(), "Writing to logfile failed - " + e.getMessage())); |
104 |
|
// As it's an IOException we should suspend logging |
105 |
|
open = false; |
106 |
|
} |
107 |
|
} |
108 |
|
else{ |
109 |
|
// If file is not open we should print this to the screen |
110 |
< |
System.out.println(formatLogLine(this.toString(), "Write failed: file not open")); |
110 |
> |
System.out.println(formatLogLine(this.toString(), "Write failed - file not open")); |
111 |
|
} |
112 |
|
} |
113 |
|
|
120 |
|
public void clear(){ |
121 |
|
// Check file is open |
122 |
|
if(open){ |
123 |
< |
write(this.toString(), Logger.SYSMSG, "Attempting to clear logfile"); |
123 |
> |
write(toString(), Logger.SYSMSG, "Attempting to clear logfile"); |
124 |
|
try{ |
125 |
|
// Close appending writer |
126 |
|
open = false; |
131 |
|
} |
132 |
|
catch(IOException e){ |
133 |
|
// We'd best log the error |
134 |
< |
write(this.toString(), Logger.ERROR, "Attempt to clear logfile failed: "+e.getMessage()); |
134 |
> |
write(toString(), Logger.ERROR, "Attempt to clear logfile failed - "+e.getMessage()); |
135 |
|
// As it's an IOException we should suspend logging |
136 |
|
open = false; |
137 |
|
} |
138 |
|
} |
139 |
|
else{ |
140 |
|
// If file is not open we should print this to the screen |
141 |
< |
System.out.println(formatLogLine(this.toString(), "Clearing failed: file not open")); |
141 |
> |
System.out.println(formatLogLine(toString(), "Clearing failed - file not open")); |
142 |
|
} |
143 |
|
} |
144 |
|
|
150 |
|
public void close(){ |
151 |
|
// Check to see if a file is open |
152 |
|
if(open){ |
153 |
< |
write(this.toString(), Logger.SYSMSG, "Attempting to terminate logging"); |
153 |
> |
write(toString(), Logger.SYSMSG, "Attempting to terminate logging"); |
154 |
|
try{ |
155 |
|
open = false; |
156 |
|
writer.close(); |
157 |
|
} |
158 |
|
catch(IOException e){ |
159 |
< |
write(this.toString(), Logger.ERROR, "Attempt to close logfile failed: "+e.getMessage()); |
159 |
> |
write(toString(), Logger.ERROR, "Attempt to close logfile failed - " + e.getMessage()); |
160 |
|
// As it's an IOException we should suspend logging |
161 |
|
open = false; |
162 |
|
} |
163 |
|
} |
164 |
|
else{ |
165 |
|
// If file is not open we should print this to the screen |
166 |
< |
System.out.println(formatLogLine(this.toString(), "Close failed: file not open")); |
166 |
> |
System.out.println(formatLogLine(toString(), "Close failed - file not open")); |
167 |
|
} |
168 |
|
} |
169 |
|
|
180 |
|
// Check to see if a file is open |
181 |
|
if(open){ |
182 |
|
// If file is already open then we should log that this went wrong |
183 |
< |
write(this.toString(), Logger.WARNING, "Open failed: a file is already open"); |
183 |
> |
write(this.toString(), Logger.WARNING, "Open failed - a file is already open"); |
184 |
|
} |
185 |
|
else{ |
186 |
|
this.filename = filename; |
187 |
|
if(!fileCheck()) { |
188 |
|
// Have to system.out.println errors because logging mechanism failed ! |
189 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed, construction terminated")); |
189 |
> |
System.out.println(formatLogLine(toString(), "File check failed, construction terminated")); |
190 |
|
throw new IOException("File check failed, unable to create FileLog"); |
191 |
|
} |
192 |
|
try{ |
196 |
|
open = true; |
197 |
|
} |
198 |
|
catch(IOException e){ |
199 |
< |
System.out.println(formatLogLine(this.toString(), "Attempt to open writer failed: "+e.getMessage())); |
199 |
> |
System.out.println(formatLogLine(toString(), "Attempt to open writer failed - " + e.getMessage())); |
200 |
|
} |
201 |
|
} |
202 |
|
} |
211 |
|
// Check to see if a file is open |
212 |
|
if(open){ |
213 |
|
// Make a note of the fact that writing has been suspended |
214 |
< |
write(this.toString(), Logger.SYSMSG, "Writing suspended"); |
214 |
> |
write(toString(), Logger.SYSMSG, "Writing suspended"); |
215 |
|
// Make sure writing not permitted |
216 |
|
open=false; |
217 |
|
} |
218 |
|
else{ |
219 |
|
// If file is not open we should print this to the screen |
220 |
< |
System.out.println(formatLogLine(this.toString(), "Suspend failed: file not open")); |
220 |
> |
System.out.println(formatLogLine(toString(), "Suspend failed - file not open")); |
221 |
|
} |
222 |
|
} |
223 |
|
|
230 |
|
// Check to see if a file is open |
231 |
|
if(open){ |
232 |
|
// If file is open we should print this to the screen |
233 |
< |
System.out.println(formatLogLine(this.toString(), "Resume failed: file open")); |
233 |
> |
System.out.println(formatLogLine(toString(), "Resume failed - file open")); |
234 |
|
} |
235 |
|
else{ |
236 |
|
// Permit writing again |
237 |
|
open=true; |
238 |
|
// Make a note of the fact that writing has been resumed |
239 |
< |
write(this.toString(), Logger.SYSMSG, "Writing resumed"); |
239 |
> |
write(toString(), Logger.SYSMSG, "Writing resumed"); |
240 |
|
} |
241 |
|
} |
242 |
|
|
244 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
245 |
|
* method to provide clean logging (every class should have this). |
246 |
|
* |
247 |
+ |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
248 |
+ |
* to format the toString() |
249 |
+ |
* |
250 |
|
* @return the name of this class and its CVS revision |
251 |
|
*/ |
252 |
|
public String toString() { |
253 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
253 |
> |
return FormatName.getName( |
254 |
> |
_name, |
255 |
> |
getClass().getName(), |
256 |
> |
REVISION); |
257 |
|
} |
258 |
|
|
259 |
|
//---PRIVATE METHODS--- |
303 |
|
} |
304 |
|
} |
305 |
|
catch(IOException e){ |
306 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed: "+e.getMessage())); |
306 |
> |
System.out.println(formatLogLine(toString(), "File check failed - "+e.getMessage())); |
307 |
|
} |
308 |
|
|
309 |
|
return fileOK; |
332 |
|
* A boolean signifying whether a file is open or not. |
333 |
|
*/ |
334 |
|
private boolean open = false; |
335 |
+ |
|
336 |
+ |
/** |
337 |
+ |
* This is the friendly identifier of the |
338 |
+ |
* component this class is running in. |
339 |
+ |
* eg, a Filter may be called "filter1", |
340 |
+ |
* If this class does not have an owning |
341 |
+ |
* component, a name from the configuration |
342 |
+ |
* can be placed here. This name could also |
343 |
+ |
* be changed to null for utility classes. |
344 |
+ |
*/ |
345 |
+ |
private String _name = Core.NAME; |
346 |
|
|
347 |
|
//---STATIC ATTRIBUTES--- |
348 |
|
|