}
public void run() {
try {
File tmp = new File(logFile);
RandomAccessFile file;
if (!tmp.exists()) {
file = new RandomAccessFile(logFile,"rw");
file.write(fileHeader.getBytes());
file.write("<log>\n".getBytes());
}
else {
file = new RandomAccessFile(logFile,"rw");
file.getChannel().position(file.getChannel().size() - "</log>".length());
}
while( ! stop ) {
if (eventsToWrite.isEmpty()) {
file.write("</log>".getBytes());
file.close();
return ;
}
String event = eventsToWrite.poll();
try {
file.write(event.getBytes());
} catch (Throwable e) {
e.printStackTrace();
if (stop) {
file.write("</log>".getBytes());
file.close();
return ;
}
}
}
file.write("</xml>".getBytes());
file.close();
}catch(Throwable t) {
t.printStackTrace();
}
}