46 |
|
* @throws IOException if there is a problem with the file check. |
47 |
|
*/ |
48 |
|
public FileLogger() throws IOException{ |
49 |
– |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
49 |
|
filename = System.getProperty("uk.ac.ukc.iscream.LoggerClass.FileLogger.filename"); |
50 |
|
// Perform file check to make sure writing is ok |
51 |
|
if(!fileCheck()){ |
52 |
|
// Have to system.out.println errors because logging mechanism failed ! |
53 |
< |
System.out.println(formatLogLine(this.toString(), "File check failed, construction terminated")); |
53 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed, construction terminated")); |
54 |
|
throw new IOException("File check failed, unable to create FileLog"); |
55 |
|
} |
56 |
|
try{ |
60 |
|
open = true; |
61 |
|
} |
62 |
|
catch(IOException e){ |
63 |
< |
System.out.println(formatLogLine(toString(), "Attempt to setup writer failed - " + e.getMessage())); |
63 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Attempt to setup writer failed - " + e.getMessage())); |
64 |
|
} |
65 |
|
// Put an initial line into the log |
66 |
|
write(toString(), Logger.SYSINIT, "started"); |
68 |
– |
write(toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
67 |
|
} |
68 |
|
|
69 |
|
//---PUBLIC METHODS--- |
80 |
|
* @param source A string representation of the calling object. |
81 |
|
* @param message The message to be logged. |
82 |
|
*/ |
83 |
< |
public synchronized void write(String source, int verbosity, String message) { |
84 |
< |
if (verbosity <= _verbosityLevel) { |
85 |
< |
// Check to make sure file is open |
86 |
< |
if(open){ |
87 |
< |
// Produce a nicely formatted line for the logfile |
88 |
< |
String line = formatLogLine(source, message); |
89 |
< |
try{ |
90 |
< |
// We have to synchronize here due to problems with two write()'s being called before a newLine() |
91 |
< |
synchronized(writer){ |
92 |
< |
// Attempt to write the line |
93 |
< |
writer.write(line); |
94 |
< |
// Best to use newLine() as it will use the correct platform encoding |
95 |
< |
writer.newLine(); |
96 |
< |
} |
97 |
< |
// Make sure the line is written immeidiately |
98 |
< |
writer.flush(); |
99 |
< |
} |
100 |
< |
catch(IOException e){ |
101 |
< |
// We'd best log the error |
102 |
< |
System.out.println(formatLogLine(this.toString(), "Writing to logfile failed - " + e.getMessage())); |
103 |
< |
// As it's an IOException we should suspend logging |
104 |
< |
open = false; |
105 |
< |
} |
106 |
< |
} |
107 |
< |
else{ |
108 |
< |
// If file is not open we should print this to the screen |
111 |
< |
System.out.println(formatLogLine(this.toString(), "Write failed - file not open")); |
112 |
< |
} |
113 |
< |
} |
83 |
> |
public synchronized void write(String line, int verbosity) { |
84 |
> |
// Check to make sure file is open |
85 |
> |
if(open){ |
86 |
> |
// Produce a nicely formatted line for the logfile |
87 |
> |
try{ |
88 |
> |
// We have to synchronize here due to problems with two write()'s being called before a newLine() |
89 |
> |
synchronized(writer){ |
90 |
> |
// Attempt to write the line |
91 |
> |
writer.write(line); |
92 |
> |
// Best to use newLine() as it will use the correct platform encoding |
93 |
> |
writer.newLine(); |
94 |
> |
} |
95 |
> |
// Make sure the line is written immeidiately |
96 |
> |
writer.flush(); |
97 |
> |
} |
98 |
> |
catch(IOException e){ |
99 |
> |
// We'd best log the error |
100 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Writing to logfile failed - " + e.getMessage())); |
101 |
> |
// As it's an IOException we should suspend logging |
102 |
> |
open = false; |
103 |
> |
} |
104 |
> |
} |
105 |
> |
else{ |
106 |
> |
// If file is not open we should print this to the screen |
107 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Write failed - file not open")); |
108 |
> |
} |
109 |
|
} |
110 |
|
|
111 |
|
/** |
135 |
|
} |
136 |
|
else{ |
137 |
|
// If file is not open we should print this to the screen |
138 |
< |
System.out.println(formatLogLine(toString(), "Clearing failed - file not open")); |
138 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Clearing failed - file not open")); |
139 |
|
} |
140 |
|
} |
141 |
|
|
160 |
|
} |
161 |
|
else{ |
162 |
|
// If file is not open we should print this to the screen |
163 |
< |
System.out.println(formatLogLine(toString(), "Close failed - file not open")); |
163 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Close failed - file not open")); |
164 |
|
} |
165 |
|
} |
166 |
|
|
183 |
|
this.filename = filename; |
184 |
|
if(!fileCheck()) { |
185 |
|
// Have to system.out.println errors because logging mechanism failed ! |
186 |
< |
System.out.println(formatLogLine(toString(), "File check failed, construction terminated")); |
186 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed, construction terminated")); |
187 |
|
throw new IOException("File check failed, unable to create FileLog"); |
188 |
|
} |
189 |
|
try{ |
193 |
|
open = true; |
194 |
|
} |
195 |
|
catch(IOException e){ |
196 |
< |
System.out.println(formatLogLine(toString(), "Attempt to open writer failed - " + e.getMessage())); |
196 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Attempt to open writer failed - " + e.getMessage())); |
197 |
|
} |
198 |
|
} |
199 |
|
} |
214 |
|
} |
215 |
|
else{ |
216 |
|
// If file is not open we should print this to the screen |
217 |
< |
System.out.println(formatLogLine(toString(), "Suspend failed - file not open")); |
217 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Suspend failed - file not open")); |
218 |
|
} |
219 |
|
} |
220 |
|
|
227 |
|
// Check to see if a file is open |
228 |
|
if(open){ |
229 |
|
// If file is open we should print this to the screen |
230 |
< |
System.out.println(formatLogLine(toString(), "Resume failed - file open")); |
230 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "Resume failed - file open")); |
231 |
|
} |
232 |
|
else{ |
233 |
|
// Permit writing again |
256 |
|
//---PRIVATE METHODS--- |
257 |
|
|
258 |
|
/** |
264 |
– |
* This method generates a nicely formatted line for the log, |
265 |
– |
* including the date/time and the source of the message. The date |
266 |
– |
* and time are formatted using the DateFormat class, and the source |
267 |
– |
* class is formatted using the toString() method found in every |
268 |
– |
* source file. This is then prepended to the message and returned. |
269 |
– |
* |
270 |
– |
* @param source A string representation of the calling object. |
271 |
– |
* @param message The message to be logged. |
272 |
– |
* @return The string to be written to the log. |
273 |
– |
*/ |
274 |
– |
private String formatLogLine(String source, String message){ |
275 |
– |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
276 |
– |
return "[" + date + "] " + source + ": " + message; |
277 |
– |
} |
278 |
– |
|
279 |
– |
/** |
259 |
|
* The fileCheck() method is used to ensure that writing is ok. It |
260 |
|
* performs so basic checks to make sure that if the file exists |
261 |
|
* it can be written to, and if not a new file is created. A boolean |
284 |
|
} |
285 |
|
} |
286 |
|
catch(IOException e){ |
287 |
< |
System.out.println(formatLogLine(toString(), "File check failed - "+e.getMessage())); |
287 |
> |
System.out.println(FormatName.formatLogLine(this.toString(), Logger.FATAL, "File check failed - "+e.getMessage())); |
288 |
|
} |
289 |
|
|
290 |
|
return fileOK; |
291 |
|
} |
292 |
|
|
293 |
+ |
private void write(String source, int verbosity, String message) { |
294 |
+ |
write(FormatName.formatLogLine(source, verbosity, message), verbosity); |
295 |
+ |
} |
296 |
+ |
|
297 |
|
//---ACCESSOR/MUTATOR METHODS--- |
298 |
|
|
299 |
|
//---ATTRIBUTES--- |
302 |
|
* The filename of the currently open, or last open, file. |
303 |
|
*/ |
304 |
|
private String filename; |
322 |
– |
|
323 |
– |
/** |
324 |
– |
* The verbosity level of this instance |
325 |
– |
*/ |
326 |
– |
private int _verbosityLevel; |
305 |
|
|
306 |
|
/** |
307 |
|
* A reference to the writer being used. |