magarena/src/magic/model/MagicGameLog.java

52 lines
1.4 KiB
Java
Raw Normal View History

2013-04-12 19:32:25 -07:00
package magic.model;
2020-01-15 12:02:42 -08:00
import static java.nio.charset.StandardCharsets.UTF_8;
2013-04-12 19:32:25 -07:00
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
2020-01-15 12:02:42 -08:00
2014-08-17 01:58:35 -07:00
import magic.utility.MagicFileSystem;
import magic.utility.MagicFileSystem.DataPath;
2013-04-12 19:32:25 -07:00
public class MagicGameLog {
private MagicGameLog() {}
public static final String LOG_FILE = "game.log";
2015-12-31 02:54:52 -08:00
private static final String gameLog = (System.getProperty("game.log") != null) ?
2015-12-31 02:54:52 -08:00
System.getProperty("game.log") :
MagicFileSystem.getDataPath(DataPath.LOGS).resolve(LOG_FILE).toString();
2013-04-12 19:32:25 -07:00
private static PrintWriter writer;
2013-06-23 18:33:35 -07:00
public static String getLogFileName(){
2014-09-03 22:37:26 -07:00
return gameLog;
}
2015-12-31 02:54:52 -08:00
2013-04-12 19:32:25 -07:00
public static void initialize() {
try {
writer = new PrintWriter(gameLog, UTF_8.name());
2013-04-12 19:32:25 -07:00
final StringBuilder sb = new StringBuilder();
MagicLogger.setLogHeader(sb, "MAGARENA GAME LOG");
2013-04-12 19:32:25 -07:00
sb.append("\n\n");
log(sb.toString());
} catch (FileNotFoundException|UnsupportedEncodingException e) {
2013-04-12 19:32:25 -07:00
System.err.println("Unable to create game log");
}
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
public static void log(final String message) {
if (writer != null) {
writer.println(message);
writer.flush();
2013-06-23 18:33:35 -07:00
}
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
public static void close() {
if (writer != null) {
writer.close();
}
}
}