rendered paste bodypackage net.st0rm.ss.loghandlers;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import net.st0rm.ss.Log;
import net.st0rm.ss.gamecmdhandlers.GameCommandHandler;
import net.st0rm.ss.irc.IRC;
public abstract class LogHandler {
public static ArrayList<GameCommandHandler> gameCmdHandlers = new ArrayList<GameCommandHandler>();
private BufferedReader reader = null;
private File directory = null;
private LogReadThread thread = null;
private boolean skip = true;
/**
* Creates a new instance
* @param directory Directory of the files used
* @param skip Should the reader skip lines already existing?
*/
public LogHandler(File directory, boolean skip) {
this.directory = directory;
this.skip = skip;
}
public String getFileName() { return ""; };
private class LogReadThread extends Thread {
public boolean stop = false;
public void run() {
String line;
restartTomorrow();
try {
reader = new BufferedReader(new FileReader(directory.getAbsolutePath() + "/" + getFileName()));
if (skip) while ((reader.readLine()) != null);
while (!stop) {
try {
Thread.sleep(100);
Log.log("Sleep..." );
} catch (InterruptedException e) { System.out.println("Thread Interupted!"); }
while ((line = reader.readLine()) != null) { onNewLine(line); }
}
reader.close();
} catch (IOException e) {
Log.log(e);
}
}
/**
* Starts a thread to restart this reader on the next day
*
*/
void restartTomorrow() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND,59);
new Timer("Restart Timer").schedule(
new TimerTask() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
IRC.getInstance().msg("Restarting reader \"" + this.getClass().getSimpleName() + "\" for the new day.");
LogHandler.this.restartReader();
}
}
,c.getTime(), 3600000*24);
}
}
/**
* Call to re-start the file stream
*
*/
public void restartReader() {
if (thread != null) thread.stop = true;
thread = new LogReadThread();
thread.start();
}
/**
* Called when a new line is added to the file
* @param line The line added
*/
public void onNewLine(String line) { }
}