innaircbot/src/main/java/InnaIrcBot/LogDriver/BotSystemWorker.java

91 lines
3.2 KiB
Java
Raw Normal View History

2019-01-27 06:19:31 +03:00
package InnaIrcBot.LogDriver;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class BotSystemWorker implements SystemWorker{
private FileWriter fileWriter;
2020-10-20 03:37:20 +03:00
private final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("HH:mm:ss");
2019-01-27 06:19:31 +03:00
private ThingToCloseOnDie thingToCloseOnDie; // call .die() method of this classes when this (system log class) dies.
2020-10-20 03:37:20 +03:00
private final String server;
2019-01-27 06:19:31 +03:00
private boolean consistent = false;
2020-10-20 03:37:20 +03:00
public BotSystemWorker(String server, String appLogDir){
this.server = server;
2019-01-27 06:19:31 +03:00
if (appLogDir.isEmpty()) {
2020-10-20 03:37:20 +03:00
appLogDir = System.getProperty("java.io.tmpdir")+File.separator+"innaircbot"+File.separator;
}
else if (! appLogDir.endsWith(File.separator)) {
appLogDir += File.separator;
2019-01-27 06:19:31 +03:00
}
2020-10-20 03:37:20 +03:00
appLogDir += server;
2019-01-27 06:19:31 +03:00
File logFile = new File(appLogDir);
2020-10-20 03:37:20 +03:00
2019-01-27 06:19:31 +03:00
try {
2020-10-20 03:37:20 +03:00
if (! logFile.getParentFile().exists()) {
if (! logFile.getParentFile().mkdirs()){
System.out.println("BotSystemWorker (@"+server+")->constructor() failed:\n" +
"\tUnable to create sub-directory(-ies) to store log file: " + appLogDir);
return;
}
}
fileWriter = new FileWriter(logFile, true);
2019-01-27 06:19:31 +03:00
consistent = true;
2020-10-20 03:37:20 +03:00
} catch (SecurityException e){
System.out.println("BotSystemWorker (@"+server+")->constructor() failed.\n" +
"\tUnable to create sub-directory(-ies) to store logs file ("+appLogDir+"):\n\t"+e.getMessage());
2019-01-27 06:19:31 +03:00
} catch (IOException oie){
2020-10-20 03:37:20 +03:00
System.out.println("BotSystemWorker (@"+server+")->constructor() failed:\n" +
"\tUnable to open file to store logs: " + appLogDir + " "+ oie.getMessage());
2019-01-27 06:19:31 +03:00
}
}
private String genDate(){
return "["+ LocalTime.now().format(dateFormat)+"] ";
}
@Override
public void logAdd(String event, String initiatorArg, String messageArg) {
if (consistent) {
try {
fileWriter.write(genDate() + event + " " + initiatorArg + " " + messageArg + "\n");
fileWriter.flush();
2020-10-20 03:37:20 +03:00
} catch (Exception e) { // ??? No ideas. Just in case. Consider removing.
System.out.println("BotSystemWorker (@" + server + ")->logAdd() failed\n\tUnable to write logs of because of exception:\n\t" + e.getMessage());
2019-01-27 06:19:31 +03:00
//this.close();
consistent = false;
}
2020-10-20 03:37:20 +03:00
return;
2019-01-27 06:19:31 +03:00
}
2020-10-20 03:37:20 +03:00
System.out.println(genDate() + event + " " + initiatorArg + " " + messageArg + "\n");
2019-01-27 06:19:31 +03:00
}
@Override
public void registerInSystemWorker(ThingToCloseOnDie thing){
if (this.thingToCloseOnDie == null){ // only one needed
this.thingToCloseOnDie = thing;
}
}
@Override
public void close() {
if (thingToCloseOnDie != null)
thingToCloseOnDie.die();
2020-10-20 03:37:20 +03:00
try {
fileWriter.close();
2019-01-27 06:19:31 +03:00
}
2020-10-20 03:37:20 +03:00
catch (IOException | NullPointerException ignore){}
2019-01-27 06:19:31 +03:00
consistent = false;
}
}